Select unique records limit with N rows

后端 未结 4 1284
悲哀的现实
悲哀的现实 2020-12-12 06:29

I have database records shown below,

id  | dataId    | value
1   |    1      |   xxx 
2   |    1      |   xx1
3   |    1      |   xx2
4   |    1      |   xx         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 07:09

    Here is an interesting article you can reference for getting a select number of items from a group. It can be found from this question.

    To get the latest 3 ids for each dataid you can use this query:

    SELECT id, dataid, value, date
    FROM myTable m
    WHERE(
      SELECT COUNT(*) FROM myTable mt
      WHERE mt.dataid = m.dataid AND mt.id >= m.id
    ) <= 3;
    

    In short, the subquery in the where clause will filter for the largest id vlaues and you can limit it to less than or equal to 3. Notice that WHERE mt.dataid = m.dataid is what is being used to group the rows.

    As the article suggests, this is not the most efficient way, but a clean way of writing it. A possibly more efficient way would be to use a UNION on each query individually. See the article for more information. It would look something like this:

    (SELECT * FROM myTable WHERE dataid = 1 ORDER BY id DESC LIMIT 3)
    UNION ALL
    (SELECT * FROM myTable WHERE dataid = 2 ORDER BY id DESC LIMIT 3)
    

    Here is an SQL Fiddle for both example.

提交回复
热议问题