這裡蒐索程式師資訊,查找有用的技術資料
當前位置:首頁 » 編程語言 » sql樹形結構查詢mysql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql樹形結構查詢mysql

發布時間: 2023-03-02 12:11:41

1. 解決mysql查詢資料庫所有的表名稱和表結構的sql語句怎麼寫

查詢MySQL資料庫所有表名的SQL命令:

show tables;

CREATE TABLE `students` (

`sid` char(10) NOT NULL,

`sname` varchar(50) NOT NULL,

`sex` char(1) NOT NULL,

`dob` date NOT NULL,

`phone` varchar(30) DEFAULT NULL,

PRIMARY KEY (`sid`),

KEY `index_tbl1_url` (`phone`(20))

) ENGINE=InnoDB DEFAULT CHARSET=gb2312

2. sql語句實現遞歸查詢所有節點,mysql和oracle都能用的

首先說一下Oracle的遞歸查詢,相信大部分人都知道很簡單。無非start with connect by 函數。下面是從pId向子節點遞歸查詢的例子,unId是資料庫表中的主鍵。

如果是從子節點遞歸到父節點查詢,就把start with 換成unid,prior左右對換

下面再講MySql 的遞歸查詢方式。MySql沒有Oracle的強大功能,雖然都是同一個公司的產品。所以只能靠自己寫。有很多方法,用sql去循環查詢,或者寫存儲過程,我這里只提供一種。就是新建一個function函數。

表結構不說了,無非就是 Id ,pId,其他列。下面是創建一個遞歸查詢子節點的函數

DROP FUNCTION IF EXISTS queryChildrenPowerInfo;

CREATE FUNCTION `queryChildrenPowerInfo` (powerId VARCHAR(2000))

RETURNS VARCHAR(2000)

BEGIN

DECLARE sTemp VARCHAR(2000);

DECLARE sTempChd VARCHAR(2000);

SET sTemp = '$';

SET sTempChd = cast(powerId as CHAR);

WHILE sTempChd is not NULL DO

SET sTemp = CONCAT(sTemp, ',', sTempChd);

SELECT group_concat(id) INTO sTempChd FROM t_discretionary_power where FIND_IN_SET(pId,sTempChd)>0;

END WHILE;

return sTemp;

調用的時候:select queryChildrenPowerInfo(""); 該語句會返回Id和父Id等於傳入參數powerId的一個字元串,中間有逗號隔開如圖

下面這句代碼的意思是,查詢出 t_discretionary_power 表中,t.id 等於上面查詢出的結果集的數據。FIND_IN_SET(A,B)是MYSQL的函數。意思是查找在B集合中有A的數據。相當於In

select t.* from t_discretionary_power t where FIND_IN_SET(t.id,queryChildrenPowerInfo(''))

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

4. mysql查詢|Mybatis查詢

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

5. 怎麼往資料庫里插入一個樹形結構的表,並且用一句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 行受影響)

6. 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實現。

7. mysql 查詢樹形介面sql

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

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

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