當前位置:首頁 » 數據倉庫 » 資料庫多表查詢
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

資料庫多表查詢

發布時間: 2022-01-14 00:21:21

sql資料庫 多表查詢

可以有兩種處理方法,
1、兩表先合並,後求和
select 日期,sum( 數值) as 數值 from
( select 日期, 金額 as 數值 from A表
union all
select 日期,數量 as 數值 from B表 )
group by 日期
2、先求和後合並,再求和
select 日期,sum( 數值) as 數值 from
( select 日期, sum(金額) as 數值 from A表 group by 日期
union all
select 日期,sum(數量) as 數值 from B表 group by 日期 )
group by 日期

對於「如果要實現總和的相乘、相除,或者相減得話,應該怎麼寫呢?」,你得提出明確需求,那後才能設計。

如果 是相乘、相除,或者相減,得有條件,還像上面,用日期關聯,A表 - B表,

A表求和:select 日期,sum(金額) as 數值 from A表 group by 日期
B表求和:select 日期,sum(數量) as 數值 from B表 group by 日期

在執行 A表 - B表 時,由於用日期關聯,則某一日期對應的記錄可能會產生三種情況: A表B表都有;A表有B表無;A表無B表有。

1、需要先找出所有日期,

select 日期 from A表
union
select 日期 from B表

2、對於A表中所有數據以上表中日期為依據構造所有相關日期數據,如果有日期數據,則為原數據,否則為0。

select a.日期 as 日期,
case when b.數值 is NULL then b.數值 else 0 end as 數值
from
( select 日期 from A表
union
select 日期 from B表 ) a
left join
( select 日期,sum(金額) as 數值 from A表 group by 日期 ) b
on a.日期 = b.日期

3、同樣對於B表也如此。

4、2表相減即得。

select a3.日期 as 日期, a3.數值 - b3.數值 as 數值
from
(
select a1.日期 as 日期,
case when b1.數值 is NULL then b1.數值 else 0 end as 數值
from
( select 日期 from A表
union
select 日期 from B表 ) a1
left join
( select 日期,sum(金額) as 數值 from A表 group by 日期 ) b1
on a1.日期 = b1.日期
) a3 ,
(
select a2.日期 as 日期,
case when b2.數值 is NULL then b2.數值 else 0 end as 數值
from
( select 日期 from A表
union
select 日期 from B表 ) a2
left join
( select 日期,sum(金額) as 數值 from B表 group by 日期 ) b2
on a2.日期 = b2.日期
) b3

where a3.日期 = b3.日期

當然,以上只是一種方法,還有其他方法也可以實現,盡供參考。

㈡ 在SQL中如何進行多表查詢

要想多表查詢,是有條件的。一般是幾張表結構相似或者是有一樣的ID號關聯。例如:
select * from 表1,表2,表3 這是把3張表結果全部查出來
select * from 表1 where not exists(select 0 from 表2 where
表1.id=表2.id)

㈢ sql 多表查詢,數據表如下,

1.select學號,姓名fromTable_stustujoinTable_Scsconstu.no=sc.no
joinTable_Courseconc.number=sc.numberwhere課程名='JAVA'

2.select課程名fromTable_stustujoinTable_Scsconstu.no=sc.no
joinTable_Courseconc.number=sc.numberwhere姓名='依依'

3.select學號,姓名fromTable_stustujoinTable_Scsconstu.no=sc.no
joinTable_Courseconc.number=sc.numbergroupby學號,姓名
havingcount(學號)=5

4.selecttop5*fromTable_Sc

5.selecttop5*fromTable_ScwhereNOnotin
(selecttop5NOfromTable_Scorderbychenji)orderbychenji

6.selectname,class,max(chenji)fromTable_stustu
joinTable_Scsconstu.no=sc.no
joinTable_Courseconc.number=sc.number
groupbyname,class

7.selectgetdate()

沒有測試,只是憑你的問題和圖片寫出。可能你需要修改。

㈣ Sql多表查詢,怎麼做

