国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

MySQL數據庫多表之間的查詢

瀏覽:87日期: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怎么寫,我在想是不是可以寫個存儲過程,畢竟存儲過程處理這樣復雜的邏輯容易一點

相關文章:
主站蜘蛛池模板: 四会市| 洛阳市| 霍城县| 新安县| 泰兴市| 万宁市| 兴和县| 托克逊县| 南涧| 赤水市| 黔江区| 黄骅市| 寿光市| 和平县| 石门县| 达州市| 临颍县| 阿图什市| 东辽县| 神池县| 潞西市| 宝山区| 崇阳县| 深水埗区| 房产| 焦作市| 孝义市| 年辖:市辖区| 宁阳县| 德江县| 民权县| 杨浦区| 昭通市| 西藏| 商南县| 连平县| 汉源县| 通山县| 长泰县| 延津县| 浦江县|