mysql - 复合mysql查询异常问题

标签 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       |
    +------------------+------------+-----------------+
    

    This table shows the contribution that users have made on a particular idea.

  • 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      |
    +----------+--------------------+-----------------+
    

I want to display all the contributions for a particular idea_id, each showing the number of votes for each contribution listed.

So, for idea_id = 1, I wish to see the following output:

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

I am currently using the following query, but weird results are being displayed:

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       |
+------------------+------------------+--------------+

最佳答案

GROUP BY (Aggregate) Functions 下所述:

If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.

这就是您的情况所发生的情况。为了防止这种情况,您需要按 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 .

关于mysql - 复合mysql查询异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16731945/

相关文章:

php - INNER JOIN 中 SELECT IF 的 Mysql 结果

mysql - 计算查询结果

php - 如何使用 PHP PDO 函数提取获取 SQL 事务中特定查询的结果?

mysql - 根据id按时间顺序连接两个表

mysql - 使用 Mysql 查询的前十个计数

mysqldump 兼容模式 postgresql 不工作

sql - 定义列别名?

mysql - 查询两个日期之间的小时和分钟

mysql - 当mysql中有外键时,如何插入到列中

.net - 无法找到 MySQL + .net 的错误