获取mysql中嵌套查询的值

时间:2013-02-18 10:58:11

标签: mysql nested

SELECT *, count(idWallHasWallPost) as republish FROM admin_pw.wall_has_wallpost 
    where WallPost_idWallPost in 
    (select WallPost_idWallPost from wall_has_wallpost where Wall_idWall in 
    (select Wall_idWall from follower where User_idUser=1))
    group by WallPost_idWallPost 
    having republish>1;

我在mysql中有这个嵌套查询。无论如何都要从查询中更高的嵌套查询中访问这些值。

我想在上次选择中访问Wall_idWall。我选择最低的查询,并希望看到in运算符使用了哪个Wall_idWall。

2 个答案:

答案 0 :(得分:1)

尝试:

SELECT w.*, count(w.idWallHasWallPost) as republish, v.all_Wall_idWall
FROM admin_pw.wall_has_wallpost w
join (select h.WallPost_idWallPost, 
             group_concat(distinct h.Wall_idWall) all_Wall_idWall
      from wall_has_wallpost h
      join follower f on h.Wall_idWall = f.Wall_idWall and f.User_idUser=1
      group by h.WallPost_idWallPost) v
on w.WallPost_idWallPost = v.WallPost_idWallPost
group by WallPost_idWallPost 
having republish>1;

答案 1 :(得分:0)

如果您在上次选择中只需要Wall_idWall,请尝试此操作

SELECT *, count(idWallHasWallPost) as republish,(select Wall_idWall from follower where User_idUser=1) as Wall_idWall FROM admin_pw.wall_has_wallpost 
where WallPost_idWallPost in 
(select WallPost_idWallPost from wall_has_wallpost where Wall_idWall in 
(select Wall_idWall from follower where User_idUser=1))
group by WallPost_idWallPost 
having republish>1;

或者您可以根据您的要求更改where子句。