① 「sqlserver」三表聯如何查詢「sql」語句
假設學生表叫student,課程表叫class,選課表叫choose
1.三層嵌套的問題
select student.name from student where student.id IN
(select choose.sid from choose where choose.cid NOT IN
(select class.id from class where class.teacher='李明'))
2.一個內連接,一個嵌套
select student.name,avg(choose.score) from
student inner join choose on student.id=choose.sid
where student.id IN
(select choose.sid from choose
where choose.score<'60'
group by choose.sid
having count(choose.sid)>=2)
gruop by student.id
3.一個聯合查詢,一個嵌套查詢
select student.name from student
where student.id IN
(select c1.sid from choose c1 where choose.cid='1'
union
select c2.sid from choose c2 where choose.cid='2'
on c1.sid=c2.sid
)
4.其實就是自連接查詢和行列交換的問題:
select student.id,
(case choose.id when '1' then choose.score end) as 1號課成績,
(case choose.id when '2' then choose.score end) as 2號課成績,
from student inner join choose on student.id=choose.sid sc1,
student inner join choose on student.id=choose.sid sc2
where sc1.id='1'
and sc2.id='2'
and sc1.score>sc2.score
② sqlserver 觸發器 多表聯查
CREATE TRIGGER [dbo].[表1_UPDATE_表2] --這里是觸發器名稱,最好一目瞭然的
ON [dbo].[表1]
FOR UPDATE,INSERT,DELETE --更新,插入,刪除時觸發(根據實際情況,可以只用一個)
AS
BEGIN
--下面是你的語句,我沒有修改,只是調整了排版和大小寫
UPDATE 表2
SET name=0,age=''
FROM 表2 INNER JOIN
(
SELECT DISTINCT ABS(id) AS billid,sale_z.ele_sordercode
FROM 表3
WHERE ele_sordercode<>''
) 表3
ON 表2.tid=表3.id LEFT JOIN
(
SELECT MAX(id) AS id, referbillid
FROM 表1
WHERE referbillid<>0
GROUP BY referbillid
) 表1 ON 表3.id=表1.id
WHERE name=1 AND refer.id IS NULL
END
③ sqlserver多表聯合查詢
select aa.a_name as 名字,count(aa.a_name) as 數量 from a aa,b bb where aa.a_id = bb.a_id group by aa.a_name
④ sqlserver里如何實現聯合主鍵及如下自增功能
不會,不過有些思路
提取你要輸入的A的值,判斷 select count(*) from TABLE where A=你輸入的值。
case1:如果查到的值是0,則B為1;
case2:如果查到的值不是0,查詢 select max(B)+1 from TABLE where A=你輸入的值 ,並將查詢到的值和A要輸入的值一起插入;
如果只是用資料庫實現的話帶參數的存儲結構應該能實現你這個要求,編程的話就更簡單了
⑤ sqlServer 2005 中內連接(inner join)與左連接(left join)的區別最好附加個實例
inner join 是內連接,left join 是左連接。
舉例:
a表,有數據 id1(1,2,3,4,5)
b表,有數據 id2(1,3,5,6,7)
select a.* from a inner join b on a.id1 = b.id2 結果,是id1(1,3,5)
select a.* from a left join b on a.id1 = b.id2 結果,是 id1(1,2,3,4,5)
⑥ 請教大神SQlSERVER向這樣多表聯合查詢怎麼寫語句
SELECT s.*,p.player_name FROM t_match_score s
LEFT JOIN t_match_player p
ON s.player_id=p.player_id
⑦ sqlserver 內聯查詢問題
應該要letf join兩次。
下面的SQL是 MYSQL的寫法,沒在MSSQL測試過,但邏輯是一樣的。
兩表:
teams(id,t1_id,t2_id)
team(id,name)
selectt1_id,t1.name,t2_id,t2.name
fromteamsasts
leftjointeamast1onts.t1_id=t1.id
leftjointeamast2onts.t2_id=t2.id
⑧ sqlserver中的關聯查詢問題
sql server 本省對語句就有自動優化功能, 第一個里邊where語句相當於join on 來操作的
這樣看來兩個的效率基本上是一樣的,你可以做兩個表試一試。但是join的寫法有助於你的編寫語法檢查,和易讀性
⑨ sqlserver 創建視圖失敗,原因:ORDER BY 子句在視圖、內聯函數、派生表、子查詢和公用表表達式中無效
sqlserver中創建視圖時,as下不能出現order by
其實你要排序的話可以對視圖進行排序嘛,沒必要在視圖創建時候就排序
--創建視圖
create view v$_emp_1
as
select * from emp
where emp.sal > (select min(sal) from emp)
--查詢視圖
select * from v$_emp_1 order by sal--這兒排序不就行了嘛~~~採納哦