三表查詢 統計

2021-06-05 05:44:35 字數 923 閱讀 1771

一張訂單表

estimate_id title

一張銷售表

sell_id estimate_id no sprice snum

一張採購表

buy_id estimate_id no bprice bnum

乙個訂單對應多個銷售和採購(estimate_id),乙個銷售對應多個採購(no)

查詢的結果:

title   scount    bcount 

xx     10.00        5.00

bb                               (注:這種情況可能是只有訂單,還沒有銷售和採購)

aa    100.00     45.00

sql語句:

select e. * , s.scount, b.bcount 

from tb_estimate as e

left join (

select sum( sprice * snum ) as scount, estimate_id

from tb_sell

where deleted =0

group by estimate_id

) as s on e.estimate_id = s.estimate_id

left join (

select sum( bprice * bnum ) as bcount, estimate_id

from tb_buy

where deleted =0

group by estimate_id

) as b on b.estimate_id = e.estimate_id

where e.deleted =0

order by updated desc

交叉表查詢統計

create table t class varchar 2 calldate datetime,callcount int insert into t select 1 2005 8 8 40 union all select 1 2005 8 7 6 union all select 2 200...

查詢(三)雜湊表查詢

概念部分不做介紹,具體見嚴蔚敏 資料結構 include include 開放定址雜湊表儲存結構 typedef char keytype 定義關鍵字型別 typedef structelemtype 定義元素型別 int hashsize 雜湊表容量遞增表,乙個合適的素數序列 typedef st...

mysql 三表查詢

學生成績表sc 學生表student 課程表 use exercise0107 資料庫的名稱 第一步查詢語文,數學的課程號 select cid from course where cname 語文 select cid from course where cname 數學 第二步分別查詢每個人的語...