我进行了很多搜索,并尝试了许多查询,但未获得满意的答案。所以想问。
我正在从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'
答案 0 :(得分:0)
请尝试以下操作:
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'