0

我正在处理一个有 2 个表的项目,如下所示:users_fbposts

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

表:职位

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

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

如果您需要任何进一步的信息,请告诉我。谢谢你的帮助。

4

5 回答 5

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

于 2013-07-02T12:28:38.463 回答
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
于 2013-07-02T12:28:57.607 回答
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
于 2013-07-02T12:25:26.007 回答
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
于 2013-07-02T12:25:36.567 回答
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

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

于 2013-07-02T12:26:36.673 回答