有三个我要加入的桌子。 submissionssubmissions_votesusers

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

如果sv.user_iduser_id与正在查看的提交有关,我还想返回0或1的计数(布尔值)。 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表格:

+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+
| Field       | Type                                                                                                                                                                                                                                                                                | Null | Key | Default | Extra          |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+
| id          | int(10) unsigned                                                                                                                                                                                                                                                                    | NO   | PRI | NULL    | auto_increment |
| title       | varchar(255)                                                                                                                                                                                                                                                                        | NO   | MUL | NULL    |                |
| slug        | varchar(255)                                                                                                                                                                                                                                                                        | NO   |     | NULL    |                |
| description | mediumtext                                                                                                                                                                                                                                                                          | NO   |     | NULL    |                |
| user_id     | int(11)                                                                                                                                                                                                                                                                             | NO   | MUL | NULL    |                |
| created     | datetime                                                                                                                                                                                                                                                                            | NO   |     | NULL    |                |
| type        | enum('tip','request')                                                                                                                                                                                                                                                               | NO   |     | NULL    |                |
| thumbnail   | varchar(64)                                                                                                                                                                                                                                                                         | YES  |     | NULL    |                |
| removed     | tinyint(1) unsigned                                                                                                                                                                                                                                                                 | NO   |     | 0       |                |
| keywords    | varchar(255)                                                                                                                                                                                                                                                                        | NO   |     | NULL    |                |
| ip          | int(10) unsigned                                                                                                                                                                                                                                                                    | NO   |     | NULL    |                |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+

===============>>#1 票数: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;

===============>>#2 票数:1 已采纳

您可以检查sv.user_id =输入user_id使用CASESUM它(通过提交各分组)。 如果SUM为1,则输入的user_id具有提交,否则没有提交。 因此,您的输入user_id将进入CASE函数。

此外, COALESCE(SUM(sv.up), 0)要求根据选择的submissions列和users表进行分组。

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

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表中选择更多列,则需要将它们分组或汇总)

===============>>#3 票数:0

我使用mssql,所以不确定我的答案是否适用于mysql。 在mssql中,如果没有分组依据,问题中的查询将无法工作。 如果您确实离开了分组依据,则应在分组依据中包含srsv.submission_id。 假设您有一个约束来确保每个提交只包含submitt_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

===============>>#4 票数: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

===============>>#5 票数:0

要获得所有提交的总数以及仅针对该用户(或其他特定于用户的聚合)的提交总数,您可以两次加入submitt_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        

===============>>#6 票数:0

我不清楚布尔值应该表示什么,但我假设这是submitt_votes中提供的user_id是否至少有1票,因为如果submit_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

  ask by bob_cobb translate from so

未解决问题?本站智能推荐: