复合mysql查询异常问题

时间:2013-05-24 09:40:08

标签: mysql

假设一个人有以下三个表:

  • td_idea

    +----------+--------------+----------------------+------------------+
    | idea_id  |  idea_name   |  idea_submitter_id   |   idea_status    |
    +----------+--------------+----------------------+------------------+
    | 1        |  shirt       |           3          |   Design         |
    | 2        |  top         |           1          |   Color          |
    +----------+--------------+----------------------+------------------+
    
  • td_idea_contribution

    +------------------+------------+-----------------+
    | contribution_id  |  idea_id   |  submitter_id   |
    +------------------+------------+-----------------+
    |        1         |      1     |        3        |
    |        2         |      1     |        4        |
    |        3         |      1     |        7        |
    |        4         |      1     |        10       |
    |        5         |      2     |        3        |
    |        6         |      2     |        10       |
    +------------------+------------+-----------------+
    

    此表显示了用户对特定想法的贡献。

  • td_idea_contribution_like

    +----------+--------------------+-----------------+
    | like_id  |  contribution_id   |  submitter_id   |
    +----------+--------------------+-----------------+
    |     1    |          1         |        1        |
    |     2    |          1         |        4        |
    |     3    |          2         |        24       |
    |     4    |          1         |        73       |
    |     5    |          4         |        124      |
    |     6    |          2         |        34       |
    |     7    |          1         |        75       |
    |     8    |          3         |        124      |
    +----------+--------------------+-----------------+
    

我想显示特定idea_id的所有贡献,每个贡献显示列出的每个贡献的投票数。

因此,对于idea_id = 1,我希望看到以下输出:

+------------------+------------------+--------------+
|  contribution_id |    total_likes   |    user_id   |
+------------------+------------------+--------------+
|        1         |        4         |      3       |
|        2         |        2         |      4       |
|        3         |        1         |      7       |
|        4         |        1         |     10       |
+------------------+------------------+--------------+

我目前正在使用以下查询,但显示了奇怪的结果:

SELECT   td_idea.idea_id,
         td_idea_contribution.contributuion_id,
         td_idea_contribution.submitter_id,
         COUNT(td_idea_contribution_like.contribution_like_id) AS tot_like
FROM     td_idea, td_idea_contribution, td_idea_contribution_like
WHERE    td_idea.idea_id = td_idea_contribution.idea_id 
     AND td_idea_contribution.contribution_id = td_idea_contribution_like.contribution_id
     AND td_idea.idea_id = '$id'
     AND td_idea.status LIKE '%Design%'
ORDER BY tot_like DESC

修改

我没有收到错误,我只是得到包含一条记录的异常结果,如下所示:

+------------------+------------------+--------------+
|  contribution_id |    total_likes   |    user_id   |
+------------------+------------------+--------------+
|        1         |        8         |      3       |
+------------------+------------------+--------------+

3 个答案:

答案 0 :(得分:1)

正如GROUP BY (Aggregate) Functions所述:

  

如果在不包含GROUP BY子句的语句中使用组函数,则相当于对所有行进行分组。

这就是你的情况。要防止这种情况发生,您需要contribution_id

SELECT   contribution_id,
         COUNT(*) AS total_likes,
         c.submitter_id AS user_id
FROM     td_idea                   i
    JOIN td_idea_contribution      c USING (        idea_id)
    JOIN td_idea_contribution_like l USING (contribution_id)
WHERE    idea_id = 1
     AND idea_status LIKE '%Design%'
GROUP BY contribution_id
ORDER BY total_likes DESC

sqlfiddle上查看。

答案 1 :(得分:0)

查询似乎缺少GROUP BY子句:

SELECT
    i.idea_id,
    i_c.contribution_id,
    i_c.submitter_id,
    COUNT(i_c_l.contribution_like_id) AS tot_like 

FROM td_idea AS i
JOIN td_idea_contribution AS i_c USING ( idea_id )
JOIN td_idea_contribution_like AS i_c_l USING ( contribution_id )

WHERE
    i.idea_id='$id'
    AND i.status LIKE '%Design%'

GROUP BY idea_id, contribution_id
ORDER BY tot_like DESC

P.S。:避免使用过时的,模糊的形式FROM t1, t2 WHERE t1.pk = t2.fk。更喜欢更明确的FROM t1 JOIN t2 ON (t1.pk = t2.fk)

P.S。 2:如果您正在查看的构思恰好没有“喜欢”或没有“贡献”,则查询不会返回任何内容。将所有JOIN条款替换为LEFT JOIN,无论如何都要检索此类提示。

答案 2 :(得分:0)

试试这个:

SELECT idea.idea_id,
       contrib.contributuion_id,
       contrib.submitter_id,
       (SELECT COUNT(like2.contribution_like_id)
          FROM td_idea_contribution_like like2
         WHERE like2.contribution_id = contrib.contribution_id) AS tot_like
  FROM td_idea idea,
       td_idea_contribution contrib,
       td_idea_contribution_like likes
  WHERE idea.idea_id = contrib.idea_id 
    AND contrib.contribution_id = likes.contribution_id
    AND idea.idea_id = '$id'
    AND idea.status LIKE '%Design%'
  ORDER BY tot_like DESC