當前位置:首頁 » 編程語言 » mysql查詢樹形結構sql

mysql查詢樹形結構sql

發布時間: 2022-12-24 16:25:47

A. mysql 查詢樹形介面sql

mysql啊,這個還真不知道可不可以。不過oracle可以,遞歸查詢上上級,或者查詢到下下級都可以。代碼參考:
查詢出員工號為7788的所有上級。
select * from scott.emp start with empno=7788 connect by prior mgr= empno;

mysql裡面如果sql不能實現,那就用程序裡面的list啊,查詢一個添加一個,循環(while)直到它的上級id為空為止。

事實上更建議用程序的方法,程序寫代碼更靈活,而sql不必寫的太復雜。

B. 怎麼往資料庫里插入一個樹形結構的表,並且用一句SQL語句將其遍歷出來

ID Value PID
0 根節點 null
1 節點1 0
2 節點2 0
3 節點3 1
..........

然後查詢出來 再形成樹就可以了

C. jsp怎麼從mysql資料庫把樹形結構展現出來

jsp從mysql資料庫讀取數據,並填充到樹形結構菜單並展現出來的實現方法:

1、引入jquery.treeview.js樹控制項

<script type="text/javascript" src="jquery/easyui/jquery.min.js"></script>
<script type="text/javascript" src="jquery/easyui/jquery.easyui.min.js"></script>

2、jsp頁面中獲取後台mysql數據,並傳到jsp頁面來

<%
// 資料庫的名字
String dbName = "zap";
// 登錄資料庫的用戶名
String username = "sa";
// 登錄資料庫的密碼
String password = "123";
// 資料庫的IP地址,本機可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1";
// 資料庫的埠,一般不會修改,默認為1433
int port = 1433;
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username
+ ";password=" + password;
//
//聲明需要使用的資源
// 資料庫連接,記得用完了一定要關閉
Connection con = null;
// Statement 記得用完了一定要關閉
Statement stmt = null;
// 結果集,記得用完了一定要關閉
ResultSet rs = null;
try {
// 注冊驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 獲得一個資料庫連接
con = DriverManager.getConnection(connectionUrl);
String SQL = "SELECT * from note";
// 創建查詢
stmt = con.createStatement();
// 執行查詢,拿到結果集
rs = stmt.executeQuery(SQL);
while (rs.next()) {
%>
<tr>

3、填充樹形菜單:

{
id : "string" // will be autogenerated if omitted
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
children : [] // array of strings or objects
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}

$('#tree').jstree({
'core' : {
'data' : function (obj, cb) {
cb.call(this,
['Root 1', 'Root 2']);
}
}});

D. 怎樣在 MySQL 表中存儲樹形結構數據

在 MySQL 表中存儲樹形結構數據:
一般比較普遍的就是四種方法:(具體見 SQL Anti-patterns這本書)
Adjacency List:每一條記錄存parent_id
Path Enumerations:每一條記錄存整個tree path經過的node枚舉
Nested Sets:每一條記錄存 nleft 和 nright
Closure Table:維護一個表,所有的tree path作為記錄進行保存。

E. mysql查詢|Mybatis查詢

1、mysql肯定可以實現
2、樹形結構的實現其實很簡單的,建議你看下ztree的官方api,你只需要按照數據結構遞歸查詢出父子節點的數據即可
3、mybatis是java中實現的方式了,至於你想怎麼優化,最後都是遞歸查詢父子節點的數據

熱點內容
反編譯封掛 發布:2024-05-17 06:17:42 瀏覽:819
台電怎麼從電腦轉至安卓 發布:2024-05-17 06:07:23 瀏覽:435
我的世界國際版伺服器里如何開掛 發布:2024-05-17 06:06:05 瀏覽:872
如何同步各個伺服器時間 發布:2024-05-17 06:00:19 瀏覽:713
上課用平板是什麼配置 發布:2024-05-17 05:29:51 瀏覽:565
linux指定組 發布:2024-05-17 05:04:39 瀏覽:86
魯班演算法 發布:2024-05-17 04:59:46 瀏覽:489
分布鋼筋配置有哪些 發布:2024-05-17 04:51:57 瀏覽:148
軒逸寶來朗逸配置一樣哪個平易 發布:2024-05-17 04:48:59 瀏覽:433
c盤刪除緩存文件對系統有影響嗎 發布:2024-05-17 04:27:47 瀏覽:340