0

別のテーブルから呼び出すときに、いくつかの SQL クエリに苦労しています。

2 つのテーブルから結果を取得しようとしています。この場合、user.id=1 から値を取得し、user.id=1 がフォローしているユーザーの値を取得したいと考えています。

しかし、常に同じfollow_idを持つすべてのユーザーまたはuser.id = 1の結果のみが得られます。

このようなことを考えていましたが、このクエリは空の結果を返します。

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 following.follow_id
WHERE user.id = following.follow_id AND user.id = 1

テーブル。次の ID | ユーザー ID | follow_id

Tweets
user_id|id|date|message

user
id|email|password|username

userdetails
id|firstname|lastname|profile_img|user_id|about
4

2 に答える 2

1
SELECT
user.email, user.username, tweets.message, tweets.date, userdetails.profile_img,userdetails.firstname, userdetails.lastname, '' as id, '' as user_id, '' as follow_id
FROM 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 = 1

このクエリは、ユーザー 1 からのすべてのツイートを返します。また、ユーザー 1 をフォローしているユーザーからのすべてのツイートも得られます。

于 2013-04-21T15:20:16.393 に答える