当前位置:网站首页>mysql duplicate data group multiple latest records

mysql duplicate data group multiple latest records

2022-08-09 21:36:00 TabKey9

Requirements: MySQL grouping, sorting in reverse order, each grouping gets a newest record

Problem: The condition of the time flashback does not take effect

Reason: Because in mysql5.7, the sorting of sub-queries has become invalid. It may be because most of the sub-queries are used as a result for the main query, so the sub-queries do not need sorting.This is actually related to the version of the database. The version after mysql 5.7 has optimized subqueries.It optimizes the sql statement.The conditions behind the group by are all done away with.

Solution: You can add a reverse order limit of 10000 to the "subquery", and group them in the "main query", so that the reverse order will take effect, limit the subquery ordering, and set the limit as large as possible.some.

Example:
SELECT * FROM(
SELECT
tms.id supplierId,
tms.name supplierName,
tms.contact_name contactName,
tms.contact_phone contactPhone,
> til.material_id materialId,
til.recent_quotation recentQuotation,
til.quote_time
FROM
tenant_inquiry_library til
LEFT JOIN tenant_material_supplier tms
ON til.supplier_id = tms.id
WHERE
til.material_id = 1377127968075161602
ORDER BY til.quote_time DESC LIMIT 10000
) a GROUP BY a.supplierId

Rendering:


Other detailed articles related to this issue:


1. The sql group is sorted by time, and the time will expire
Copyright statement: This article is an original article by CSDN blogger [Growing Up in Bald], and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.


2. MySQL group query for the latest piece of data in each group (popularEasy to understand)
Copyright statement: This article is an original article by CSDN blogger "C Luo Lai Er" and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.


原网站

版权声明
本文为[TabKey9]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091824198813.html