理由:
私は2つのselectクエリに参加しようとしていますが、私はどんな結果を得ることができません。これらのクエリを結合するために私を助けてください。
最初のクエリ==>
SELECT *,all_posts.id as lid,all_posts.image as lpimage, users.image as uimage,all_posts.post_for_id as typ_id FROM all_posts LEFT JOIN users on all_posts.user_id = users.id WHERE all_posts.parent_id = 0 order by lid desc limit 5;
このクエリは、「アクセス」フィールド=>公開、友人、NULLにかかわらず、すべての投稿を返します
クリックしてここに結果のために
サンプルデータ===== all_postsテーブル
id user_id image post_type post_date access
== ======== ===== ======= ========= =====
4526 1663 undefined image 2020-03-07 06:47:04 Public
4523 196 undefined image 2020-03-07 06:47:04 Friends
ユーザーテーブル=>
id name
== ========
1663 test1
196 test2
1966 test3
フレンズ表 -
id from_id to_id add_date
== ======== ===== =======
316 196 1966 2019-10-15
第二に、クエリ==>
SELECT count(*) as isfriend from friend where (from_id=:from_id and to_id=:user_id) or (from_id=:user_id and to_id=:from_id)
現在のユーザーのに基づいて、このクエリで1または0を返すことにより、他のユーザー(私の場合は、Query1を上記の結果から、各user_idをして確認したい)の友人です。クリックしてここに結果のために私のクエリ==>
SELECT *,all_posts.id as lid,all_posts.image as lpimage, users.image as uimage,all_posts.post_for_id as typ_id FROM all_posts LEFT JOIN users on all_posts.user_id = users.id WHERE user.id = 1(SELECT count(*) as isfriend from friend where (from_id=:from_id and to_id=:user_id) or (from_id=:user_id and to_id=:from_id)) all_posts.parent_id = 0 order by lid desc limit 5;
私は、私も最初のクエリからのすべての公共の記事「フレンズ」と「アクセス」を持ち、USER_IDは、現在のユーザーの友人でポストを得ることができ、問合せをしたいです。私はこれらの2つのクエリを結合しなければならないことを意味は、これを取得します。私はこの権利を取得するために助けてください。ありがとうございました。
恋人誰:
私は、これは正しい方向かもしれないと思います:
SELECT *,all_posts.id as lid,all_posts.image as lpimage, users.image as uimage,all_posts.post_for_id as typ_id FROM all_posts LEFT JOIN users on all_posts.user_id = users.id WHERE all_posts.parent_id = 0 AND (all_posts.access = 'Public' OR (all_posts.access = 'Friends' AND 0 < (SELECT count(*) as isfriend from friend where (from_id=all_posts.user_id and to_id=users.id) or (from_id=users.id and to_id=all_posts.user_id))) ORDER BY lid DESC LIMIt 5;