Mysql嵌套/多个查询处理

时间:2014-03-08 03:53:16

标签: mysql database join

我有三个查询给我个别正确的结果,但我的要求是我只需要单个查询的所有结果,那我该怎么办?

select * from user_post_like
inner join user_post  on user_post_like.postID = user_post.postID  
inner join Users on Users.userID=user_post_like.userID 
where (user_post.poster='$uid' AND user_post_like.userID!='$uid') 
ORDER BY likeID DESC;

select * from user_post_comment   
inner join user_post  on user_post_comment.postID = user_post.postID  
inner join Users on Users.userID=user_post_comment.commenter 
where (user_post.poster='$uid' AND  user_post_comment.commenter!='$uid') 
ORDER BY commentID DESC;

select * from user_post_share   
inner join user_post  on user_post_share.postID = user_post.postID  
inner join Users on Users.userID=user_post_share.Share_user_id 
where (user_post.poster='$uid' AND   user_post_share.Share_user_id!='$uid') 
ORDER BY shareID DESC;

2 个答案:

答案 0 :(得分:0)

因为无论如何你都要加入表格,你可以在你的选择中添加所有列 - 并保持你的语句可读。如果您有重复的列名(来自不同的表),您可能需要将它们与函数和group by聚合在一起。

SELECT s.*, p.*, u.* 
FROM user_post_share s 
INNER JOIN user_post p ON s.postID = p.postID 
INNER JOIN Users u ON u.userID = p.poster 
WHERE (p.poster='$uid' AND s.Share_user_id != '$uid') 
ORDER BY shareID DESC 

答案 1 :(得分:-3)

尝试像这样的东西

select * from user_post_like,user_post_comment,user_post_share  <inner joins> <where conditions>