当前位置:首页 » 编程语言 » 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不必写的太复杂。