获取具有值的列的百分比

时间:2014-04-03 08:21:49

标签: php mysql

我试着这样做:

 SELECT (SELECT COUNT(*)
 FROM users WHERE email != '') / (SELECT COUNT(*) FROM users)

但它在php Operand should contain 1 column(s)中给了我这个错误,在mysql中一切正常。还有其他解决办法吗?

  编辑:它不完全像我几个月也有组,上面的查询有效,但接受的答案解决了我的错误

4 个答案:

答案 0 :(得分:3)

您可以在一个查询中执行此操作:

SELECT SUM(email != '') / COUNT(*) 
    AS ratio
  FROM users

......或者,如果你需要百分比:

SELECT 100 * SUM(email != '') / COUNT(*)
    AS percentage
  FROM users

答案 1 :(得分:0)

试试这个..

SELECT (SELECT COUNT(your_primary_key) FROM users WHERE email != '') / (SELECT COUNT(your_primary_key) FROM users) ...

由于两个表中的字段数量不同而发生这种情况......

答案 2 :(得分:0)

试试这个:

 SELECT COUNT(*) as counts
 FROM users 
 WHERE email != '' 

答案 3 :(得分:0)

你想做什么?如果您可以在SQL查询中计算百分比,则可以点击链接Calculating Percentage within MYSQL query based on conditions