ClassID ParentID ClassName
1 0 医院概况
2 0 医院动态
3 0 科室介绍
4 1 医院简介
5 1 医院领导介绍
6 1 院长致辞
转换成这样的
ClassID ParentID ClassName
1 0 医院概况
4 1 医院简介
5 1 医院领导介绍
6 1 院长致辞
2 0 医院动态
3 0 科室介绍
其实就是父子关系表 子类紧跟父类
/*
标题:SQL SERVER 2005中树结构按照各顶级节点排序
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-10-24
地点:陕西西安
说明:
如下树级结构中需要按照各顶级节点排序
id pid name
---- ---- ----------
001 000 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇
011 000 江西省
012 011 南昌市
013 012 南昌县
014 012 新建县
015 012 进贤县
016 012 安义县
017 013 麻丘镇
018 011 九江市
需要的结果:
id name pid
---- ---------- ----
001 广东省 000
002 广州市 001
004 天河区 002
003 深圳市 001
005 罗湖区 003
006 福田区 003
007 宝安区 003
008 西乡镇 007
009 龙华镇 007
010 松岗镇 007
011 江西省 000
012 南昌市 011
013 南昌县 012
017 麻丘镇 013
014 新建县 012
015 进贤县 012
016 安义县 012
018 九江市 011
*/
create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , '000' , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
insert into tb values('011' , '000' , N'江西省')
insert into tb values('012' , '011' , N'南昌市')
insert into tb values('013' , '012' , N'南昌县')
insert into tb values('014' , '012' , N'新建县')
insert into tb values('015' , '012' , N'进贤县')
insert into tb values('016' , '012' , N'安义县')
insert into tb values('017' , '013' , N'麻丘镇')
insert into tb values('018' , '011' , N'九江市')
go
;with t as
(
select px = cast(id as varchar(500)) , * from tb t
where not exists(select 1 from tb where id = t.pid)
union all
select cast(b.px + ',' + a.id as varchar(500)) , a.*
from tb a , t b where a.pid = b.id
)
select id , name , pid from t order by px
drop table tb
create table tb (id int , Name varchar(10) , pid int )
insert into tb values(1 ,'广东省' , 0)
insert into tb values(2 ,'四川省' , 0)
insert into tb values(3 ,'湖北省' , 0)
insert into tb values(4 ,'东莞市' , 1)
insert into tb values(5 ,'广州市' , 1)
insert into tb values(6 ,'天河区' , 5)
insert into tb values(7 ,'绵阳市' , 2)
insert into tb values(8 ,'武汉市' , 3)
insert into tb values(9 ,'汉口区' , 8)
insert into tb values(10,'随州市' , 3)
go
create function f_getnum(@id int) returns varchar(4000)
as
begin
declare @ret varchar(4000) , @pid int
set @ret = right(' ' + rtrim(@id) , 4)
while exists(select 1 from tb where id = @id and pid <> 0 )
begin
select @pid = pid from tb where id = @id and pid <> 0
set @id = @pid
set @ret = right(' ' + rtrim(@id) , 4) + @ret
end
return @ret
end
go
select id , name , REPLICATE(' ' , len(dbo.f_getnum(id))/4 - 1) + name as name from tb order by dbo.f_getnum(id)
drop function f_getNum
drop table tb
/*
id name name
----------- ---------- --------------
1 广东省 广东省
4 东莞市 东莞市
5 广州市 广州市
6 天河区 天河区
2 四川省 四川省
7 绵阳市 绵阳市
3 湖北省 湖北省
8 武汉市 武汉市
9 汉口区 汉口区
10 随州市 随州市
(所影响的行数为 10 行)
*/
create table tb(ClassID int,ParentID int,ClassName nvarchar(10))
insert into tb
select 1,0,'医院概况' union all
select 2,0,'医院动态' union all
select 3,0,'科室介绍' union all
select 4,1,'医院简介' union all
select 5,1,'医院领导介绍' union all
select 6,1,'院长致辞'
go
select * from tb order by (case when parentid=0 then classid else parentid end)
go
drop table tb
/*
ClassID ParentID ClassName
----------- ----------- ----------
1 0 医院概况
4 1 医院简介
5 1 医院领导介绍
6 1 院长致辞
2 0 医院动态
3 0 科室介绍
(6 行受影响)
*/
这是查询语句查出的结果,我想用这个条件,来查询这些区域的某项成绩。  我是这么写的,好像不行。 ![图片说明]...
1.序言 记得前几年,还是初级的时候,面试官问到,请你讲一下sql语句的执行顺序。当时我以为就是按照sql的关键字排列顺序来执行的。...了解一个sql语句的执行顺序,可以让我们清楚到sql执行时的操作顺序,进...
表中我们可以发现一个科目会有多个老师教,如果我们直接进行表连接的话就会出现问题,这就牵扯到我们的一对多的sql语句,从而让我们写一条sql语句就带到一对多的效果 sql语句一对多(spr_course和sys_user表) SELECT spr...
Oracle和SQLPlus 每次启动只需启动两个...2.OracleService***:数据库的实例服务,他的命名标准:OracleServiceSID,每当为系统增加一个数据库的时候都会自动的出现一个类似的服务名称。 在默认情况下,SID的名称...
一、Select语句: select语句除了可以查看数据库中的表格和视图的信息外,还可以查看SQL...一个基本的select语句可以分解成三个部分:查找什么数据(select)、从哪里查找(from)、查找的条件是什么(where)。 selec
如题 oracle查询语句 一个加t 一个不加t 结果竟然不同 求大神解释
SELECT ROUND(AVG(CESHICHENGJI),2) AS pjcj FROM t_datacenter WHERE CESHIXIANGMUMINGCHENG = 身高 AND t_datacenter.JIGOUDAIMA LIKE (SELECT t_department.CODE FROM t_department WHERE t_department.NAME = '...
单表查询: #查询pname和price,去掉pname和price同时重复的数据 select distinct pname,price from product; #查询商品名和价格并起别名 select pname as 商品名称,price as 商品价格 from product; #查询商品...
好像这个问题应该很好回答,毕竟自己已经写了无数个 SQL 查询了,有一些还很复杂的。还装不了这个逼了?! 但事实是,我仍然很难确切地说出它的顺序是怎样的。 言归正传,SELECT语句的完整语法如下: 1. ...
将一个语句的查询字段结果作为另一个语句的查询字段并返回查询的值 SELECT PJ_JFSf FROM tg_zb_zbkzb WHERE zbuuid=v_zbuuid) --case when sum(fm) > 0 then sum(fz)/sum(fm) ELSE 0 end create or ...
在查询之前,我们也先准备好一些数据 -- 创建 db2 数据库 CREATE DATABASE db2;-- 使用 db2 数据库 USE db2;-- 数据 student 数据表 CREATE TABLE student ( id INT, -- 编号 NAME VARCHAR(20), -- 姓名 ...
数据库基本查询语句规范为:select 区域 from 表名 查询指定表 select * from 表名 *:代表所有列 示例:select * from TL_REQUEST 查询指定列 select 列名 from 表名 列名:代表从指定的列名中查找 ,:...
以员工表:emp 为例 id name gender birthday dept job sal bonus 编号 姓名 性别 ...基本查询 – 查询emp表中的所有员工信息 ...– 查询emp表中的所有员工的姓名、薪资、奖金 ...– 查询emp表中...
--查询语句 SELECT * FROM BASE_USER; --查询前10条语句 SELECT TOP 10 * FROM BASE_USER; --允许脏读查询 SELECT * FROM BASE_USER WITH(NOLOCK); --获取当前数据库名称 SELECT DB_NAME(dbid) as dbName FROM ...
** 一、效率高的写法 ** 1.无ORDER BY排序的写法。...即使查询的数据量再大,也几乎不受影响,速度依然!) SELECT * FROM (SELECT ROWNUM AS rowno, t.* FROM emp t WHERE hire_date BETWEEN TO_DATE ...
2.结果集:通过查询语句查询出来的数据以表的形式展示,我们称这个表为虚拟结果集。存放在内存中。查询返回的结果是一张虚拟表。 3.查询指定列的数据: SELECT 列名1,列表2…FROM 表名; 条件查询: **1.**条件查询...
如果觉得 Mongodb 语句不太好理解,可以和 SQL 语句进行对比,学起来要容易很多。 1. 查询(find) 查询所有结果 select * from article db.article.find() 指定返回哪些键 select title, author from article ...
有一个包含产品目录的数据库表,其中每一类物品占一行。对于每一种物品,要存储的信息包含产品描述,价格,以及生产该产品的供应商。 现在有同一供应商生产的多个产品,那么在何处存储供应商名称,地址,联系方法等...
查询名称里面带蔡徐坤的名字 select * from stu where name like '%蔡徐坤%'; 这样就能查询出“是蔡徐坤是”,“蔡徐坤咯”这样类似的名称 ...这样名称前面就只能多一个字符,多了就查不出 ...
条件查询 注意:条件查诟需要用到 where 询句,where 必须放到 from 询句表的后面; 执行顺序:先from再where过滤后再检索出来。 1、、支持如下运算 2、等号(=)操作符 (1)、查询薪水为5000的员工 例如:...
Oracle查询语句 Select 选择哪一个列,from 列在哪一个表 例如:我要查员工姓名(last_name),员工姓名在employees表里,实现代码以及输出截图: Select last_name from employees; 如果要查询两个列的话要用...
全面的sql查询总结
sql语句动态拼接多个查询条件查询数据库 问题描述: 公司开发中自己负责的模块业务需要根据用户选择内容去查询数据库,其中会计期间是必须的条件,默认为当前日期。用户只需要根据需要去输入就可以进行相关查询。...
先来一波查询语句语法的基本解释: 列子: db.mycol.find({&quot;likes&quot;: {$gt:10}, $or: [{&quot;by&quot;: &quot;yiibai tutorials&quot;}, {&...
实验四 数据库的组合查询和统计查询 以下通过几个实例来演示sql语句的基本查询和多表联查,以及统计查询 假设目前已经创建好名成为’JXGL’的数据库,并且... SELECT查询语句 --查询所有的学生的学号和姓名 U...
查看 SQL Server 中所有数据库的信息 select * from sysdatabases 查看当前使用的数据库中所有表信息 ...select * from sysobjects where type='U' select * from sysobjects where type not in('U','S','SQ','...
(查第一个表字段)as first left join (查第二个表字段) as second on first.一表字段名=second.二表字段名 ) where 条件 举个栗子: select a.name,count(*) as Num from ( (select top 6 name...
mysql查询语句 SELECT column_name,column_name FROM table_name [WHERE Clause] [OFFSET M ][LIMIT N] 1、where条件查询 SELECT field1, field2,...fieldN FROM table_name1, table_name2... [WHERE ...
假设需要查询 select count * from tablename where a=...(a有4个可能的值) and b = ...(a有5个可能的值) 所以总共有20种组合 ... 如果一个个查的就有20条语句, 不知道有什么方法可以减少查询的语句数量,跪求
Spring Boot JPA的查询语句 之前的文章中,我们讲解了如何使用Spring Boot JPA, 在Spring Boot JPA中我们可通过构建查询方法或者通过@Query注解来构建查询语句,本文我们将会更详细的讨论查询语...