當前位置:首頁 » 編程語言 » mysqlsql查詢樹形結構
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

mysqlsql查詢樹形結構

發布時間: 2022-06-23 19:39:12

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

樹形結構統一使用下面的測試表與測試數據
CREATE TABLE test_tree (
test_id INT,
pid INT,
test_val VARCHAR(10),
PRIMARY KEY (test_id)
);

INSERT INTO test_tree VALUES(1, NULL, '.NET');
INSERT INTO test_tree VALUES(2, 1, 'C#');
INSERT INTO test_tree VALUES(3, 1, 'J#');
INSERT INTO test_tree VALUES(4, 1, 'ASP.NET');
INSERT INTO test_tree VALUES(5, 1, 'VB.NET');

INSERT INTO test_tree VALUES(6, NULL, 'J2EE');
INSERT INTO test_tree VALUES(7, 6, 'EJB');
INSERT INTO test_tree VALUES(8, 6, 'Servlet');
INSERT INTO test_tree VALUES(9, 6, 'JSP');

INSERT INTO test_tree VALUES(10, NULL, 'Database');
INSERT INTO test_tree VALUES(11, 10, 'DB2');
INSERT INTO test_tree VALUES(12, 10, 'MySQL');
INSERT INTO test_tree VALUES(13, 10, 'Oracle');
INSERT INTO test_tree VALUES(14, 10, 'SQL Server');

INSERT INTO test_tree VALUES(15, 13, 'PL/SQL');
INSERT INTO test_tree VALUES(16, 15, 'Function');
INSERT INTO test_tree VALUES(17, 15, 'Procere');
INSERT INTO test_tree VALUES(18, 15, 'Package');
INSERT INTO test_tree VALUES(19, 15, 'Cursor');

INSERT INTO test_tree VALUES(20, 14, 'T-SQL');

Oracle
使用 START WITH CONNECT BY
語句實現樹狀查詢

SQL> ed
Wrote file afiedt.buf

1 SELECT
2 LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val
3 FROM
4 test_tree
5 START WITH
6 test_id IN (1, 6, 10)
7* CONNECT BY PRIOR test_id = pid
SQL> /

TEST_VAL
-----------------------------------------------------------

.NET
C#
J#
ASP.NET
VB.NET
J2EE
EJB
Servlet
JSP
Database
DB2

TEST_VAL
-----------------------------------------------------------

MySQL
Oracle
PL/SQL
Function
Procere
Package
Cursor
SQL Server
T-SQL

20 rows selected.

SQL Server
使用 Common Table Expression (CTE) 來實現 遞歸調用。

1> WITH StepCTE
2> AS
3> (
4> SELECT
5> test_id,
6> pid,
7> test_val,
8> 1 as Lev
9> FROM
10> test_tree
11> WHERE
12> test_id IN (1,6,10)
13> UNION ALL
14> SELECT
15> T.test_id,
16> T.pid,
17> T.test_val,
18> CTE.Lev + 1
19> FROM
20> test_tree T INNER JOIN StepCTE CTE
21> ON T.pid = CTE.test_id
22> )
23> SELECT
24> test_id, pid, test_val, Lev
25> FROM StepCTE;
26> go
test_id pid test_val Lev
----------- ----------- ---------- -----------
1 NULL .NET 1
6 NULL J2EE 1
10 NULL Database 1
11 10 DB2 2
12 10 MySQL 2
13 10 Oracle 2
14 10 SQL Server 2
20 14 T-SQL 3
15 13 PL/SQL 3
16 15 Function 4
17 15 Procere 4
18 15 Package 4
19 15 Cursor 4
7 6 EJB 2
8 6 Servlet 2
9 6 JSP 2
2 1 C# 2
3 1 J# 2
4 1 ASP.NET 2
5 1 VB.NET 2

(20 行受影響)

2. mysql 如何查詢一個帶有樹結構的表的數據

當然這種結構就不要追求什麼效率了。如果要效率高的,只能改表結構。

1:select p2.id from table p1 ,table p2 where p1.id=p2.pid and p1.id=0
2:假設表名是tree
SQL codeselect distinct a.id from tree as a inner join tree as b on (a.pid = b.pid) where b.pid >=0;
select distinct a.id from tree as a inner join tree as b on (a.pid = b.pid) where b.pid >=2;

