mysql从一个以逗号分隔值的字段计算总数

时间:2015-06-25 04:31:22

标签: mysql

在我的mysql数据库中,我有一个包含此列的表。此列中的值如下所示

年份:1974,2000,1976,1977

如何编写一个mysql语句来选择和计算上述值的总数超过1997的数量?

我的输出如下:2

Select year FROM my_table

这是我目前的陈述:

SELECT  year 
FROM reg2015 
WHERE cancel is NULL AND mealroom = 'Y' ORDER BY regid ASC

我的输出就像这样

year
-------------------------
1974,1976,2005,2008
1954,1958
1987
1946
1956,1959
1944,1946
1974,1975,2005,2007,2001,2002
1971
1978
1955,1969,2000,2003

所以我想知道每行有多少比1997年多。

1 个答案:

答案 0 :(得分:0)

检查这个我使用过Postgresql,有" id"," comma_sep_string"具有表名" string_comma"的列,当有计数时,它会显示该记录,如果计数为零,则它不显示该特定记录并查看该输出的屏幕截图给定查询

select temp1.id, temp1.comma_sep_string, sum(count) from
(select temp.id, temp.comma_sep_string, temp.years, count(*) as count
from
(SELECT
ID, comma_sep_string,
regexp_split_to_table(comma_sep_string, E',') AS years
FROM string_comma) as  temp
where temp.years::int > 1980
group by temp.id, temp.years, temp.comma_sep_string) as temp1
group by temp1.id, temp1.comma_sep_string

This is the Screenshot