表成人:
id, Adname
1 , Harry
2 , Sally
3 , Beth
4 , David
表儿童:
id, Chname , adult_id , DOB(YYYY-MM-DD)
1 , Rebecca , 1 , 5/23/1987
2 , Stanley , 3 , 9/7/2003
3 , Emma , 3 , 3/17/2000
4 , Maria , 4 , 11/8/1995
5 , Michael , 4 , 8/15/1998
6 , Jessica , 4 , 4/28/1991
查询:仅显示有 2 个或更多孩子的成人及其孩子。到目前为止,我有:
SELECT Adult.Adname, COUNT(Children.Adult_id) AS NumberOfChildren FROM (Adult
INNER JOIN Children
ON Adult.ID=Children.Adult_id)
GROUP BY Adname
HAVING COUNT (Children.Adult_id) > 2
UNION SELECT Adult.Adname, Children.Chname
FROM Adult LEFT JOIN Children ON Adult.[ID] = Children.[Adult_id]
WHERE Children.Adult_id <> NULL;
但结果仍然显示成人和儿童不到2 ??