我需要一个 mysql 查询来从一个表中提取两个住在同一地址的用户,其中我有这些字段(id、username、date_of_birth、address)
问问题
257 次
2 回答
1
由于我的评论有帮助,因此答案相同:)
SELECT u.* FROM (
SELECT address
FROM users
GROUP BY address
HAVING COUNT(*) > 1
) addresses
INNER JOIN users AS u USING (address);
或者,如果您想要使用逗号分隔的用户名的地址:
SELECT address, GROUP_CONCAT(username)
FROM users
GROUP BY address
HAVING COUNT(*) > 1;
于 2012-06-13T13:14:34.570 回答
0
你可以试试这个来获取地址重复
SELECT * FROM `Table` AS t1 WHERE EXISTS (SELECT * FROM `Table` AS t2 WHERE t1.address = t2.address AND t1.id != t2.id)
于 2012-06-13T12:47:43.320 回答