我有两张桌子:
r_rec
必要的内容是主id
和
r_fibu
其中i_rec
是rec
的外键,sh
填充h
或s
。
我想查询以查找来自id
的{{1}} rec
,其中r_fibu
有多个数据集sh
= h但我无法弄清楚需要查询。
这就是我现在所拥有的:
SELECT r.id
FROM `r_rec` AS r
INNER JOIN r_fibu AS f ON r.id = f.i_rec
WHERE f.sh = 'h'
HAVING COUNT( * ) >=2
答案 0 :(得分:1)
要获得每count
r.id
,您需要group by
SELECT r.id,count(*) as tot
FROM `r_rec` AS r
INNER JOIN r_fibu AS f ON r.id = f.i_rec
WHERE f.sh = 'h'
group by r.id
having tot >=2