久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

MySQL數據庫多表之間的查詢

瀏覽:116日期:2022-06-17 14:06:34

問題描述

問題解答

回答1:思路一

分兩種情況選出符合要求的company_id并union

把這些company_id的earning求和(2013-2014)

連接上company_name

好像搞的比較復雜。

with cid(id) as ( select company_id from tableB where year = 2014 and earning > 20 union select company_id from tableB where year in (2013, 2014) group by company_id having sum(earning) > 50), cid_earning(id, earning) as ( select company_id, sum(earning) from tableB where company_id in (select id from cid) and year in (2013, 2014) group by company_id)select a.company_name, c.earningfrom cid_earning c left join tableA a using(id)思路二

如果把2013和2014年的earning作為表的兩個field,SQL的邏輯會清晰很多:

withe3(id, earning) as ( select company_id, earning from tableB where year = 2013), e4(id, earning) as ( select company_id, earning from tableB where year = 2014)select a.company_name, e3.earning + e4.earning as earningfrom e3 inner join e4 using(id)left join tableA a using(id)where e4.earning > 20 or e3.earning + e4.earning > 50回答2:

好復雜哦,同問,這樣的sql怎么寫,我在想是不是可以寫個存儲過程,畢竟存儲過程處理這樣復雜的邏輯容易一點

相關文章:
主站蜘蛛池模板: 梨树县| 石渠县| 寻乌县| 白沙| 阳朔县| 霸州市| 五指山市| 浦县| 霍邱县| 芜湖市| 茶陵县| 肇庆市| 通榆县| 甘孜| 常州市| 澜沧| 东安县| 民权县| 锦屏县| 潞西市| 青川县| 雅江县| 布尔津县| 彭水| 桐梓县| 加查县| 刚察县| 石狮市| 玉田县| 德化县| 嘉定区| 澄迈县| 正阳县| 松溪县| 永和县| 延边| 德清县| 平江县| 荃湾区| 柳州市| 民丰县|