在mysql中组合依赖查询

时间:2013-11-01 14:51:31

标签: php mysql sql

我有两个单独的查询。

1)

SELECT Email, UserId, FirstName, LastName FROM user WHERE 1;

2)

      SELECT COUNT(PostId) as userTextCount
        FROM posts 
  INNER JOIN user ON user.UserID =  posts.PostedAsId
       WHERE UserId='$uid'
         AND PostedAs='USER'
         AND PostType='text';

我想将这两个查询合二为一。问题是在第二个查询中有一个变量callsed $ uid,我将在运行第一个查询后得到它。我们可以这样做,以便我们不需要该变量并组合这两个查询。如果您需要更多说明,请与我联系。

感谢。

1 个答案:

答案 0 :(得分:1)

SELECT Email, UserID, FirstName, LastName, count(*) as userTextCount
FROM user INNER JOIN posts ON posts.PostedAsId = user.UserID and PostedAs = 'USER'
  and PostType = 'text'
WHERE 1
GROUP BY Email, UserID, FirstName, LastName