Duplicate values can be found using this
SELECT email, COUNT(email)
FROM users
GROUP BY email
HAVING COUNT(email) > 1
for the data
ID NAME EMAIL
1 John asd#example.com
2 Sam asd#example.com
3 Tom asd#example.com
4 Bob bob#example.com
5 mob bob#example.com
But the table I need is
NAME EMAIL
John, Sam, Tom asd#example.com
Bob, mob bob#example.com
You need the GROUP_CONCAT() function, which IIRC works in both MySql and Sqlite, but handles NULL values different between the two (I may be wrong on that last part).
SELECT group_concat(name) as Name, email
FROM users
GROUP BY email
HAVING COUNT(email) > 1
Related
Table stucture
name
surname
phone
email
status
Jonh
Red
123
johnmail
5
Mary
Blue
333
marymail
0
Helen
Green
234
marymail
0
Jonh
Red
123
johnmail
0
How can I compare these rows with name, surname, email and phone, and select (display) only Jonh Red because that row exist two time and one row have status 5?
Thanks in advance.
You could try an aggregation approach here:
SELECT name, surname, phone, email
FROM yourTable
GROUP BY name, surname, phone, email
HAVING COUNT(*) > 1 AND SUM(status = 5) > 0;
This would return every tuple (sans status values) which both appear in duplicate and also have a status value of 5 in at least one of the duplicates.
I want to find table entry that appears more than once with certain value
Name | ID
==================
Peter 1
James 2
Peter 2
James 2
I want to select entries where ID is both 1 and 2, in this example only Peter appears twice with value 1 and 2 while James appear twice but ID of James is not 1 and 2
Is there operator for this kind of query?
You can do:
select name
from t
where id in (1, 2)
group by name
having count(distinct id) = 2;
ID NAME Email
1 John john#example.com
2 Mathew mathew#example.com
3 John jon#example.com
4 Johnson johns#example.com
5 Peter pete#example.com
How can I create a query that will return
1 John john#example.com
3 John jon#example.com
4 Johnson johns#example.com
Ansers like that for Find rows that have the same value on a column in MySQL returns rows with same value. Here I need similar values
Get all the records with similar email first and then only print those records that match the inner condition.
select * from table_name where email in (
select email from table_name
group by email
having count(*)>1
)
Tested and working fine.
I have a large table with data that is not unique but needs to be. This table is a result of multiple union selects so is not an actual table. I cannot make it an actual table for other reasons.
All of the UNION'd tables have an email column which will eventually be unique. The resulting records look like this:
1 ozzy#test.com Ozzy
2 test#test.com Tony
3 test#yahoo.com Steve
4 tiny#test.com
13 tony#gmail.com Tony
14 test#test.com Ozzy
15 test#yahoo.com Dave
16 tiny#test.com Tim
As you can see, some emails appear more then once with different names or non-existent names. When I add a GROUP BY email clause at the end, the results look like this:
1 ozzy#test.com Ozzy
2 test#test.com Tony
3 test#yahoo.com Steve
4 tiny#test.com
13 tony#gmail.com Tony
As you can see, email 4 does not have a name because it chose the first entry with NULL for a name. Then I tried to use GROUP_CONCAT which made the results look like this:
1 ozzy#test.com Ozzy
14 test#test.com Ozzy,Tony
15 test#yahoo.com Dave,Steve
16 tiny#test.com Tim
13 tony#gmail.com Tony
As you can see, now everyone has a name but some rows have more then one name concatenated. What I want to do is GROUP BY email and choose the first NOT NULL entry of each column for each row to theoretically look like so:
1 ozzy#test.com Ozzy
2 test#test.com Tony
3 test#yahoo.com Steve
4 tiny#test.com Tim
13 tony#gmail.com Tony
I have tried using COALESCE but it doesn't work as intended. My current query looks like so:
SELECT
id,
email,
`name`
FROM
(
SELECT
email,
`name`
FROM
multiple_tables_and_unions
) AS emails
GROUP BY email
I have removed the code from the temporary table as it contains many tables but all select the email and name column. Essentially I need a function like GROUP_COALESCE but unfortunately it does not exist. What are my options?
Try using MAX, like this:
SELECT
email,
MAX(`name`)
FROM
(
SELECT
email,
`name`
FROM
multiple_tables_and_unions
) AS emails
GROUP BY email
To select the first non null value, GROUP_CONCAT can be handy here. Below is the example:
SELECT
email,
SUBSTRING_INDEX(GROUP_CONCAT(`name`), ',',1)
FROM
(
SELECT
email,
`name`
FROM
multiple_tables_and_unions
) AS emails
GROUP BY email;
Despite a good deal of searching I haven't come up with the correct method of listing all columns for rows that have the same content in a particular column (let's say the column 'Name').
So if my table, called USERS, had the following content:
ID User Name
1 Nick Nick
2 NickP Nick
3 NickC Nick
4 John John
5 Brian Brian
The SELECT statement should return:
ID User Name
1 Nick Nick
2 NickP Nick
3 NickC Nick
As these are all the rows that contain the same content in column 'Name'. How would I write this?
How about this?
SELECT * FROM Users WHERE Name IN(SELECT Name FROM Users GROUP BY Name HAVING COUNT(1) > 1)