简体   繁体   English

mysql 查询:如何获得每天的yes/no票数

[英]mysql query: how to get the number of yes/no votes per day

I have to create a mysql query to get a voting distribution of each day exceeding a particular date, something like this...我必须创建一个 mysql 查询以获得超过特定日期的每一天的投票分布,就像这样......

    date          yes_votes    no_votes
------------------------------------------
    2010-01-07    21           22
    2010-01-07    2            0

My table is like this..我的桌子是这样的。。

    post_votes
--------------------------
    id(longint)
    date(timestamp)
    flag(tinyint) // this stores the yes/no votes 1-yes, 2-no

I am stuck at this....我被困在这个....

SELECT COUNT(*) AS count, DATE(date) FROM post_votes WHERE date > '2010-07-01' GROUP BY DATE(date)

this gives the total number of votes per day, but not the distribution that I want.这给出了每天的总票数,但不是我想要的分布。

SELECT COUNT(*) AS count
     , DATE(date)
     , SUM(flag = 1) AS yes_votes
     , SUM(flag = 2) AS no_votes
FROM post_votes 
WHERE date > '2010-07-01' 
GROUP BY DATE(date)

This is a trick that works in MySQL, as flag=1 will either be True or False .这是一个适用于 MySQL 的技巧,因为flag=1将是TrueFalse But True = 1 and False = 0 in MySQL so you can add the 1s and 0s using the SUM() function.但是 MySQL 中的True = 1False = 0 ,因此您可以使用SUM() function 添加 1 和 0。

Other solutions with IF or CASE would be better for clarity or if there is any chance you want to move the database to another RDBMS.使用IFCASE的其他解决方案会更好,或者如果您有任何机会将数据库移动到另一个 RDBMS。

Comments not related to the question:与问题无关的评论:

  • It's bad habit to use reserved words like date or count for naming fields or tables.使用datecount等保留字来命名字段或表是一个坏习惯。
  • It's also not good to use "date" when you actually store a timestamp.实际存储时间戳时使用“日期”也不好。 Names should reflect use.名称应反映用途。
  • For table names it's recommended to use singular ( post_vote ) and not plural - although many use plural, it gets confusing in the end.对于表名,建议使用单数 ( post_vote ) 而不是复数 - 尽管许多使用复数,但最终会让人感到困惑。 Plural is good for some fields or calulated fields, like your yes_votes and no_votes where we have a counting.复数适用于某些字段或计算字段,例如我们有计数的yes_votesno_votes

Sum it:总结一下:

select date(date) as date,
       sum(case when flag = 1 then 1 else 0) as yes,
       sum(case when flag = 2 then 1 else 0) as no
from post_votes
where date > '2010-07-01'
group by date(date)

you are almost at the solution:)你几乎是在解决方案:)

i would recommend the use of an IF condition in a SUM method like so:我建议在SUM方法中使用IF条件,如下所示:

SELECT SUM(IF(flag = 'yes',1,0)) AS yes_count,
       SUM(IF(flag = 'no',1,0)) AS no_count, 
       DATE(date) 
FROM post_votes 
WHERE date > '2010-07-01' 
GROUP BY DATE(date)

this will allow for the function to add 1 to each sum only if the value is equal to yes / no这将允许 function 仅在值等于是/ yesno每个总和加 1

SELECT DATE(date) as dt,
sum(if(flag=1,1,0)) as yes,
sum(if(flag=2,1,0)) as no
FROM post_votes WHERE date > '2010-07-01' 
GROUP BY dt

I had that problem too.我也有这个问题。 The best solution of that I can think of, is to split the "flag" in two fields, like:我能想到的最佳解决方案是将“标志”拆分为两个字段,例如:

upvote(tinyint)
downvote(tinyint)

Then you are able to count them very easy and without mysql-voodoo:然后你可以很容易地计算它们,而无需 mysql-voodoo:

SELECT 
  SUM(upvote) AS up,
  SUM(downvote) AS down,
  DATE(`date`) AS Created_at
FROM post_votes 
WHERE Created_at > '2010-07-01' 
GROUP BY Created_at

Btw.: You should not name a column date , because it's a MySQL-Keyword.顺便说一句:您不应该命名列date ,因为它是 MySQL 关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM