Mysql仅选择行的百分比

时间:2014-03-31 13:49:50

标签: mysql sql

我想使用第二列(art_count)仅显示包含总art_count的X%的行。

我的数据:

title   art_count
a       3
b       12
c       9
d       4
e       45

到目前为止我的查询:

SELECT title, COUNT(art) AS art_count
FROM table1
GROUP BY art HAVING ... ?

用SUM尝试但没有成功。

1 个答案:

答案 0 :(得分:5)

SELECT title, COUNT(art) AS art_count
FROM table1
GROUP BY art 
HAVING art_count >= (select count(*) * X / 100 from table1)

您需要为X

插入一个值