我有一个查询问题,你能告诉我我错在哪里吗?
我有 3 张桌子:
用户:
`users` (
`id` int(11) AUTO_INCREMENT,
`first_name` varchar(16),
`last_name` varchar(16)
`email` varchar(16)
`password` varchar(32)
`phone` varchar(13)
`age` tinyint(4)
`gender` varchar(6)
)
用户眼睛:
`users_eyes` (
`user_id` int(4),
`user_eyescolor_id` tinyint(2),
UNIQUE KEY `user_id` (`user_id`,`user_eyescolor_id`)
)
users_eyestype:
`users_eyestype` (
`user_id` int(4),
`user_eyestype_id` tinyint(2),
UNIQUE KEY `user_id` (`user_id`,`user_eyestype_id`)
)
这是我的查询
SELECT
SQL_CALC_FOUND_ROWS
u.first_name,
u.last_name,
u.age,
u.gender,
u.phone,
u.id
, GROUP_CONCAT(DISTINCT ue.user_eyescolor_id SEPARATOR ' ' ) as eyes_color
, GROUP_CONCAT(DISTINCT uet.user_eyestype_id SEPARATOR ' ' ) as eyes_type
FROM users u
LEFT JOIN users_eyes ue ON u.id = ue.user_id
LEFT JOIN users_eyestype uet ON u.id = uet.user_id
WHERE
ue.user_eyescolor_id IN (1,2,3,4)
GROUP BY u.id
HAVING
COUNT(ue.user_id) = 4
结果是一个有两种眼睛的人,而不是这个有四种眼睛的人,在我加入和eyes_type之前一切都很完美。