0

假设有以下三个表:

  • td_idea

    +----------+--------------+----------+ ------------------+
    | 想法ID | 想法名称 | idea_submitter_id | 想法状态 |
    +----------+--------------+----------+ ------------------+
    | 1 | 衬衫 | 3 | 设计 |
    | 2 | 顶部 | 1 | 颜色 |
    +----------+--------------+----------+ ------------------+
    
  • td_idea_contribution

    +-----------------+------------+----------------- +
    | 贡献ID | 想法ID | 提交者 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 | 贡献ID | 提交者 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,我希望看到以下输出:

+------------------+------------------+------------ ---+
| 贡献ID | 总喜欢 | 用户 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

编辑

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

+------------------+------------------+------------ ---+
| 贡献ID | 总喜欢 | 用户 ID |
+------------------+------------------+------------ ---+
| 1 | 8 | 3 |
+------------------+------------------+------------ ---+
4

3 回答 3

1

GROUP BY(聚合)函数下所述:

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

这就是您的情况。为了防止它,您需要按以下方式对结果进行分组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上查看。

于 2013-05-24T10:13:22.887 回答
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

PS:避免使用过时、晦涩的形式FROM t1, t2 WHERE t1.pk = t2.fk。更喜欢更明确的FROM t1 JOIN t2 ON (t1.pk = t2.fk)

PS 2:如果您正在查看的想法恰好没有“喜欢”或没有“贡献”,则查询不会返回任何内容。无论如何,将所有JOIN子句替换LEFT JOIN为检索此类想法。

于 2013-05-24T10:11:31.053 回答
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
于 2013-05-24T10:11:38.070 回答