简体   繁体   中英

How can I query data from multi tables in a single query, including “like” data based on user login

I have three tables for streaming data from database

stream_master
    stream_id
    user_id -> foreign Key
    stream_title
    stream_content

user
    user_id
    user_name
    password


stream_likes
    stream_like_id
    stream_id
    user_id

I can join together to get data stream, but I want to get "stream_likes" data for each "stream_master" rows, based on user(Logged in) activity. Mean if user already like stream get 1 for each row

Is this possible to execute in single query?

my output should be:

[stream_id => 10,
user_id => 10015,
stream_title=>"title of stream",
stream_content="stream content",
stream_like=>1],
[stream_id => 10,
user_id => 10018,
stream_title=>"title of stream222",
stream_content="stream content22",
stream_like=>0]

I am not able to find a nice solution for this. Could someone can help me on this?

Thanks

Peter

SELECT m.*, IF(l.stream_like_id IS NOT NULL, 1, 0) AS stream_like
FROM stream_master m
LEFT JOIN stream_likes l
ON m.stream_id = l.stream_id AND m.user_id = l.user_id

Would that work for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM