如果列具有相同的值次数,请选择行

时间:2015-09-15 08:28:29

标签: mysql select

我有一个名为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

3 个答案:

答案 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