tags:

views:

21

answers:

1

Firstly sorry if the title is confusing, difficult to explain in one line!

ok, so i'm making a script to alert me of any potential duplicate rows in my db.

SELECT events.date, events.ven_id, COUNT(*)
FROM events    
GROUP BY events.date, events.ven_id
HAVING COUNT(*) > 1 

this successfully finds duplicate row, however only returns the date and ven_id columns, id like to get an events.* into the select statement somehow but still only return duplicate rows.

is this possible?

A: 
SELECT events.date, events.ven_id, (select COUNT(*) events where date = ev.date and ven_id=ev.id) as eventcount
FROM events ev
GROUP BY events.date, events.ven_id 
where eventcount > 1
Noctris
By the way.. an easier way would be to create a unique index.. trying to handle duplicates afterwards is ALWAYS a bad idea.. this should be done when inserting.. if you put a unique index on date+ven_id, a double should never be able to be inserted..
Noctris
@Noctris - where does the 'ev' table come from?
Haroldo
- this isn't working at all?
Haroldo
the ev is an alias for "events" .. i have typed this without any syntax check since i don't have the database schema you are using but mysql does do aliases ( saying "select * from table tb" means that in the reset of your query, you can use "tb" which is an alias for "table" )
Noctris
ok.. i've done a quick query on a table i have here and translated this back to the tablenames you are giving here. Try this and see if it gives the result you want: select date,ven_id,(select count(ven_id) from events where ven_id = ev.ven_id and date = ev.date) as eventcount from events ev where (select count(ven_id) from events where ven_id = ev.ven_id and date = ev.date) > 1
Noctris