MySql:通过联合或联接按计数选择
我需要一种方法让页面版主可以查看按用户在该特定页面上发布的帖子总数排序的页面成员。这些帖子存储在不同的表中,例如民意调查、下载、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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用左连接!
如果您想坚持使用联合方法:
您想使用
group by
进行聚合,并且count
函数将忽略您遇到的任何空值,因此它将仅计算正匹配。Use left joins!
If you'd like to stick with the union approach:
You want to use a
group by
for aggregates, and thecount
function will ignore any nulls that you run into, so it will only count positive matches.我找到了解决方案。我以为我很早就有了这个,但我想我有点偏离了。感谢所有参与的人。
只需继续对每个新表末尾添加 + 的子查询即可。也许不是最简洁的代码,但它可以工作。
这有效。我两天都无法接受它作为答案:(
I FOUND THE SOLUTION. I thought I had this earlier but I guess I was off by the slightest bit. Thanks everyone who participated.
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 :(
这是我最喜欢的 SQL 查询,对于一个表,
将显示每个人的 user_id 以及他们在该表中的行数。将这些结果组合起来应该很简单。
顺便说一句,如果所有表都有相同的结构,为什么不让它们都在同一个表中呢?您可以添加一列来指示记录适用的项目类型。
This is my favorite SQL query, for one table,
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.