MySql:通过联合或联接按计数选择

发布于 2024-12-21 09:51:31 字数 1737 浏览 1 评论 0原文

我需要一种方法让页面版主可以查看按用户在该特定页面上发布的帖子总数排序的页面成员。这些帖子存储在不同的表中,例如民意调查、下载、wiki 等(例如 15 个表)。每个都有 id、user_id、page_id、标题、内容等结构。列出页面所有用户的表称为“收藏夹”。其结构很简单,就是 id、user_id、page_id、date。

我目前有一个查询,它将所有“内容表”联合在一起,以便特定用户在整个网站上显示他们在哪个页面上的所有帖子,我可以使用 WHERE page_id = $ 轻松地将其特定于每个页面page_id)。我尝试使用该代码来执行我在主持人区域中尝试执行的操作。正如我提到的,在主持人区域中,我需要按用户的帖子总数 DESC 对用户列表进行排序。

我有这段代码,但它不起作用。

$getfans = mysql_query("SELECT DISTINCT user_id, ((SELECT id FROM wiki WHERE wiki.page_id = $page_id AND wiki.user_id = favorites.user_id)
UNION ALL
(SELECT id FROM downloads WHERE downloads.page_id = $page_id AND downloads.user_id = favorites.user_id)
UNION ALL
(SELECT id FROM polls WHERE polls.page_id = $page_id AND polls.user_id = favorites.user_id)) AS posts 
FROM favorites WHERE favorites.page_id = $page_id AND favorites.status = 0 ORDER BY posts DESC", $conn);

我尝试了其他方法,但也不起作用,但这个方法对我来说最符合逻辑,所以这是我要显示的唯一方法,

请帮忙。谢谢。

另外......

这也许是朝着正确方向迈出的一步

$getfans = mysql_query("选择不同的 user_id, (SELECT COUNT(id) 来自民意调查 WHERE page_id = $page_id AND user_id = favorites.user_id) AS 帖子来自收藏夹 WHERE favorites.page_id = $page_id AND favorites.status = 0 ORDER BY posts DESC", $conn);

当我回显 $posts 时,它为我提供了该页面的用户投票的正确计数。

但是,当我尝试执行多个表时,它只返回一个表用户

$getfans = mysql_query("选择不同的 user_id, SUM((SELECT COUNT(id) FROM 民意调查 WHERE page_id = $page_id AND user_id = favorites.user_id)+(从 wiki WHERE page_id = 中选择 COUNT(id) $page_id AND user_id = favorites.user_id)) AS 来自收藏夹的帖子 WHERE favorites.page_id = $page_id AND favorites.status = 0 排序依据 发布 DESC", $conn);

我找到了解决方案
如果好奇,请阅读我下面的帖子。

I need a way for page moderators to view the members of their page ordered by the total number of posts the user has on that particular page. The posts are stored in different tables such as polls, downloads, wiki, etc (like 15 tables). Each of those have the structure of id, user_id, page_id, title, content, etc etc. The table that lists all the users of a page is called 'favorites'. The structure of that is simply id, user_id, page_id, date.

I currently have a query that unions all of the "content tables" together for a specific user to display ALL of their posts across the site regard of what page it is on, I could easily make it specific to each page using WHERE page_id = $page_id). I tried using that code to do what I'm trying to do in the moderator area. As I mentioned, in the moderator area I need to sort the list of users by their total posts count DESC.

I have this code but it is not working.

$getfans = mysql_query("SELECT DISTINCT user_id, ((SELECT id FROM wiki WHERE wiki.page_id = $page_id AND wiki.user_id = favorites.user_id)
UNION ALL
(SELECT id FROM downloads WHERE downloads.page_id = $page_id AND downloads.user_id = favorites.user_id)
UNION ALL
(SELECT id FROM polls WHERE polls.page_id = $page_id AND polls.user_id = favorites.user_id)) AS posts 
FROM favorites WHERE favorites.page_id = $page_id AND favorites.status = 0 ORDER BY posts DESC", $conn);

I tried other methods that also did not work but this one made the most logical sense to me so it's the only one I'm going to display

Please help. Thanks.

Additionally...

This was perhaps a step in the right direction

$getfans = mysql_query("SELECT DISTINCT user_id, (SELECT COUNT(id)
FROM polls WHERE page_id = $page_id AND user_id = favorites.user_id)
AS posts FROM favorites WHERE favorites.page_id = $page_id AND
favorites.status = 0 ORDER BY posts DESC", $conn);

When I echo'ed $posts it gave me the correct count for the user's polls for that page.

HOWEVER when I tried to do more than one table it only returned one user.

Example:

$getfans = mysql_query("SELECT DISTINCT user_id, SUM((SELECT COUNT(id)
FROM polls WHERE page_id = $page_id AND user_id =
favorites.user_id)+(SELECT COUNT(id) FROM wiki WHERE page_id =
$page_id AND user_id = favorites.user_id)) AS posts FROM favorites
WHERE favorites.page_id = $page_id AND favorites.status = 0 ORDER BY
posts DESC", $conn);

