I want to extract records by specifying a Limit condition for a specific column.

Asked 1 years ago, Updated 1 years ago, 353 views

Is it possible to adapt LIMIT to specific columns?

As an example, I would like to extract two records with the same id, assuming the following data is available.

data for:

Desired Results:

*If id=3000, there are no more than two records with the same id, so I would like to display just one record as above without excluding it.

mysql sql

2022-09-30 21:56

1 Answers

ROW_NUMBER() is available for MySQL 8.0, so

MySQL 8.0 Reference Manual::12.21.1 Window Function Descriptions

You can write as follows:

SELECT no, id, name FROM(
  SELECT*, ROW_NUMBER() OVER(PARTION BY ID ORDER BY NO) as ranking
  FROM member
AS_WHERE ranking <=2;


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.