Required single query to fetch data from tables

一曲冷凌霜 提交于 2019-12-01 08:12:23

1)all details from user_message

SELECT * FROM user_messages WHERE userid = <userID> AND messageid = <messageID>;

2)last 10 comments related to messageid in ascending order from comments table (one message can have multiple comments)which includes comment_id ,comment, comment_date,and details of commented_by(fname,lname,small_pic_path).

SELECT a.comment_id, a.comment, a.comment_date, b.fname || b.lname || c.small_pic_path "Commented by" 
FROM comments a, smusers b, profile_pic c, user_messages d
WHERE d.messageid = <messageID>
AND d.userid = b.id
AND b.id = c.userid
ORDER BY comment_date
LIMIT 0, 10;

3)all small_pic_path from post_images(one message can have multiple images),

SELECT small_pic_path
FROM post_images;

4)total likes from like table,

SELECT * FROM likes;

5)all details (smsusers.*,profile_pic.*) of sentby

You have not posted the structure of sentby

4) total likes from like table,

SELECT count(*) AS total_likes FROM likes WHERE element_id = <messageID>;

5) all details (smsusers.*,profile_pic.*) of sentby

SELECT smsusers.*,profile_pic.* FROM user_messages
    JOIN smsusers 
        ON user_messages.sentby = smsusers.id
    JOIN profile_pic.userid = smsusers.id
WHERE user_messages = <messageID>

Now join them all in one query

SELECT (<Query_1>),(<Query_2>),[...],(<Query_N>)

You seem to have got the answer. Let me know how was that and if you need anything else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!