查询以显示已发布最大帖子数的2008批次的校友用户(Role-'Alumni')的名称,按名称排序

时间:2016-03-19 19:44:01

标签: mysql sql

我有4张桌子:

CREATE TABLE ROLE (
  id   INT PRIMARY KEY,
  name VARCHAR(50)
);

CREATE TABLE PROFILE (
  id     INT PRIMARY KEY,
  batch  VARCHAR(10)
);

CREATE TABLE USER (
  id          INT PRIMARY KEY,
  name        VARCHAR(50),
  role_id     INT REFERENCES ROLE(id),
  profile_id  INT REFERENCES PROFILE(id)
);

CREATE TABLE POST (
  id       INT PRIMARY KEY,
  content  VARCHAR(4000),
  user_id  INT REFERENCES USER(id)
);

编写一个SQL查询,以显示已经/已发布最大帖子数的2008批次的校友用户(Role-'Alumni')的名称,按名称排序。

我试过这个:

select user.name
from   user
       inner join role
       on user.role_id=role.id
       inner join profile
       on user.profile_id=profile.id
       inner join post
       on user.id=post.user_id
where  profile.batch="2008"
group by user.name
having count(post.content)=3
order by user.name;

我无法在有条款中加入正确的条件。

4 个答案:

答案 0 :(得分:1)

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE ROLE (
  id   INT PRIMARY KEY,
  name VARCHAR(50)
);

CREATE TABLE PROFILE (
  id     INT PRIMARY KEY,
  batch  VARCHAR(10)
);

CREATE TABLE USER (
  id          INT PRIMARY KEY,
  name        VARCHAR(50),
  role_id     INT REFERENCES ROLE(id),
  profile_id  INT REFERENCES PROFILE(id)
);

CREATE TABLE POST (
  id       INT PRIMARY KEY,
  content  VARCHAR(4000),
  user_id  INT REFERENCES USER(id)
);

INSERT INTO ROLE    VALUES ( 1, 'Role1' );
INSERT INTO PROFILE VALUES ( 1, '2008'  );
INSERT INTO USER    VALUES ( 1, 'Alice', 1, 1 );
INSERT INTO USER    VALUES ( 2, 'Bob', 1, 1 );
INSERT INTO USER    VALUES ( 3, 'Carol', 1, 1 );
INSERT INTO POST    VALUES ( 1, 'Post 1', 1 );
INSERT INTO POST    VALUES ( 2, 'Post 2', 1 );
INSERT INTO POST    VALUES ( 3, 'Post 1', 2 );
INSERT INTO POST    VALUES ( 4, 'Post 1', 3 );
INSERT INTO POST    VALUES ( 5, 'Post 2', 3 );

查询1

SELECT name
FROM   (
  SELECT name,
         CASE WHEN @prev_value =  num_posts THEN @rank_count
              WHEN @prev_value := num_posts THEN @rank_count := @rank_count + 1
         END AS rank
  FROM   (
    SELECT u.name,
           ( SELECT COUNT( 1 ) FROM post t WHERE t.user_id = u.id ) AS num_posts
    from   user u
           inner join role r
           on u.role_id=r.id
           inner join profile p
           on u.profile_id=p.id,
           ( SELECT @prev_value := NULL, @rank_count := 0 ) i
    where  p.batch="2008"
    ORDER BY num_posts DESC
  ) posts
) ranks
WHERE rank = 1
ORDER BY name ASC

<强> Results

|  name |
|-------|
| Alice |
| Carol |

答案 1 :(得分:0)

可以是此查询

select user.name, count(post.id)
from user 
inner join role on user.role_id = role.id
inner join profile on user.profile_id = profile.id 
inner join post on user.id = post.user_id
where profile.batch = '2008'
and role.name = 'alumni'
group by user.name 
order by user.name ;

如果您只想要有最大总和帖子的用户应该是这个

select user.name  from (
select user.name,  sum(post.id) totpost
from user 
inner join role on user.role_id = role.id
inner join profile on user.profile_id = profile.id 
inner join post on user.id = post.user_id
where profile.batch = '2008'
and role.name = 'alumni'
group by user.name 
having totpost = max(post.id)
order by totpost ) as mytable

答案 2 :(得分:0)

select user.name, count(post.id)
from user 
inner join role on user.role_id = role.id
inner join profile on user.profile_id = profile.id 
inner join post on user.id = post.user_id
where profile.batch = '2008'
and role.name='Alumni'
group by user.name 
order by user.name ;

答案 3 :(得分:0)

我对内容是否是一些帖子有点不确定?或者它是一个单独的帖子意味着每个用户有多行?无论如何,您可以尝试以下使用连接构造和子查询。

SELECT U.name
FROM User U, Profile Pr, Post Po, Role R
WHERE profile_id = Pr.id
AND role_id = R.id
AND user_id = U.id
AND R.name = 'Alumni'
AND batch = '2008'
AND content >= ALL (SELECT content FROM Post)
ORDER BY 1 DESC  (<---or ASC)