WebMake view containing all last row id create view lastrecords as (select max (id) from foo where uid = a.uid group by uid) Now join your main query with this view. It will be faster. SELECT t1.* FROM tablename as t1 JOIN lastrecords as t2 ON t1.id = t2.id AND t1.uid = t2.uid; OR You can do join with last records direct in query also: SELECT t1.* WebNote that if a user has multiple records with the same "maximum" time, the query above will return more than one record. If you only want 1 record per user, use the query below: SQLFIDDLEExample SELECT t1.* FROM lms_attendance t1 WHERE t1.id = (SELECT t2.id FROM lms_attendance t2 WHERE t2.user = t1.user ORDER BY t2.id DESC LIMIT 1) …
Retrieving the last record in each group - MySQL
WebJun 8, 2024 · Sometimes you may need to get last 24 hours data or select records for last 24 hours, for reporting & analysis. Here’s how to get records from last 24 hours in MySQL. You can use this SQL query to get rows for last 24 hours in your database. WebNov 28, 2012 · 相关问题 SQL 查询获取过去 30 天之前的记录(不是过去 30 天) - SQL query to get records of before last 30 days( Not last 30 days ) PHP + Mysql - 获取过去 30 天中每一天的统计数据 - PHP + Mysql - Get statistics for each day of the last 30 days 获取最近30天的记录 - Getting last 30 days of records 最近 ... christine docherty goodwin
Get Last Record in Each MySQL Group, the efficient way and
WebApr 5, 2011 · 5 Answers Sorted by: 112 This should work for you. SELECT * FROM [tableName] WHERE id IN (SELECT MAX (id) FROM [tableName] GROUP BY code) If id is AUTO_INCREMENT, there's no need to worry about the datetime which is far more expensive to compute, as the most recent datetime will also have the highest id. WebJan 17, 2012 · mySql to get last records of duplicate entries [duplicate] Ask Question Asked 11 years, 2 months ago Modified 11 years, 2 months ago Viewed 11k times 5 This question already has answers here: Closed 11 years ago. Possible Duplicate: Retrieving the last record in each group Hi all i am having my table data as follows Web7 Answers Sorted by: 129 Use the aggregate MAX (signin) grouped by id. This will list the most recent signin for each id. SELECT id, MAX (signin) AS most_recent_signin FROM tbl GROUP BY id To get the whole single record, perform an INNER JOIN against a subquery which returns only the MAX (signin) per id. geriatric health