I FOUND THE SOLUTION

if curious, just read my post below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小情绪 2024-12-28 09:51:31

使用左连接!

SELECT 
    f.user_id, 
    COUNT(w.user_id) + COUNT(d.user_id) + COUNT(p.user_id) as PageCount
FROM 
    favorites f
    left join wiki w on
        f.page_id = w.page_id
        and f.user_id = w.user_id
    left join downloads d on
        f.page_id = d.page_id
        and f.user_id = d.user_id
    left join polls p on
        f.page_id = p.page_id
        and f.user_id = p.user_id
WHERE 
    f.page_id = $page_id 
    AND f.status = 0
GROUP BY
    f.user_id
ORDER BY PageCount DESC

如果您想坚持使用联合方法:

select
    f.user_id,
    count(x.user_id) as PageCount
from
    favorites f
    inner join (
        select user_id, page_id from wiki
        union all
        select user_id, page_id from downloads
        union all
        select user_id, page_id from polls
        union all
        select user_id, page_id from videos
    ) x on
       f.user_id = x.user_id
       and f.page_id = x.page_id
WHERE 
    f.page_id = $page_id 
    AND f.status = 0
group by
    f.user_id
ORDER BY PageCount DESC

您想使用group by进行聚合,并且count函数将忽略您遇到的任何空值,因此它将计算正匹配。

Use left joins!

SELECT 
    f.user_id, 
    COUNT(w.user_id) + COUNT(d.user_id) + COUNT(p.user_id) as PageCount
FROM 
    favorites f
    left join wiki w on
        f.page_id = w.page_id
        and f.user_id = w.user_id
    left join downloads d on
        f.page_id = d.page_id
        and f.user_id = d.user_id
    left join polls p on
        f.page_id = p.page_id
        and f.user_id = p.user_id
WHERE 
    f.page_id = $page_id 
    AND f.status = 0
GROUP BY
    f.user_id
ORDER BY PageCount DESC

If you'd like to stick with the union approach:

select
    f.user_id,
    count(x.user_id) as PageCount
from
    favorites f
    inner join (
        select user_id, page_id from wiki
        union all
        select user_id, page_id from downloads
        union all
        select user_id, page_id from polls
        union all
        select user_id, page_id from videos
    ) x on
       f.user_id = x.user_id
       and f.page_id = x.page_id
WHERE 
    f.page_id = $page_id 
    AND f.status = 0
group by
    f.user_id
ORDER BY PageCount DESC

You want to use a group by for aggregates, and the count function will ignore any nulls that you run into, so it will only count positive matches.

雨后彩虹 2024-12-28 09:51:31

我找到了解决方案。我以为我很早就有了这个,但我想我有点偏离了。感谢所有参与的人。

$getfans = mysql_query("SELECT DISTINCT user_id,
(SELECT COUNT(id) FROM wiki WHERE page_id = $page_id AND user_id = favorites.user_id) + 
(SELECT COUNT(id) FROM downloads WHERE page_id = $page_id AND user_id = favorites.user_id) + 
(SELECT COUNT(id) FROM polls WHERE page_id = $page_id AND user_id = favorites.user_id)  
AS posts FROM favorites
WHERE favorites.page_id = $page_id
AND favorites.status = 0
ORDER BY posts DESC", $conn);

只需继续对每个新表末尾添加 + 的子查询即可。也许不是最简洁的代码,但它可以工作。

这有效。我两天都无法接受它作为答案:(

I FOUND THE SOLUTION. I thought I had this earlier but I guess I was off by the slightest bit. Thanks everyone who participated.

$getfans = mysql_query("SELECT DISTINCT user_id,
(SELECT COUNT(id) FROM wiki WHERE page_id = $page_id AND user_id = favorites.user_id) + 
(SELECT COUNT(id) FROM downloads WHERE page_id = $page_id AND user_id = favorites.user_id) + 
(SELECT COUNT(id) FROM polls WHERE page_id = $page_id AND user_id = favorites.user_id)  
AS posts FROM favorites
WHERE favorites.page_id = $page_id
AND favorites.status = 0
ORDER BY posts DESC", $conn);

just keep doing the subquery with the + at the end for each new table. Perhaps not the most concise code but it works.

This works. I can't accept it as an answer for two days :(

疑心病 2024-12-28 09:51:31

这是我最喜欢的 SQL 查询,对于一个表,

Select Distinct user_id, Count(user_id) as C from yourtable group by user_id 

将显示每个人的 user_id 以及他们在该表中的行数。将这些结果组合起来应该很简单。

顺便说一句,如果所有表都有相同的结构,为什么不让它们都在同一个表中呢?您可以添加一列来指示记录适用的项目类型。

This is my favorite SQL query, for one table,

Select Distinct user_id, Count(user_id) as C from yourtable group by user_id 

will show you each person's user_id and the number of rows they have in that table. Combining a bunch of these results should be straightforward.

BTW, if all the tables have the same structure, why not have them all be in the same table? You could add a column indicating what type of item the record applies to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文