根據你的查詢結果要求,可以根據一下幾個步驟確定多表查詢語句的寫法:
1、要顯示所有學生信息、班級、年級等信息,則需以主表1為主記錄,其他表通過外連接的方式進行關聯;
2、LEFT JOIN 關鍵字會從左表那裡返回所有的行,即使在右表中沒有匹配的行,確定主表之後,其他關聯表使用LEFT JOIN;
3、拼接SQL語句,需要確定關聯欄位主表1與表2的關聯為主表1.studentid=表2.studentid,
主表1與表3的關聯為主表1.gradId=表3.gradId,主表1與表4的關聯為主表1.classId=表4.classId
4、具體語句為:
SELECT 表1.ID, 表2.STUDENTNAME,表3.GRADNAME,表4.CLASSNAME
FROM 表1

LEFT JOIN 表2 ON 表1.STUDENTID = 表2.STUDENTID
LEFT JOIN 表3 ON 表1.GRADID = 表3.GRADID
LEFT JOIN 表4 ON 表1.CLASSID= 表4.CLASSID

㈤ 資料庫多表查詢

我們通常使用的表之間的聯接是等值聯接;你的情況需要使用外聯接來做。
我按照你的意思分別見了table1,table2,table0(對應你的table表),並把你的數據也增加進去了。
執行下面的SQL可能會得到你想要的結果:
(selecttable1.hm,table1.no1,table2.(table1.hm=table2.hm)wheretable1.hmisnotnull)
union
(selecttable2.hm,table1.no1,table2.(table1.hm=table2.hm)wheretable2.hmisnotnull)
輸出結果如下:
希望是你想要的!
剛才沒用到table0表,如果保證hm的值必須在table0中,如要修改上面的SQL如下:
select*from(selecttable1.hmashm,table1.no1,table2.(table1.hm=table2.hm)wheretable1.hmisnotnull
union
selecttable2.hmashm,table1.no1,table2.(table1.hm=table2.hm)wheretable2.hmisnotnull)astwheret.hmin(selecthmfromtable0)
這樣,當table1,table2中的hm值不在table0表中,查詢結果就不會出現。
外聯接,包括左外聯接、右外聯接、全外聯接的確大多數的程序員都不使用。寧肯用很長的程序代碼來實現,但是用資料庫sql來實現其實效率是最快的。
建議你了解外聯接的知識。

㈥ SQL如何實現多表查詢

大體上的方法有兩種
(1)聯合查詢
select dbo.vipcard.cardno,........,dbo.viptype.typename as cardtype from dbo.vipcard inner join dbo.viptype on dbo.vipcard.cardtype=dbo.viptype.ctypeid
(2)子查詢
select dbo.vipcard.cardno,........,(select typename from viptype where dbo.vipcard.cardtype=dbo.viptype.ctypeid) as cardtype from dbo.vipcard

㈦ SQL多表查詢語句怎麼寫

SQL多表查詢語句的步驟如下:

我們需要准備的材料分別是:電腦、sql查詢器。

1、首先,打開sql查詢器,連接上相應的資料庫表,例如m1表和m2表。

㈧ sql資料庫多表查詢

1.

select
st.StudentNumber,
st.Name,
count(*)
from
studentst,
sbooksk
where
st.StudentNumber=sk.StudentNumber
andto_char(sk.BorrowingTime,'yyyy')='2008'
groupby
st.StudentNumber,
st.Name

2.

select
b.BookNumber,
b.BookName,
st.Name,
sk.BorrowingTime,
sk.ShouldReturnTime
from
studentst,
sbooksk,
bookb
where
st.StudentNumber=sk.StudentNumber
andsk.BookNumber=b.BookNumber
andmonth(sk.BorrowingTime)=month(sysdate)

3.

CreateviewyourViewNameas
select
b.BookNumber,
b.BookName,
st.Name,
sk.BorrowingTime,
sk.ShouldReturnTime
from
studentst,
sbooksk,
bookb
where
st.StudentNumber=sk.StudentNumber
andsk.BookNumber=b.BookNumber
andmonth(sk.BorrowingTime)=month(sysdate)

沒運行過可能有點問題

㈨ 資料庫 多表聯合查詢

oralce么?
select A.姓名,A.電話,wm_concat(B.卡號) 卡號 from 表A A,表B B where A.姓名=B.姓名 group by A.姓名,A.電話