I have a table structured this way:
id, event_id, event_date
1, 12345, 2016-02-03
2, 12345, 2016-02-03
3, 12345, 2016-02-06
4, 12345, 2016-02-06
5, 12345, 2016-02-06
6, 12342, 2016-02-03
7, 12342, 2016-02-03
8, 12342, 2016-02-03
Notice that the id is unique, but not the event_id and the event_date (which is quite different from the claimed duplicate).
Is there a way to shorten this SQL code:
First Step: get event id
SELECT DISTINCT event_id FROM events
Second Step: get event_date using id
SELECT DISTINCT event_date
FROM events
WHERE event_id='<event_id>'
Third Step: get event_id, event_date, and event_count
SELECT id, event_id, event_date, COUNT(*) as event_count
FROM events
WHERE event_id ='<event_id>'
AND event_date='<event_date>'
Forth Step: delete record if event_id and event_date are the same.
DELETE FROM events
WHERE event_id ='<event_id>'
AND event_date='<event_date>'
LIMIT <event_count-1>;
I put all of them in functions, and it pulls up all the data slow from MySQL database because of multiple queries and huge data, but is there a way to query once?