我有一个这样的数据库:
USER WL
Kevin yes
Kevin no
Kevin yes
Steve yes
Steve no
我需要算出肯定的数量(凯文有2个)并除以总数(凯文总共3个)然后按最高顺序显示结果。凯文2/3 = 66%
SELECT *, SUM(wl = 'yes')/COUNT(wl) AS total
FROM Table1
WHERE user = '*' GROUP BY user ORDER BY total DESC;
答案 0 :(得分:0)
这称为条件计数。你可以通过sum()或count()来实现它。我将使用sum():
为您提供示例查询SELECT user, SUM(if(wl = 'yes',1,0))/COUNT(wl) AS total
FROM Table1
GROUP BY user ORDER BY total DESC;
你也可以在sum()中使用case语句而不是if()函数。