users
、contacts
、およびの3 つのテーブルがありgroups
ます。ユーザーのすべての連絡先を検索し、選択した連絡先からgroup_id
、groups
テーブルに特定の連絡先があるそのユーザーの連絡先を除外したいと考えています。
私groups
のテーブルは次のように構成されています。
id (primary key)
group_id (a foreign key to a table contain general group info)
user_id (a foreign key to the users table)
私contacts
のテーブルは次のように構成されています。
id (primary key)
user_id (a foreign key to the `users` table of the user who added the contact)
contact_id (a foreign key to the `users` table of the added contact)
現在、機能していないクエリは次のとおりです。
"""SELECT c.*, u.*
FROM contacts c
LEFT JOIN groups g ON c.contact_id = g.user_id
INNER JOIN users u on u.id = c.contact_id
WHERE c.user_id = %s AND
<not sure what further constraints to place on query>""", (user_id, group_id)
私の理解では、は確かに正しくありません。それが正しくないことを考えると、句LEFT JOIN
にこれ以上の制約をまだ追加していません。WHERE
これを達成するための最良の方法は何ですか?ありがとうございました。