當前位置:首頁 » 編程語言 » sql三張表查詢

sql三張表查詢

發布時間: 2024-10-26 17:05:23

A. 三表聯查的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

5.至於你說的insert報錯的問題,我想可能是因為學生ID和課程ID這兩個外鍵有重復的值,
你可以檢查下,實在不行刪除外鍵,插入數據,在這里外鍵對你最後要的結果影響不大。

純手工打造~有幫助記得給分哈

B. 求三表聯合查詢的SQL查詢語句

1、SQL語句:select u.*,r.*,r.id rid

from user u left join sys_user_role sur on u.id = sur.useridleft join sys_role r on sur.roleid = r.id

圖片:(表名截圖)

  • 算了,建表語句也給你們了,你們自己測試,這樣更詳細,(程序員)多動手,比什麼都好。(這里的 界面 對寫代碼不太友好,我放博客里了,自己復制粘貼測試使用就行)

    sql語句地址:網頁鏈接

    2、SQL語句解釋:

    select a.*,b.*

    from a表 a left join b表 b on a.id = b.aid

    left join c表 c on b.cid = c.id

    注2:此語句適合a表與c表連接,b表是關系表的情況。

C. SQL資料庫的表。怎麼同時連接3個表查詢。

可以參考下面的方法:

1、select * from 表1,表2,表3 where 表1.欄位=表2.欄位 and 表1.欄位=表3.欄位

2、select * from 表1 join 表2 on 表1.欄位=表2.欄位 and join 表3 on 表1.欄位=表3.欄位

如果沒有AND,前面就需要加括弧了。

(3)sql三張表查詢擴展閱讀:

參考語句

創建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根據已有的表創建新表:

1、create table tab_new like tab_old (使用舊表創建新表)

2、create table tab_new as select col1,col2… from tab_old definition only

刪除新表

drop table tabname

熱點內容
微信猜拳演算法 發布:2024-10-26 19:23:35 瀏覽:389
android編譯第三方庫 發布:2024-10-26 19:14:20 瀏覽:614
蘋果手機怎麼用藍牙傳照片給安卓 發布:2024-10-26 18:57:40 瀏覽:614
ios8程序加密 發布:2024-10-26 18:30:27 瀏覽:532
密碼門鎖沒電了用什麼數據線 發布:2024-10-26 18:21:11 瀏覽:854
linuxloop 發布:2024-10-26 18:20:46 瀏覽:931
linuxvg查看 發布:2024-10-26 18:09:51 瀏覽:585
圖標是鑰匙的加密軟體 發布:2024-10-26 18:07:31 瀏覽:625
vip七九寶琉璃怎麼配置魂環 發布:2024-10-26 17:51:17 瀏覽:571
opencv編譯linux 發布:2024-10-26 17:49:50 瀏覽:983