mysql:如果有相同的值,最后五条记录

0sgqnhkj  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(416)

我搜索了很多,尝试了很多问题,但没有得到满意的答案。所以我想问问。
如果有相同的值,我会从mysql表中查找最后5条记录。
像这样的 if col_n is having same value x from last 5 records then count otherwise not . 但是我不知道如何为这个写查询?

SELECT count(col_n) 
from track if(last five col_n = 'ok') 
WHERE col_a = 'value1' AND col_b = 'value2'

输入mysql表记录

1zmg4dgp

1zmg4dgp1#

请试试这个:

SELECT  p.*
FROM
    ( SELECT * 
      FROM   demo  ORDER by id DESC
      LIMIT 5
    ) AS p
  JOIN
    ( SELECT COUNT(*) AS cnt
      FROM   
        ( SELECT 1 
          FROM   demo
          LIMIT 5
        ) AS tmp
    ) AS c
    ON c.cnt = 5 WHERE p.name='x'

相关问题