select latest duplicate record from table MySQL

ellabells

This is my sample tables:

Table1:

ID        Runnum    Thickness          Date

1          2           1.23             1/8/2015

2          2           2.32             1/9/2015

3          3           2.21             1/9/2015

4          4           3.12             1/8/2015

Table2 :

ID        Runnum    Insitu             

1          2           1.23             

2          3           2.32             

3          4           2.21             

4          5           3.12          

Result i want:

Runnum Insitu Runnum Thickness Date

 2       1.23      2           2.32             1/9/2015

 3       2.32      3           2.21             1/9/2015

 4       2.21      4           3.12             1/8/2015

I want to join table1 and table2 and select the duplicate with the latest records, this is my query so far.

SELECT a.Runnum, a.insitu, b.Runnum, b.thickness
FROM test.dummy_ease a
INNER JOIN (SELECT runnum, thickness, times FROM test.dummy_xrf group by runnum) AS b
ON a.runnum = b.runnum

The values returned from that query are almost right except for the duplicate value it is not returning the latest one.

M Khalid Junaid

I assume ID from table1 is set to auto_increment and the recent one is also based in this criteria then you have to self join table1 to the latest data

select a.Runnum,
b.insitu,
b.Runnum Runnumb, 
a.Thickness
from Table1 a
join (select max(ID) ID ,Runnum
      from Table1 
     group by Runnum) a1
using(ID,Runnum)
join Table2 b ON a.Runnum = b.Runnum 

DEMO

If recent one depends on recent date criteria then in inner query you have to select max of your date column but from your sample data it looks like you have stored date as varchar type which will not be treated as date object you have to first convert it in to date object using str_to_date then calculate max of date and join your table1 again query will be something like Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL latest record from table

From Dev

MySQL SELECT latest record from a subquery with UNION

From Dev

MYSQL: select latest record only (on left join table)

From Dev

how to select latest record from table by using Group by statements

From Dev

how to select latest record from table by using Group by statements

From Dev

Select All Fields From Only Latest Created Record of dataset in MySQL?

From Dev

Remove Duplicate record from Mysql Table using Group By

From Dev

Select 10 latest comments with unique author from MySQL table

From Dev

mySQL Query - select join if latest record in join table contains a specific value

From Dev

MySQL select and sum from multiple table without duplicate it

From Dev

pdo select latest from table

From Dev

Select latest history record from db

From Dev

Find latest record with distinct key in table with duplicate keys

From Dev

Retrieving the latest record from a Table (Oracle SQLPlus)

From Dev

MaraiDB / MySQL Insert Into Table From Select Query But On Duplicate Key Use Data From Select Query

From Dev

MySQL Select Latest Date - Single Table

From Dev

Select record from table with not in other table

From Dev

Select record from table with not in other table

From Dev

Select from table if other table points to record

From Dev

Mysql query select every user per date latest record

From Dev

MySQL: how to select record with latest date before a certain date

From Dev

Select latest from joined table excluding duplicates

From Dev

Select second instance of record ID in mysql table

From Dev

Mysql can't select the record in the table

From Dev

Select second instance of record ID in mysql table

From Dev

MYSQL: sum of latest record

From Dev

MYSQL: sum of latest record

From Dev

Select once for duplicate record

From Dev

Mysql - Order in group by , select latest from group

Related Related

  1. 1

    MySQL latest record from table

  2. 2

    MySQL SELECT latest record from a subquery with UNION

  3. 3

    MYSQL: select latest record only (on left join table)

  4. 4

    how to select latest record from table by using Group by statements

  5. 5

    how to select latest record from table by using Group by statements

  6. 6

    Select All Fields From Only Latest Created Record of dataset in MySQL?

  7. 7

    Remove Duplicate record from Mysql Table using Group By

  8. 8

    Select 10 latest comments with unique author from MySQL table

  9. 9

    mySQL Query - select join if latest record in join table contains a specific value

  10. 10

    MySQL select and sum from multiple table without duplicate it

  11. 11

    pdo select latest from table

  12. 12

    Select latest history record from db

  13. 13

    Find latest record with distinct key in table with duplicate keys

  14. 14

    Retrieving the latest record from a Table (Oracle SQLPlus)

  15. 15

    MaraiDB / MySQL Insert Into Table From Select Query But On Duplicate Key Use Data From Select Query

  16. 16

    MySQL Select Latest Date - Single Table

  17. 17

    Select record from table with not in other table

  18. 18

    Select record from table with not in other table

  19. 19

    Select from table if other table points to record

  20. 20

    Mysql query select every user per date latest record

  21. 21

    MySQL: how to select record with latest date before a certain date

  22. 22

    Select latest from joined table excluding duplicates

  23. 23

    Select second instance of record ID in mysql table

  24. 24

    Mysql can't select the record in the table

  25. 25

    Select second instance of record ID in mysql table

  26. 26

    MYSQL: sum of latest record

  27. 27

    MYSQL: sum of latest record

  28. 28

    Select once for duplicate record

  29. 29

    Mysql - Order in group by , select latest from group

HotTag

Archive