如果用户投票,则获取总计数以及计数

时间:2014-04-20 01:51:50

标签: mysql sql

我加入了三张桌子。 submissionssubmissions_votesusers

我想知道有多少总有用的投票(这是所有submissions_votes的总和),我已经得到了。

如果user_id的{​​{1}}与正在查看的提交相关,我还想返回0或1的计数(布尔值)。 sv.user_id传递给user_id子句。

WHERE

我知道我需要一个额外的加入(在 SELECT s.*, u.username, u.photo as userPhoto, COALESCE(SUM(sv.up), 0) helpfulVotes FROM submissions s LEFT JOIN submissions_votes sv on s.id = sv.submission_id WHERE u.id = ? INNER JOIN users u ON s.user_id = u.id 上),但我会选择什么?然后我会按sv.user_id = u.id分组吗?

编辑:

sv.id表:

users

+----------------+------------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +----------------+------------------------+------+-----+-------------------+-----------------------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | email | varchar(128) | NO | MUL | NULL | | | username | varchar(23) | NO | | NULL | | | type | enum('normal','admin') | NO | | normal | | | about | varchar(255) | NO | | NULL | | | photo | varchar(32) | NO | | NULL | | +----------------+------------------------+------+-----+-------------------+-----------------------------+ 表:

submissions_votes

+---------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | submission_id | int(10) unsigned | NO | MUL | NULL | | | when | datetime | NO | | NULL | | | user_id | int(10) unsigned | NO | MUL | NULL | | | up | tinyint(3) unsigned | NO | | NULL | | | down | tinyint(3) unsigned | NO | | NULL | | +---------------+---------------------+------+-----+---------+----------------+ 表:

submissions

6 个答案:

答案 0 :(得分:1)

我认为你不需要额外加入。只是select中的布尔表达式:

SELECT s.*,
       u.username,
       u.photo as userPhoto,
       COALESCE(SUM(sv.up), 0) helpfulVotes,
       SUM(sv.user_id = u.id) as SubmissionUserMatches
FROM submissions s LEFT JOIN
     submissions_votes sv
     on s.id = sv.submission_id INNER JOIN
     users u
     ON s.user_id = u.id
GROUP BY s.id, u.username, u.photo;

答案 1 :(得分:1)

您可以使用sv.user_iduser_id来检查CASE =输入SUM是否合适(按每次提交进行分组)。如果SUM为1,则输入user_id有提交,否则没有。 因此,您的输入user_id将进入CASE函数。

此外,COALESCE(SUM(sv.up), 0)要求按照submissionsusers表格的任意列进行分组。

以下是基于 SQL Fiddle here 中的表格的查询。

SELECT 
  s.id as submission_id,
  s.title as submission_title,
  MAX(u.email) as submission_user_email,
  COALESCE(SUM(sv.up), 0) helpfulVotes,
  SUM(CASE sv.user_id
      WHEN ? THEN 1
      ELSE 0
      END) User_Submission
FROM
  submissions s
LEFT JOIN submissions_votes sv on s.id = sv.submission_id
INNER JOIN USERS u
ON s.user_id = u.id
GROUP BY s.id, s.title;

(如果需要选择submissions表中的更多列,则需要对它们进行分组或汇总)

答案 2 :(得分:0)

我使用mssql所以不确定我的答案是否适用于mysql。在mssql中,如果没有group by,问题中的查询将无法工作。如果您确实离开了该组,则应该在组中包含srsv.submission_id。 假设您有一个约束,确保每个提交和用户只有submission_vote,则以下内容应该有效:

SELECT s.*,
  u.username,
  u.photo as userPhoto,
  COALESCE(SUM(sv.up), 0) helpfulVotes
  case when srsv.submission_id is null then 1 else 0 end SelfRelatedVote
FROM
  submissions s
LEFT JOIN submissions_votes sv on s.id = sv.submission_id
Left JOIN submissions_votes srsv on s.id = srsv.submission_id and s.user_id = srsv.user_id
INNER JOIN users u
ON s.user_id = u.id

答案 3 :(得分:0)

检查

SELECT s.*,
  u.username,
  COALESCE(SUM(sv.up), 0) helpfulVotes,
  IF (srsv.submission_id is null, 1, 0)
FROM
  submissions s
LEFT JOIN submissions_votes sv on s.id = sv.submission_id
LEFT JOIN submissions_votes srsv on s.id = srsv.submission_id and s.user_id = {USER_ID}
INNER JOIN users u ON s.user_id = u.id

答案 4 :(得分:0)

要获取所有提交的总数以及仅为用户提交的提交总数(或其他特定于用户的聚合),您可以两次加入submission_votes表,并为表的第二个连接使用不同的别名。这将允许对用户特定的Submission_Vote详细信息进行任何聚合。

这是伪代码 - GROUP BY s。*必须由您选择的字段替换并按以下分组:

SELECT 
     s.*
    ,u.username
    ,u.photo                        AS  userPhoto
    ,COALESCE(SUM(sv.up), 0)        AS  helpfulVotes
    ,COALESCE(SUM(sv_user.up), 0)   AS  helpfulVotes_user
FROM
    submissions s

    LEFT JOIN submissions_votes sv 
    on s.id = sv.submission_id 

    LEFT JOIN submissions_votes sv_user
    on s.id = sv_user.submission_id 

    INNER JOIN users u
    ON s.user_id = u.id

WHERE
    sv_user.user_id = @User_id
GROUP BY
     s.*
    ,u.username
    ,u.photo        

答案 5 :(得分:0)

我不清楚布尔值应该表示什么,但我假设它是否在submission_votes中提供的user_id至少有1次投票,因为如果submission_votes中的所有投票都应该与user_id,然后布尔值将是多余的,因为人们只能查看计数。

我认为子查询是要走的路。尝试这样的事情:

SELECT s.*,
      u.username,
      u.photo as userPhoto,
      COALESCE(
      (
        SELECT SUM(sv.up) FROM
        submission_votes sv
        WHERE sv.submission_id = s.id
      ), 0) helpfulVotes,
      (CASE 
       WHEN EXISTS (SELECT 1 
            FROM submission_votes 
            WHERE sv.submission_id = s.id 
            AND sv.user_id = ?)
       THEN 1 
       ELSE 0
       END) userSubmissionVoteExists
    FROM
    submissions s
    INNER JOIN users u ON s.user_id = u.id