[英]How do I join these 3 tables and make my Select query work?
I have 2 tables: 我有2张桌子:
review_shared: review_shared:
review_shared_id cat_id user_id contact_id
19 5 10276 10275
20 5 10276 10277
21 5 10276 10273
15 6 10279 10277
category: 类别:
cat_id user_id cat_name
3 10278 A
4 10279 B
5 10276 C
6 10279 D
I carry out my query with: 我用以下方法执行查询:
$sql = "
SELECT *
FROM review_shared
JOIN category
ON review_shared.cat_id = category.cat_id
WHERE review_shared.contact_id = ?
";
With review_shared.contact_id
as 10277
, the above would give me: 使用
review_shared.contact_id
为10277
,以上内容将给我:
review_shared. category.
review_shared_id cat_id user_id contact_id cat_id user_id cat_name
20 5 10276 10277 5 10276 C
15 6 10279 10277 6 10279 D
Now I have a third table, contacts
: 现在我有了第三张桌子,
contacts
:
contact_auto_inc user_id contact_id
1 10278 10273
2 10277 10276
3 10261 10285
if the contacts
table has a row with user_id
as 10277
and contact_id
as the user_id
value in review_shared
table then I want to return these values. 如果
contacts
表在review_shared
表中具有user_id
为10277
和contact_id
作为user_id
值的行,那么我想返回这些值。 So in the case above the result would be (with review_shared.contact_id = ?
as 10277
): 因此,在上述情况下,结果将是(带有
review_shared.contact_id = ?
为10277
):
review_shared. category.
review_shared_id cat_id user_id contact_id cat_id user_id cat_name contact_auto_inc user_id contact_id
20 5 10276 10277 5 10276 C 2 10277 10276
This is the statement I am using but it doesn't seem to be be working: 这是我正在使用的语句,但似乎不起作用:
$sql = "
SELECT *
FROM review_shared
JOIN category
ON review_shared.cat_id = category.cat_id
JOIN contacts
ON review_shared.contact_id = contacts.user_id
AND contacts.contact_id = review_shared.user_id
WHERE review_shared.contact_id = ?
";
you have an error on the contact join: 您在联系人加入时出错:
$sql = "
SELECT *
FROM review_shared
JOIN category
ON review_shared.cat_id = category.cat_id
JOIN contacts
ON review_shared.contact_id = contacts.contact_id
WHERE review_shared.contact_id = ?
";
Alternative you can try a equiJoin Like: 或者,您可以尝试一个equiJoin,例如:
SELECT *
FROM review_shared r, category c, contacts p,
WHERE r.cat_id = c.cat_id
AND p.contact_id = r.user_id
AND r.contact_id = ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.