我有一个名为log
的列,其中包含日志记录信息。我需要一个查询来选择值today
在我的日志列中出现两次或三次的所有行。
id log
1 today, yesterday, today, tomorrow, today
2 now, today, now
3 now, today, today
Select id from table if `today` appears three times in log column
将选择id:1
答案 0 :(得分:1)
这将完成你的工作:
select * from table where ROUND (
(
LENGTH(log)
- LENGTH( REPLACE ( log, "today", "") )
) / LENGTH("today")
) >=3
答案 1 :(得分:0)
试试这个:
select * from yourtable a
inner join (
select max(id) as id,log,
sum(case when log = 'today' then 1 else 0 end) as c
from yourtable
group by log
) b on b.id = a.id and b.cnt = 3
答案 2 :(得分:0)
将GROUP BY
子句与HAVING
SELECT log
FROM tbl
GROUP BY log
HAVING COUNT(*) >= 2