Mysql查询,检查同一天出生的两个用户

时间:2012-06-13 12:37:59

标签: mysql

我需要一个mysql查询来从表中提取住在同一地址的两个用户,其中我有这些字段(id,username,date_of_birth,address)

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

你可以尝试这个来获取地址重复

SELECT * FROM `Table` AS t1 WHERE EXISTS (SELECT * FROM `Table` AS t2 WHERE t1.address = t2.address AND t1.id != t2.id)