文章詳情頁
一個MySQL關聯查詢是否存在的問題
瀏覽:95日期:2022-06-16 10:43:29
問題描述
表C:
idcname1test表G:
idgname1GTEST表P:
idg_idc_id1G表ID,5C表IDSQL查出所有C的列表關聯P,g_id等于1的(g_id的count計數)
比如 C關聯P的c_id。查出所有的C。結果:c.* 和 g_id=1的計數。
比如:當前表P沒有g_id=1的。要求結果:
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 ;
相關文章:
1. [python2]local variable referenced before assignment問題2. 求救一下,用新版的phpstudy,數據庫過段時間會消失是什么情況?3. mysql - 如何在有自增id的情況下,讓其他某些字段能不重復插入4. python小白,關于函數問題5. django - Python error: [Errno 99] Cannot assign requested address6. angular.js - 百度支持_escaped_fragment_嗎?7. java - 線上應用,如果數據庫操作失敗的話應該如何處理?8. node.js - win 下 npm install 遇到了如下錯誤 會導致 無法 run dev么?9. python小白 關于類里面的方法獲取變量失敗的問題10. Python2中code.co_kwonlyargcount的等效寫法
排行榜
