我想检查MySQL中是否连续有四个相同的值(我的意思是一个接着一个,而不是一个表行)。因此,如果我正在检查值"附件",则以下查询结果将为false:
type_count post_type
1 post
2 attachment
3 post
4 post
5 attachment
6 post
7 attachment
8 post
9 post
10 post
11 post
12 attachment
13 post
14 post
15 post
虽然这个数据集证明是正确的:
type_count post_type
1 post
2 attachment
3 post
4 post
5 attachment
6 attachment
7 attachment
8 attachment
9 post
10 post
11 post
12 attachment
13 post
14 post
15 post
这可能吗?使用查询结果而不是表格。
答案 0 :(得分:3)
如果查询如下:
SELECT COUNT(*)
FROM (
SELECT type_count, post_type,
@seq := IF(@pt = post_type, @seq+1,
IF(@pt := post_type, 1, 1)) AS seq
FROM mytable
CROSS JOIN (SELECT @seq := 0, @pt := '') AS vars) AS t
WHERE post_type = 'attachment' AND seq >= 4
返回一个等于或大于1的数字,那么你的条件为真,否则为假。
如果您尝试在查询结果而不是表中查找序列,请使用派生表,例如FROM (select * from table2) AS mytable
答案 1 :(得分:0)