寻一个SQL语句 统计 另一表的数据 表1 p_cond ID 1 2 3 4 表2 p_cond_han p_cond

寻一个SQL语句 统计 另一表的数据 表1 p_cond ID 1 2 3 4 表2 p_cond_han p_cond_id 1 1 1 2 2 2 3 3 3
寻一个SQL语句 统计 另一表的数据
表1 p_cond
ID
1
2
3
4
表2 p_cond_han
p_cond_id
1
1
1
2
2
2
3
3
3
想得到如下结果
需要查询的就是查询表1的同时 统计表2里p_cond_id=表1ID的内容
ID isnum
1 3
2 3
3 3
4 0
MYSQL执行
select a.id,ifnull(b.counts,0) as isnum from p_cond as a left join (select p_cond_id,count(*) as counts from p_cond_han group by p_cond_id) as bon a.id=b.p_cond_id
报错
[SQL] select a.id,ifnull(b.counts,0) as isnum from p_cond as a left join (select p_cond_id,count(*) as counts from p_cond_han group by p_cond_id) as bon a.id=b.p_cond_id
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a.id=b.p_cond_id' at line 1
liubo994 1年前 已收到1个回答 举报

哈叽咕叽哈 幼苗

共回答了18个问题采纳率:100% 举报

select a.id,isnull(b.counts,0) as isnum
from
p_cond as a
left join
(select p_cond_id,count(*) as counts from p_cond_han group by p_cond_id) as b
on a.id=b.p_cond_id

sqlserver用上边写法
mysql把isnull换成ifnull
oracle把isnull换成nvl

1年前 追问

2

liubo994 举报

MYSQL执行 select a.id,ifnull(b.counts,0) as isnum from p_cond as a left join (select p_cond_id,count(*) as counts from p_cond_han group by p_cond_id) as bon a.id=b.p_cond_id

举报 哈叽咕叽哈

晕 bon a.id=b.p_cond_id 这个地方 b on a.id=b.p_cond_id b和on之间有空格,你看看吧,百度复制的时候不会自动换行
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.026 s. - webmaster@yulucn.com