MySQL语法问题,多个SELECT语句

时间:2013-07-02 12:20:43

标签: mysql sql database database-schema

我正在开展一个项目,其中有两个表格如下: users_fb 帖子

我花了3个小时玩代码然后我放弃了。

表:帖子

+-----+---------+---------+---------+---------+-----------+
| id  | by_user | by_page |  votes  | status  |    time   |
+-----+---------+---------+---------+---------+-----------+
|   1 |       1 |       0 |      20 |       1 | 372041014 |
+-----+---------+---------+---------+---------+-----------+

table:users_fb

+-----+-----------+-------+---------+--------+-------+
| id  | username  | name  | gender  | fb_id  | email |
+-----+-----------+-------+---------+--------+-------+

SELECT username, 
        (
        SELECT      COUNT(b.by_user)
        FROM        users_fb a LEFT JOIN posts b ON a.id = b.by_user
        WHERE       b.by_page = '0'
        GROUP BY    a.username
        ) AS totalCount ,
        (
        SELECT      IFNULL(SUM(b.votes),0)
        FROM        users_fb a LEFT JOIN posts b ON a.id = b.by_user
        GROUP BY    users_fb.id
        ) AS total_votes
        FROM users_fb ORDER BY  total_votes DESC

所需的输出

+-------------------+-------------+-------------+
|     username      | totalCount  | total_votes |
+-------------------+-------------+-------------+
| user4             |          1  |          25 |
| user1             |          0  |          0  |
| user2             |          0  |          0  |
| user3             |          0  |          0  |
+-------------------+-------------+-------------+

不幸:这就是我的目标

+-------------------+-------------+-------------+
|     username      | totalCount  | total_votes |
+-------------------+-------------+-------------+
| user4             |          1  |          25 |
| user1             |          1  |          25 |
| user2             |          1  |          25 |
| user3             |          1  |          25 |
+-------------------+-------------+-------------+

如果您需要任何进一步的信息,请与我们联系。谢谢你的帮助。

5 个答案:

答案 0 :(得分:1)

SELECT      a.username,
            COUNT(b.by_user) totalCount,
            SUM(IFNULL(b.votes,0)) total_votes
FROM        users_fb a 
            LEFT JOIN posts b 
ON a.id = b.by_user
WHERE       b.by_page = '0'
GROUP BY    a.id,a.username

答案 1 :(得分:1)

相当多的问题,最大的问题是你没有在“主要查询”和“子查询”之间进行任何连接。

所以最后,这样的事情会更好。

SELECT
a.username,
SUM (CASE WHEN b.by_page IS NOT NULL and b.by_page = '0' then 1 else 0 end) as cnt,
SUM (IFNULL(b.votes),0) as nbVotes,
FROM  users_fb a 
LEFT JOIN posts b ON a.id = b.by_user
GROUP BY    users_fb.id

答案 2 :(得分:1)

由于您没有选择匹配(即它们之间没有绑定WHERE),MySQL无需等待它们一起显示。

你应该这样做:

    SELECT username, totalCount.count, total_votesGROUPED.sum
    FROM users_fb
    LEFT JOIN (
       SELECT      COUNT(b.by_user) as count, a.username
       FROM        users_fb a LEFT JOIN posts b ON a.id = b.by_user
       WHERE       b.by_page = '0'
       GROUP BY    a.username
    ) AS totalCount ON totalCount.username = users_fb.username,
    (
       SELECT      IFNULL(SUM(b.votes),0) as sum, id
       FROM        users_fb a LEFT JOIN posts b ON a.id = b.by_user
       GROUP BY    users_fb.id
    ) AS total_votesGROUPED ON total_votesGROUPED.id = users_fb.id
    ORDER BY  total_votes DESC

如果我有更多信息,我可以测试一下

答案 3 :(得分:1)

SELECT username, IFNULL(COUNT(b.by_user), 0) totalCount, IFNULL(SUM(c.votes), 0) total_votes
FROM users_fb a
LEFT JOIN posts b
ON a.id = b.by_user AND b.by_page = 0
LEFT JOIN posts c
ON a.id = b.by_user
GROUP BY username

SQLFIDDLE

答案 4 :(得分:1)

您似乎没有任何东西可以将您的桌子加在一起以匹配帖子/投票与哪个用户匹配。

这样的事情应该这样做

SELECT users_fb.username, Sub1.postcount, Sub2.votecount
FROM users_fb
LEFT OUTER JOIN(
    SELECT      a.username, COUNT(*) AS postcount
    FROM        users_fb a 
    INNER JOIN posts b 
    ON a.id = b.by_user
    WHERE       b.by_page = '0'
    GROUP BY    a.username
) Sub1
ON users_fb.username = Sub1.username
LEFT OUTER JOIN(        
    SELECT      users_fb.id, IFNULL(SUM(b.votes),0) AS votecount
    FROM        users_fb a 
    LEFT JOIN posts b 
    ON a.id = b.by_user
    GROUP BY    users_fb.id
) Sub2
ON users_fb.id = Sub2.id

可能简化为

SELECT      a.username, SUM(IF(b.by_page = '0', 1, 0)) AS postcount, IFNULL(SUM(b.votes),0) AS votecount
FROM        users_fb a 
LEFT JOIN posts b 
ON a.id = b.by_user
GROUP BY    a.username