關於sql聯合查詢

2021-04-02 19:50:11 字數 677 閱讀 6758

【t_productor】

cityid  產品名  產量

1     冰箱     100

1     熱水器   200

2     電視機   50

2     洗衣機   100

【t_city】

id    名稱

1     北京

2     南京

用一條sql語句求出一下結果

城市名    產量

北京     300

南京     150

寫了3個方法:

select t_city.名稱,a.產量 from t_city join (select sum(產量) as 產量,cityid from t_productor group by cityid) as a on a.cityid=t_city.id

select t_city.名稱,產量=sum(t_productor.產量) from t_productor join t_city on t_productor.cityid=t_city.id group by t_city.名稱

select t_city.名稱,產量=sum(t_productor.產量) from t_productor,t_city where t_productor.cityid=t_city.id group by t_city.名稱

sql聯合查詢

sql查詢 多表聯合查詢 將具有相同的欄位的查詢結果合併為一個表 關鍵字 union 例項 查詢subs表 select subs id,prefix,acc nbr,cust id,user id,acct id,price plan id,area id,update date from sub...

SQL 聯合查詢

use xsgl go select from student select from cause select from exam 聯合查詢 join on 預設為inner,如果有right or left 那麼就指的是外聯,outer 可以不寫 1.最長見為內聯 table1 inner jo...

SQL 聯合查詢

a表 aaa bbb ccc 1a 1b 1c 2a 2b 2c 3a 3b 3c b表 aaa bbb ddd 1a 1b 1d 4a 4b 4d 1 union union all all 表示將查詢的所有結果都合併到結果集中,若不加all會將重複的行只保留一行 sql view plain c...

sql 聯合查詢

概述 聯合查詢效率較高,舉例子來說明聯合查詢 內聯inner join 左聯left outer join 右聯right outer join 全聯full outer join 的好處及用法。聯合查詢效率較高,以下例子來說明聯合查詢 內聯 左聯 右聯 全聯 的好處 t1表結構 使用者名稱,密碼 ...

sql 聯合查詢

1,內聯和外聯查詢的區別 內聯join inner join join 把兩個表連線在一起,返回兩個表中相匹配的記錄,是兩表的交集。左外聯left join left outer join left join,左側表所有的記錄都返回,右側匹配的記錄返回,沒有匹配的返回null 右外聯right jo...