how to filter repeated rows based on date in Mysql?

最后都变了- 提交于 2019-12-25 04:15:58

问题


I have two tables

table 1 name : depot_tracking

     -------------------------------------------------
    depot_tracking_id | cont_details_id |    date    |
    -------------------------------------------------
           1          |       4         | 05-02-2015 |
           2          |       1         | 25-03-2015 |
           3          |       3         | 05-04-2015 |
           4          |       2         | 15-02-2015 |
           5          |       3         | 05-05-2015 |
           6          |       2         | 23-03-2015 |
           7          |       1         | 15-05-2015 |
    --------------------------------------------------

second table name : cont_details

      -----------------------------------
          cont_no      | cont_details_id | 
       -----------------------------------
           aa          |       1         |
           bb          |       2         |
           cc          |       3         |
           dd          |       4         | 
           ee          |       5         |
           ff          |       6         |
           gg          |       7         |
    --------------------------------------

now i want to merge this two tables and filter repeated rows and one more most important thing is i have to filter the rows which has OLDER dates. This is what exactly i am expecting output

     ---------------------------------------------------------
    depot_tracking_id | cont_details_id |    date    | cont_no
    ----------------------------------------------------------
           1          |       4         | 05-02-2015 |  dd
           5          |       3         | 05-05-2015 |  cc
           6          |       2         | 23-03-2015 |  bb
           7          |       1         | 15-05-2015 |  aa
    ---------------------------------------------------------

please some one help me to filter repeated rows ( filter the rows which has old date rows) thanks

ORDER BY (str_to_date(date,'%d-%m-%Y')) DESC LIMIT 1 

by using this code i can change date column (DD-MM-YYYY) to mysql format date

but dont dont know how to filter repeated rows based on dates


回答1:


Because all of your field values can be reliably determined by using aggregate functions, you only need a simple aggregation query.

select max(d.depot_tracking_id), c.cont_details_id, max(d.date), c.cont_no
  from depot_tracking d
    inner join cont_details c
      on c.cont_details_id = d.cont_details_id
  group by c.cont_details_id

In order for this to work, your date fields have to be an actual date type, not a string type. or if they ARE a string type, they need to be 'yyyy-mm-dd' format, not 'dd-mm-yyyy'.

demo here

update

Since it seems you are stuck using the broken date format - this query will work:

select max(d.depot_tracking_id), c.cont_details_id, max(str_to_date(d.date,'%d-%m-%Y')), c.cont_no
  from depot_tracking d
    inner join cont_details c
      on c.cont_details_id = d.cont_details_id
  group by c.cont_details_id

demo




回答2:


http://sqlfiddle.com/#!9/113ed/1

SELECT 
d.depot_tracking_id,
c.cont_details_id,
d.`date`,
c.cont_no
FROM depot_tracking d
LEFT JOIN depot_tracking d1
ON d.cont_details_id = d1.cont_details_id
  AND d.`date` < d1.`date`
LEFT JOIN cont_details c
ON c.cont_details_id = d.cont_details_id
WHERE d1.`date` IS NULL



回答3:


First and foremost, MySQL does not store dates in the 'dd-mm-yyyy' format but in 'yyyy-mm-dd'. If you are using a date type, the following solution will work. If it's not, you'll have to do some string to date formatting. I highly recommend you change to store dates the proper way though.

Start by selecting the newest date for each cont_details_id in the depot_tracking table like this:

SELECT cont_details_id, MAX(date) AS latest
FROM depot_tracking
GROUP BY cont_details_id;

Once you have that, you can join it back to your original tables using date/cont_details_id to get the rows you want:

SELECT d.depot_tracking_id, d.cont_details_id, d.date, c.cont_no
FROM depot_tracking d
JOIN(
  SELECT cont_details_id, MAX(date) AS latestDate
  FROM depot_tracking
  GROUP BY cont_details_id) tmp ON tmp.cont_details_id = d.cont_details_id AND tmp.latestDate = d.date
JOIN cont_details c ON c.cont_details_id = tmp.cont_details_id;

Here is an SQL Fiddle example that shows both queries separately so you can see how they come together.



来源:https://stackoverflow.com/questions/30105515/how-to-filter-repeated-rows-based-on-date-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!