Fetch Values From Different Tables With Same Id
I'm struggling with some the sql querys when calling from different tables. Im trying to get some results from 2 tables. In this case, i would like to get the values from user.id=1
Solution 1:
SELECT
user.email, user.username, tweets.message, tweets.date, userdetails.profile_img,userdetails.firstname, userdetails.lastname, '' as id, '' as user_id, '' as follow_idFROM user
JOIN userdetails ON user.id = userdetails.user_id
JOIN tweets ON userdetails.user_id = tweets.user_id
WHERE user.id = 1
UNION ALL
SELECT
user.email, user.username, tweets.message, tweets.date, userdetails.profile_img,userdetails.firstname, userdetails.lastname, following.id, following.user_id, following.follow_id
FROM user
JOIN userdetails ON user.id = userdetails.user_id
JOIN tweets ON userdetails.user_id = tweets.user_id
JOIN following ON user.id = following.user_id and following.follow_id = 1This query will give all tweets from user 1. And all tweets from users that follow user 1 i belive.
Solution 2:
If this were my problem, I would start my troubleshooting with this query:
selectcount(*)
fromuserwhere user.id =1If that returned a number greater than 0, I would then run this:
selectcount(*)
fromuser u innerjoin userdetails ud on u.id = ud.id
where u.id =1Then I would continue to add the other bits one at a time until the query returned 0. When it did, I'd know why.
Post a Comment for "Fetch Values From Different Tables With Same Id"