MySQL - 在子查询中引用别名表

时间:2014-06-23 12:07:11

标签: mysql

我正在尝试获取我正在处理的项目中最受欢迎用户的列表。受欢迎的标准是:a)拥有最多的帖子,b)在帖子上拥有最多的喜欢,c)拥有最多的帖子,以及d)拥有最多的博客订阅者。

我可以成功地获得标准a)和d)运作良好,如下:

SELECT User.id, User.first_name, COALESCE(Sub.subs, 0) AS subscribers, COALESCE(Post.posts, 0) AS posts, User.profile_pic
FROM blogs AS Blog
LEFT JOIN (
    SELECT user_id, count(1) as subs
    FROM subscriptions
    GROUP BY user_id) AS Sub ON (Sub.blog_id = Blog.id)
LEFT JOIN (
    SELECT user_id, count(1) as posts
    FROM posts
    GROUP BY user_id) AS Post ON (Post.blog_id = Blog.id)
INNER JOIN users AS User ON (User.id = Blog.user_id)
ORDER BY subscribers DESC, posts DESC
LIMIT 25

这为我提供了大多数帖子和博客大多数订阅者排名前25位的用户列表。但是,我不确定如何将标准b)和c)合并到此查询中,这可能涉及子查询中的JOINS,基于下面的表结构:

博客表:

---------------------------------
| id  |  blog_name  |  user_id  |
---------------------------------

帖子表:

------------------------------
| id  |  user_id  |  blog_id |
------------------------------

订阅表:

-----------------------------
| id  | user_id  |  blog_id |
-----------------------------

用户表:

-------------------------------------------------------------
| id  | first_name  | last_name  |  enc_pwd  |  profile_pic |
-------------------------------------------------------------

喜欢表:

-----------------------------
| id  | post_id  |  user_id |
-----------------------------

股票表:

----------------------------------------
| id  | from_user | to_user |  post_id |
----------------------------------------

我尝试过以下查询,但它给出了Unknown column Post.id in ON clause错误。

SELECT User.id, User.first_name, COALESCE(Sub.subs, 0) AS subscribers, COALESCE(Post.posts, 0) AS posts, COALESCE(Share.shares, 0) AS shares, User.profile_pic
FROM blogs AS Blog
LEFT JOIN (
    SELECT user_id, count(1) as subs
    FROM subscriptions
    GROUP BY user_id) AS Sub ON (Sub.blog_id = Blog.id)
LEFT JOIN (
    SELECT user_id, count(1) as posts
    FROM posts
    GROUP BY user_id) AS Post ON (Post.blog_id = Blog.id)
LEFT JOIN (
    SELECT s.post_id, count(1) as shares
    FROM shares AS s
    INNER JOIN posts AS p ON p.id = s.post_id
    INNER JOIN users AS U ON u.id = p.user_id
    GROUP by s.post_id) AS Share ON (Share.post_id = Post.id) -- Error occurs here
INNER JOIN users AS User ON (User.id = Blog.user_id)
ORDER BY subscribers DESC, posts DESC
LIMIT 25

1 个答案:

答案 0 :(得分:0)

我经过一些试验和错误后想出来了。在我看来,您必须以这样的方式编写查询:所有子查询最终应引用FROM子句中第一个表中的相关列(在本例中为x.blog_id = Blog.id )。我是一个中级MySQL用户,这是我第一次不得不写这样的怪物查询(而且我知道这里的所有那些真正的怪物SQL都无法接近!),所以请原谅我无法在我的回答中使用正确的SQL术语。我一直非常满意CakePHP和Rails为我编写查询。所以,如果你们中的任何一位SQL专家都遇到过这种情况,那么如果你能在技术上解释一下这将会很棒。

以下是我如何获得帖子最多,帖子次数最多且分享次数最多且排名最多的前25位用户的列表:

SELECT User.id, User.first_name, COALESCE(Sub.subs, 0) AS subscribers, COALESCE(Post.posts, 0) AS posts, COALESCE(Share.shares, 0) AS shares, COALESCE(`Like`.likes, 0) AS likes, User.profile_pic
FROM blogs AS Blog
LEFT JOIN (
    SELECT blog_id, count(1) as subs
    FROM subscriptions
    GROUP BY blog_id) AS Sub ON (Sub.blog_id = Blog.id)
LEFT JOIN (
    SELECT blog_id, count(1) as posts
    FROM posts
    GROUP BY blog_id) AS Post ON (Post.blog_id = Blog.id)
LEFT JOIN (
    SELECT blog_id, count(1) as shares
    FROM blogs
    INNER JOIN posts ON (posts.blog_id = blogs.user_id)
    INNER JOIN shares ON (posts.id = shares.post_id)
    GROUP by blog_id) AS Share ON (Share.blog_id = Blog.id)
LEFT JOIN (
    SELECT blog_id, count(1) as likes
    FROM blogs
    INNER JOIN posts ON (posts.blog_id = blogs.user_id)
    INNER JOIN likes ON (likes.post_id = posts.id)
    GROUP BY blog_id) AS `Like` ON (`Like`.blog_id = Blog.id)
INNER JOIN users AS User ON (User.id = Blog.user_id)
ORDER BY subscribers DESC, posts DESC, shares DESC, likes DESC
LIMIT 25

结果(例如):

-------------------------------------------------------------------------------------------------
| id  |  first_name  | last_name  |  subscribers |  posts  |  shares  |  likes  |  profile_pic  |
-------------------------------------------------------------------------------------------------
|  1  |     Yss      |    Salas   |      243     |   433   |   154    |   545   | 1437ytr_o.png |
|  4  |    Balu      |      V     |      143     |   145   |    45    |   234   | default.png   |
|  3  |   Jessica    |    Nigri   |      134     |   120   |    90    |    77   | default.png   |
-------------------------------------------------------------------------------------------------