multiple-select - mysql 多表关联查询count返回0如何写select
PHP中文网
PHP中文网 2017-04-17 13:11:54
[MySQL讨论组]

现在
表A数据
id name
0 aaa
1 bbb
2 ccc

表B数据
id detailAlpha
0 one
0 two
0 three
2 one
2 two

表C数据
id detailBeta
0 one
0 two
1 one
1 two

A.id=B.id & A.id=C.id
关联查询语句count如何写能够返回0

我想select的count数据是
id count(B.id) count(C.id)
0 3 2
1 0 2
2 2 0

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(2)
PHPz
select id1 id, bcount, ccount from
  (select A.id as id1, 
    sum(B.id is not null) as bcount 
    from A left join B using(id) group by id1) t1,
  (select A.id as id2, 
    sum(c.id is not null) as ccount 
    from A left join C using(id) group by id2) t2
where id1 = id2;

总体就是 @OhKam 的思路.

怪我咯

A表查询ID 左联接加入 临时表B和临时表C
(B表查询ID和ID统计)as temptableB
(C表查询ID和ID统计)as temptableC

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号