⑴ sql 模糊查詢按匹配度排序的原理
SELECT`id`,`text`,(CASEWHENtextLIKE'%dell%'ANDtextLIKE'%poweredge%'THEN2ELSE1END)aslevelsFROM`t_name`ORDERBYlevelsdesc
⑵ 我用sql語句查詢出樹形欄目列表,但我想排一下順序,請大哥大姐幫幫忙!
select name from table
group by (case when level=1 then 大類 ,
when level =2 then 中類 ,
else 小類 end) 類別
order by 類別
⑶ 在MySql下,怎麼用SQL語句遍歷一個樹結構
f exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tb]
GO
--示例數據
create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
insert [tb] select 0,'中國'
union all select 0,'美國'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江蘇'
union all select 6,'蘇州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'無錫'
union all select 2,'紐約'
union all select 2,'舊金山'
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_id]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_id]
GO
/*--樹形數據處理
級別及排序欄位
--鄒建 2003-12(引用請保留此信息)--*/
/*--調用示例
--調用函數實現分級顯示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
--*/
create function f_id()
returns @re table([id] int,[level] int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select [id],@l,right(10000+[id],4)
from [tb] where [pid]=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)
from [tb] a,@re b
where a.[pid]=b.[id] and b.[level]=@l-1
end
return
end
go
--調用函數實現分級顯示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
go
--刪除測試
drop table [tb]
drop function f_id
go
/*--結果
中國
----北京
----上海
----江蘇
--------蘇州
------------常熟
--------南京
--------無錫
美國
----紐約
----舊金山
加拿大
--*/
⑷ sqlserver2008樹查詢,急急急,求大神教育
你想要達到的預期效果是什麼?
⑸ sql里樹形結構分組排序
createtableT1(thisvarchar(10),parentvarchar(10))
insertintoT1(this,parent)
values('id1',null)
,('id2',null)
,('id3','id1')
,('id4','id2')
,('id5','id3')
,('id6','id3')
,('id7','id4')
,('id8','id7')
--sqlserver的cte功能
withtree(this,parent,root,depth)as(
selectthis,parent,thisasroot,
unionall
selecta.this,a.parent,b.root,b.depth+1asdepthfromT1a,treebwherea.parent=b.this
)
selectthis,parent,root,depth
fromtree
orderbyroot,depth,this
⑹ SQL聚集索引:「原來的數據會按照索引鍵排序後,重新存在硬碟上」什麼意思
聚集索引: 該索引中鍵值的邏輯順序決定了表中相應行的物理順序。如果用 新華字典 作例子來一個例子的話。 [拼音]就可以看作是聚集索引 例如 吖、阿、啊 在字典的最前面。 左、作、坐 在字典的最後面。 拼音[邏輯順序]很接近,在字典中頁數的位置[物理順序]也很接近。
非聚集索引: 非聚集索引與聚集索引一樣有 B 樹結構,但是有兩個重大差別: 數據行不按非聚集索引鍵的順序排序和存儲。 非聚集索引的葉層不包含數據頁。 相反,葉節點包含索引行。每個索引行包含非聚集鍵值以及一個或多個行定位器, 這些行定位器指向有該鍵值的數據行(如果索引不唯一,則可能是多行)。如果用 新華字典 作例子來一個例子的話。 [筆畫]就可以看作是非聚集索引 例如 化 仇 仃 僅 仂 這幾個字,都是 單人旁,筆畫數相同的。 筆畫[邏輯順序]很接近,在字典中頁數的位置[物理順序]則在不同的位置上。
⑺ SqlServer樹形結構的深度排序怎麼實現
/*
轉一個鄒老大的例子
*/
--測試數據
DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
INSERT @t SELECT '001',NULL ,'山東省'
UNION ALL SELECT '002','001','煙台市'
UNION ALL SELECT '003','001','青島市'
UNION ALL SELECT '004','002','招遠市'
UNION ALL SELECT '005',NULL ,'四會市'
UNION ALL SELECT '006','005','清遠市'
UNION ALL SELECT '007','006','小分市'
--深度排序顯示處理
--生成每個節點的編碼累計(相同當單編號法的編碼)
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM @t
WHERE PID IS NULL
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
FROM @t a,@t_Level b
WHERE a.PID=b.ID
AND b.Level=@Level-1
END
--顯示結果
SELECT a.*
FROM @t a,@t_Level b
WHERE a.ID=b.ID
ORDER BY b.Sort
/*--結果
⑻ 誰能用sql語句(2008)幫我實現這種查找排序,關於產品BOM單的
查詢出來的結果應該是:
層次 內部序號 編號
1 1 1
2 1 1.1
3 1 1.1.1
4 1 1.1.1.1
2 2 1.2
1 1 2 //你應該寫錯了,內部序號應該是2
2 1 2.1
3 1 2.1.1
3 2 2.1.1 //你應該寫錯了,編號應該是2.1.2
1 1 3 //你應該寫錯了,內部序號應該是3
2 1 3.1
從上面的規律看來
1、整個結果的排序是按「編號」來排列的
2、層次:「編號」的小數點個數+1,也就是child_item的小數點個數加1,你自己想個辦法吧,這個在保存proct_bom 表時記錄到表中就更好了
網路到個好辦法:
len(child_item) - len( replace(child_item, '.' , '') ) + 1
3、內部序號:「編號」的最後一位,當然你也可以用parent_item 來截取child_item
select len(child_item) - len( replace(child_item, '.' , '') ) + 1
,right(child_item,1) 內部序號, child_item
from proct_bom
order by child_item
⑼ sql 樹型結構查詢
假如 Code 欄位 為 字元類型 則 select * from tb order by code即可如果 Code欄位 為 整型 則select Cast(Code as varchar) as Code,[Name] from tb order by code