寻一个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