I recently found myself in a situation where I had to get data from MySQL database which had identical date-times but had text data in another column to uniquely identify a record. So I could use the combination to identify a unique record the problem was when appending a file with new records which had half of the records with the exact same date-times.
I constructed a query with some help to get the rest of the records from the last entry in the file. This is done by:
- Firstly getting data that is equal to and greater than the date-time in the text file.
- Then assign each row with a row number using MySQL variables @rownum:=@rownum+1 this is placed inside a sub query
- Then after the first sub query create a where statement where rownum is greater than the next sub query
- In the next sub query it will return the rownum the query should start from by running the same query as the first one but with a where clause identifying the the unique text.
You can use the query below for any instance where one column has exactly the same numbers which you need to order by with another uniquely identifiable column, so you know where you should start pulling from.
A clever use of SQL variables and sub queries.
Select x.column c
FROM (select @rownum:=@rownum+1 as rownum, a.column
FROM table a, (SELECT @rownum:=0) r
WHERE a.datetime >= '2010-08-02 12:53:12' ORDER BY a.date ASC) x
where x.rownum > (Select x.rownum FROM
(select @rownum:=@rownum+1 as rownum, a.text
FROM table a, (SELECT @rownum:=0) r
WHERE a.datetime >= '2010-08-02 12:53:12' ORDER BY a.date ASC) x
where x.text = 'unique-descriptive-text');