Reputation: 853
I need a mysql query to extract two users who who live on same adress from a table, in which I have these fields(id, username, date_of_birth, address)
Upvotes: 0
Views: 316
Reputation: 173652
Since my comment helped, here's the same in an answer :)
SELECT u.* FROM (
SELECT address
FROM users
GROUP BY address
HAVING COUNT(*) > 1
) addresses
INNER JOIN users AS u USING (address);
Or, if you want address with comma separated user names:
SELECT address, GROUP_CONCAT(username)
FROM users
GROUP BY address
HAVING COUNT(*) > 1;
Upvotes: 1
Reputation: 2218
You can try this to get address duplicates
SELECT * FROM `Table` AS t1 WHERE EXISTS (SELECT * FROM `Table` AS t2 WHERE t1.address = t2.address AND t1.id != t2.id)
Upvotes: 0