get first record in each group using aggregate functions in group by sql in mysql
To get the first record in each group using aggregate functions in GROUP BY SQL in MySQL, you can use the MIN() function to retrieve the minimum value of a column, and then join it with the original table on that column to retrieve the full row that corresponds to that minimum value. Here's an example:
SELECT t1.*
FROM my_table t1
INNER JOIN (
SELECT group_column, MIN(order_column) AS min_order
FROM my_table
GROUP BY group_column
) t2 ON t1.group_column = t2.group_column AND t1.order_column = t2.min_order
In this example, my_table is the name of the table you want to query, group_column is the column you want to group by, and order_column is the column you want to retrieve the minimum value of. The subquery selects the minimum value of order_column for each distinct value of group_column, and then the outer query joins that subquery with the original table to retrieve the full row that corresponds to that minimum value
原文地址: https://www.cveoy.top/t/topic/fFiH 著作权归作者所有。请勿转载和采集!