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

sqlsever層次查詢

發布時間: 2022-04-18 05:55:54

sqlserver group by 多欄位多條件查詢

分組查詢中:select後的欄位必須是group by中包含的欄位如下:
select userinfo.班級,count(userinfo.sex) from userinfo group by 班級,userinfo.sex;
語句的含義為:按照班級分組,統計每個班級的男、女總數

Ⅱ 多層次的人員層級架構,下屬層級是不確定的,在sqlserver2005中怎麼窮盡找到其所有下屬呢

這個問題太寬泛,具體的還的看你的組織機構表的設計
目前常見的解決辦法有如下方式
1、在表設計時候加入冗餘欄位,維護層級關系 ,如1.2.3.4,通過模糊查詢即可查詢出全部的下級 like '1.%'
2、通過遞歸的方式,2005 開始支持cte,
範例如下:
with FullPathBuilder as
(
select Name
,Id from orgTable b
where b.ParentId is null
union all
select b.Name ,b.Id
from orgTable b join FullPathBuilder on b.parentId = FullPathBuilder.Id
)
select * from FullPathBuilder

Ⅲ sqlserver查詢樹形結構的所有子節點

用標准sql的with實現遞歸查詢(sql2005以上肯定支持,sql2000不清楚是否支持):

with subqry(id,name,pid) as (
select id,name,pid from test1 where id = 5
union all
select test1.id,test1.name,test1.pid from test1,subqry
where test1.pid = subqry.id
)
select * from subqry;

Ⅳ 如何做SqlServer 數據查詢優化!

一、建立索引
二、建立存儲過程
三、只查詢您所需要的數據,不要把所有數據都查詢出來,防止數據冗餘。
四、對於大量及海量數據一般還要建立分區

Ⅳ SQL如何在查詢結果里再次查詢

做法:可以使用括弧「(select查詢子句)"套嵌一個查詢結果。語法格式:select columnlist... from (select子句) table_name where 。注意:」)「 後面需要給查詢結果指定一個名稱table_name,名稱不要與其他列名稱相同,增加SQL語句的可讀性。

拓展:

1、SQL語言,是結構化查詢語言(StructuredQueryLanguage)的簡稱。SQL語言是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統;同時也是資料庫腳本文件的擴展名。

2、SQL語言是高級的非過程化編程語言,允許用戶在高層數據結構上工作。它不要求用戶指定對數據的存放方法,也不需要用戶了解具體的數據存放方式,所以具有完全不同底層結構的不同資料庫系統可以使用相同的結構化查詢語言作為數據輸入與管理的介面。SQL語言語句可以嵌套,這使他具有極大的靈活性和強大的功能。

3、結構化查詢語言SQL(STRUCTURED QUERY LANGUAGE)是最重要的關系資料庫操作語言,並且它的影響已經超出資料庫領域,得到其他領域的重視和採用,如人工智慧領域的數據檢索,第四代軟體開發工具中嵌入SQL的語言等。

Ⅵ MS SQLServer 多級父子關系數據查詢

加多一列層級碼,會簡單很多比如亞洲用『01』 中國用『0101』,北京用『010101『,海定用『01010101』,東城用』01010102『
這樣語句可以
select id as col1,(select name from 表 where code = SubString(a.Code, 1,2)) as col2,
(select name from 表 where code = SubString(a.Code, 3,2)) as col3,
(select name from 表 where code = SubString(a.Code, 5,2)) as col4,
(select name from 表 where code = SubString(a.Code, 7,2)) as col5
from 表 a where Type = 'Distric'
不然
(select d.name from 表 b, 表 c, 表 d where a.parentid = b.id and b.parentid = c.id
and c.parentid = d.id) as col2,...
寫起來比較麻煩

Ⅶ sqlserver 遞歸查詢

CREATE TABLE #tb1(stuId INT,stuName VARCHAR(30),teaId INT);
INSERT INTO #tb1 (stuId,stuName,teaId)
VALUES(1,'zhou',0),(2,'kong',0),(3,'hong',2),(4,'zhang',1),(5,'liu',4),
(6,'zhao',5),(7,'zheng',6),(8,'wei',7)
;WITH cte AS (
SELECT t.stuId,t.stuName,t.teaId FROM #tb1 AS t
WHERE t.stuId=8
UNION ALL
SELECT t.stuId,t.stuName,t.teaId FROM cte AS c
JOIN #tb1 AS t ON c.teaId=t.stuId
)
SELECT * FROM cte

Ⅷ 關於sqlserver遞歸查詢

你的意思沒特別看懂,但是在ORACLE裡面的遞歸語法是:
select * from tab ...start with ... connect by col_parent...
你可以查一下start with ,connect 的語法

Ⅸ 有層級的,怎麼用sql查詢返回結果

樹形結構的查詢。
你需要說明一下, 你的資料庫是什麼資料庫。

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

DB2 與 MySQL 使用 CTE 遞歸處理來實現。

Ⅹ sql server 中如何實現查找下級分類以及下下級

在文件 includes/lib_goods.php 最後加上

//***調用商品分類指定分類下級分類
functionget_parent_id_tree($parent_id)
{
$three_c_arr=array();
$sql='SELECTcount(*)FROM'.$GLOBALS['ecs']->table('category')."WHEREparent_id='$parent_id'ANDis_show=1";
if($GLOBALS['db']->getOne($sql))
{
$child_sql='SELECTcat_id,cat_name,parent_id,is_show'.
'FROM'.$GLOBALS['ecs']->table('category').
"WHEREparent_id='$parent_id'ANDis_show=1ORDERBYsort_orderASC,cat_idASC";
$res=$GLOBALS['db']->getAll($child_sql);
foreach($resAS$row)
{
if($row['is_show'])
$three_c_arr[$row['cat_id']]['id']=$row['cat_id'];
$three_c_arr[$row['cat_id']]['name']=$row['cat_name'];
$three_c_arr[$row['cat_id']]['url']=build_uri('category',array('cid'=>$row['cat_id']),$row['cat_name']);
}
}
return$three_c_arr;
}

聲明後用$smarty調用,就是在index.php中加上下面一句:
$smarty->assign('get_parent_id16_tree',get_parent_id_tree(16));//調用父級分類6的下級分類
最後就可以在index.dwt模板文件里開始調用了
<!--{foreachfrom=$get_parent_id16_treeitem=list}-->
<ahref="http://chenlihong89791781.blog.163.com/{$list.url}"target="_blank">{$list.name|truncate:15:true}</a>
|<!--{/foreach}--></div>