3.通過程序或資料庫的store procere來實現了。 在mySQL中無法以一句SQL實現。

3. MYSQL 查詢樹形結構數據,查詢某個節點下的所有子節點數據。

java版的實際例子。類同你說的情況

private void findChildList(AssetType parent,List<AssetType> list){
String hql = "from AssetType a where a.parentAssetType.assetTypeId=? ORDER BY a.sort,a.assetTypeName asc";

List<AssetType> childList = this.assetTypeDao
.getEntityManager()
.createQuery(hql)
.setParameter(1, parent.getAssetTypeId())
.getResultList();
int size = childList.size();
if(size>0){
for (int i = 0; i < size; i++) {
AssetType assetType = childList.get(i);
List<AssetType> childs = assetType.getChildAssetType();
if(childs.size()>0){
list.addAll(childs);
this.findChildList(assetType, list);//遞歸查詢節點的子節點
}
}
}
}

4. 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']);
}
}});

5. mysql中查詢資料庫中表名稱和結構的sql語句是什麼啊啊

TABLE 語句

具體語法:TABLE table_name [ORDER BY column_name] [LIMIT number [OFFSET number]]
其實從語法上看,可以排序,也可以過濾記錄集,不過比較簡單,沒有 SELECT 那麼強大。

示例 1

簡單的建一張很小的表 y1,記錄數為 10 條。表 t1,插入 10 條記錄

  • mysql-(ytt/3305)->create table t1 (r1 int,r2 int);

  • Query OK, 0 rows affected (0.02 sec)

  • mysql-(ytt/3305)->insert into t1

  • with recursive aa(a,b) as (

  • select 1,1

  • union all

  • select a+1,ceil(rand()*20) from aa where a < 10

  • ) select * from aa;

  • Query OK, 10 rows affected (0.00 sec)

  • Records: 10 Duplicates: 0 Warnings: 0

  • 簡單全表掃描mysql-(ytt/3305)->select * from t1;+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)

  • TABLE 結果mysql-(ytt/3305)->table t1;+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)

  • 看下 table 的執行計劃mysql-(ytt/3305)->explain table t1 order by r1 limit 2G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 10 filtered: 100.00 Extra: Using filesort1 row in set, 1 warning (0.00 sec)

  • 其實可以看到 TABLE 內部被 MySQL 轉換為 SELECT 了。mysql-(ytt/3305)->show warningsG*************************** 1. row *************************** Level: Note Code: 1003Message: /* select#1 */ select `ytt`.`t1`.`r1` AS `r1`,`ytt`.`t1`.`r2` AS `r2` from `ytt`.`t1` order by `ytt`.`t1`.`r1` limit 21 row in set (0.00 sec)

  • 那其實從上面簡單的例子可以看到 TABLE 在內部被轉成了普通的 SELECT 來處理。示例 2應用於子查詢里的子表。這里要注意,內表的欄位數量必須和外表過濾的欄位數量一致。克隆表 t1 結構mysql-(ytt/3305)->create table t2 like t1;Query OK, 0 rows affected (0.02 sec)

  • 克隆表 t1 數據mysql-(ytt/3305)->insert into t2 table t1;Query OK, 10 rows affected (0.00 sec)Records: 10 Duplicates: 0 Warnings: 0

  • table t1 被當做內表,表 t1 有兩個欄位,必須同時滿足 t2 檢索時過濾的欄位也是兩個。mysql-(ytt/3305)->select * from t2 where (r1,r2) in (table t1);+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)

  • 注意:這里如果過濾的欄位數量和子表數量不一致,則會報錯。

6. mysql查詢|Mybatis查詢

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

7. mysql 如何查詢樹形狀 評論 回復

可以自己進行編寫評論的留言板塊,也可以使用網上現成的查件,我最近使用的暢言評論系統就非常好用,支持PC端和移動端,同時支持自適應,只需要進行設置就可以獲得代碼,簡單方便!

8. mysql 查詢樹形介面sql

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

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

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