文章詳情頁
一個MySQL關(guān)聯(lián)查詢是否存在的問題
瀏覽:200日期:2022-06-16 10:43:29
問題描述
表C:
idcname1test表G:
idgname1GTEST表P:
idg_idc_id1G表ID,5C表IDSQL查出所有C的列表關(guān)聯(lián)P,g_id等于1的(g_id的count計數(shù))
比如 C關(guān)聯(lián)P的c_id。查出所有的C。結(jié)果:c.* 和 g_id=1的計數(shù)。
比如:當(dāng)前表P沒有g(shù)_id=1的。要求結(jié)果:
idcnamegid=11test0問題解答
回答1:select c.*, (select count(*) from p where c.id=p.c_id and p.g_id=1) from c 回答2:
select c.*, count(p.g_id) as gid from c left jion p on c.id=p.c_id and p.g_id=1;
回答3:select c.*,ifnull(num,0) from c left join (select count(*) num,c_idfrom p group by g_id) b on c.id=p.c_id ;
相關(guān)文章:
1. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. docker鏡像push報錯3. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題4. javascript - 在 model里定義的 引用表模型時,model為undefined。5. 利用IPMI遠程安裝centos報錯!6. node.js - mongodb查找子對象的名稱為某個值的對象的方法7. 運行python程序時出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯誤?8. html5和Flash對抗是什么情況?9. phpstudy8.1沒集成mysql-front10. 前端 - @media query 使用出現(xiàn)的問題?
排行榜

熱門標(biāo)簽
網(wǎng)公網(wǎng)安備