How take record with last date in MySQL?

how to get latest record in sql by datetime
laravel get latest record by date
sql select last record by id
how to get latest record using jpa
how to fetch latest record from database
mysql get last updated record
sql select most recent record by date
mysql select row with max date

In MySQL database I have table with such structure:

| OBJECT_ID | OBJECT_NAME | TIME_KEY            | OBJECT_CODE |
|-------------------------------------------------------------|
| 66        | Kemir       | 2019-01-01 12:00:00 | XVG         |
| 66        | Kemir       | 2019-01-01 13:00:00 | GTG         |
| 88        | Damir       | 2019-01-01 12:00:00 | NGD         |
| 99        | Vivle       | 2019-01-01 12:00:00 | FGT         |
| 99        | Vivle       | 2019-01-01 12:00:00 | HFD         |
| 99        | Vivle       | 2019-01-01 14:00:00 | KJD         |

In final result I need to take all data with last date as below:

| OBJECT_ID | OBJECT_NAME | TIME_KEY            | OBJECT_CODE |
|-------------------------------------------------------------|
| 66        | Kemir       | 2019-01-01 13:00:00 | GTG         |
| 88        | Damir       | 2019-01-01 12:00:00 | NGD         |
| 99        | Vivle       | 2019-01-01 14:00:00 | KJD         |

I use next SQL statement:

SELECT *
FROM TABLE_MAIN
GROUP BY OBJECT_ID DESC;

It return first date of the records:

| OBJECT_ID | OBJECT_NAME | TIME_KEY            | OBJECT_CODE |
|-------------------------------------------------------------|
| 66        | Kemir       | 2019-01-01 12:00:00 | XVG         |
| 88        | Damir       | 2019-01-01 12:00:00 | NGD         |
| 99        | Vivle       | 2019-01-01 12:00:00 | FGT         |

How to fix this problem?

Try following query:

Select *from
table1 t1
where 
   (object_id,time_key) in 
           (select object_id, max(time_key)
            from table1
            group by object_id)
 ;

MySQL get last date records from multiple, You can use GROUP BY , but it's important to understand how it works. When you use GROUP BY a group of rows is collapsed into one and� MemSQL is the cloud-native, operational database built for speed and scale. One database for every workload & infrastructure

You want to filter the rows, so use WHERE. Here is one method:

SELECT m.*
FROM TABLE_MAIN m
WHERE m.time_key = (SELECT MAX(m2.time_key)
                    FROM TABLE_MAIN m2
                    WHERE m2.object_id = m.object_id
                   );

Select row with most recent date per user, I know that last group by shouldn't be there, but without it, it returns the most recent time, but This also works in a case where there are two different records with the same else - basically any criteria that is guaranteed to be unique would make the job here. If your on MySQL 8.0 or higher you can use Window functions:. Table 3: The result of the recent record using ORDER BY LIMIT 1. Example #2: MySQL latest record #2.1 MySQL most recent date. Let’s try to get the most recent date of records in the transaction table by using the MAX() function as shown in the query below: SELECT MAX(trans_date) AS latest_date_trans FROM transaction;

You could use a correlated subquery

select t1.* form TABLE_MAIN t1
  where t1.TIME_KEY= (
                select max(TIME_KEY) 
         from TABLE_MAIN t2 where t1.OBJECT_ID=t2.OBJECT_ID
        and   t1.OBJECT_NAME=t2.OBJECT_NAME
      )

How can i get the latest record in database based on datetime?, I have a table with value as shown below id branch_id qty datetime --- --------- ---- ----------- 1 1 2 2017-07-29 I will select the latest record according to branch in where clause. Hi, using SQL (MySQL) you can do it like this: Last Date Record MySQL Fetch Last WEEK Record . Using the below MySQL query for fetching the last week records from the MySQL database table. SELECT name, created_at FROM employees WHERE YEARWEEK(`created_at`, 1) = YEARWEEK( CURDATE() - INTERVAL 1 WEEK, 1) Fetch Last WEEK Record Get Last 7 Day Record. Using the below MySQL query for fetching the last 7 days records from the mysql database table.

Finding the Oldest/Youngest Records Within a Group, How to find the oldest or most recent record associated with a particular foreign key. And let's say you need to display a list of users next to the date of their A small one if you are displaying the latest orders to the admins of� SELECT last entry without using LIMIT in MySQL? Get the second last row of a table in MySQL? Get the last record from a table in MySQL database with Java? How to get the second last record from a table in MySQL? MySQL query to get count of each fileid entry in a table with Id and FileIDs? Get last second of a date in MySQL? Retrieve the last entry in the TreeMap in Java; MySQL query to get the last created table name (most recent)?

SQL MAX() on date value, SQL MAX() on date value: Here we have discussed the usage of SQL NET Database SQL(2003 standard of ANSI) MySQL PostgreSQL Last update on February 26 2020 08:07:42 (UTC/GMT +8 hours) To get the maximum ' ord_date' from the 'orders' table, the following SQL statement can be used : Getting last row for each day in MySQL. Ask Question Asked 7 years, 7 months ago. Active 7 years, 7 months ago. (dates) for each day and its relevant records.

MySQL LAST_DAY() function, MySQL LAST_DAY() returns the last day of the corresponding month for a date If the date or datetime value is invalid, the function returns NULL. foreach($dbh ->query('SELECT LAST_DAY("2009-05-18")') as $row) { echo� Records of today or records of last X seconds by using curdate() or unix_timestamp Records of last one month from today by using date field Records of present week days by using dayofweek function

Comments
  • SELECT id, MIN(whatever), MAX(whatever), another_column FROM your_table GROUP BY id
  • You SQL query works faster than examples of other people. Thank you!