9

Mysql Nested / Multiple Query handling

 3 years ago
source link: https://www.codesd.com/item/mysql-nested-multiple-query-handling.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Mysql Nested / Multiple Query handling

advertisements

I have three queries gives me result individually correct but my requirement is i need all result in single query only so how should i proceed?

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;


Since you're joining the tables anyway, you can put columns from all in your select - and keep your statement readable. If you have duplicate column names (from different tables) you may need to aggregate them with functions and 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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK