3

私はかなり頭がおかしいので、前に間違った質問をしました。今、これが私が達成しようとしていることです。

user.id = 1フォローしているユーザーの値と値を取得しようとしていuser.id = 1ます。

しかし、私はエーテルから結果を得るuser.id 1か、この場合は次のfollowing.follow_id = 2. 何か案は ?

表示用の SQLfiddle を作成しました。ご覧のとおり、得られる唯一の結果は user_one からのものです。

http://sqlfiddle.com/#!2/6a39f/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
LEFT JOIN following ON user.id = 1
WHERE following.user_id = 1 

テーブル:

つぶやき

ユーザー ID | ID | 日付 | メッセージ

ユーザー

ID | 電子メール | パスワード | ユーザー名

ユーザー詳細

ID | ファーストネーム | 姓 | プロフィール画像 | ユーザー ID | 約

続く

ID | ユーザー ID | follow_id

この場合、次の表に値があります

 user_id = 1 and follow_id = 2

まとめ 。

User_id = 1 is following follow_id = 2

しかし、私user_id = 1は両方からではなく、結果のみを取得します。ありがとう。

基本的に、この 2 つのクエリを結合しようとしています。

フォローしているユーザーから結果を取得するためのクエリ。

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 following.follow_id = tweets.user_id AND following.user_id  = '1' ORDER BY tweets.date DESC 

そして、ユーザー 1 の値を取得するためのクエリ

SELECT user.email, 
user.username, 
tweets.message, 
tweets.date, 
userdetails.profile_img, 
userdetails.firstname, 
userdetails.lastname
FROM user
JOIN userdetails ON user.id = userdetails.user_id
JOIN tweets ON userdetails.user_id = tweets.user_id
WHERE user.id = '1'  ORDER BY tweets.date DESC
4

3 に答える 3

2

ID が 1 であるか、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
WHERE user.id = 1
  OR user_id IN
      (
       SELECT follow_id
       FROM following
       WHERE user.id = following.user_id
       )
于 2013-04-21T17:04:04.293 に答える
1

これは私の(わずかに)変更されたバージョンbobsの回答です(サブクエリを追加LEFT OUTER JOINおよび変更しました):

SELECT user.id,
  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
LEFT JOIN following ON user.id = following.user_id 
JOIN userdetails ON user.id = userdetails.user_id
JOIN tweets ON userdetails.user_id = tweets.user_id
WHERE user.id=1 OR 
                user.id IN (SELECT follow_id 
                            FROM following 
                            WHERE following.user_id = 1);

3 番目のユーザーが追加されたSQL Fiddleを次に示します。実際のデータセットで機能するかどうかを確認してください。

于 2013-04-21T21:28:44.797 に答える