[英]Select records comparing a field from another table
I have three tables 我有三张桌子
In orders, I have fields id, mem_id, date, prod_id, status
where mem_id coming from members
table and prod_id is coming from products
table 在订单中,我有字段id, mem_id, date, prod_id, status
,其中mem_id来自members
表,而prod_id来自products
表
In members, I have fields mem_id, name, phone, address, city, state, zip, country
where country holds id of country from country
table 在成员中,我有字段mem_id, name, phone, address, city, state, zip, country
,国家/地区持有国家/ country
表中国家/ country
ID
Now, I want to show the records from orders
table only for product id 2 and from members
from country id 25 现在,我只想显示产品ID为2的orders
表中的记录以及国家ID为25的members
的记录
I have tried doing: 我试着做:
SELECT o.mem_id, o.prod_id, m.mem_id FROM orders o INNER JOIN members m ON m.mem_id = (SELECT mem_id FROM members WHERE country=25) WHERE o.prod_id=2
But it gives: 但是它给出:
Fatal error: Call to a member function fetch_assoc() on a non-object in
So, its not fetching any data and a problem in my query. 因此,它没有获取任何数据和查询中的问题。 Please suggest me, Thanks 请建议我,谢谢
Join the table using ON condition
and apply the where condition
like this 使用ON condition
连接表并应用where condition
如下所示
SELECT o.mem_id, o.prod_id, m.mem_id
FROM orders o
INNER JOIN members m
ON m.mem_id = o.mem_id
WHERE o.prod_id=2 and m.country=25
You can JOIN
the tables on column and specify the condition in WHERE
clause, eg: 您可以在列上JOIN
表,并在WHERE
子句中指定条件,例如:
SELECT o.mem_id, o.prod_id, m.mem_id
FROM orders o JOIN members m ON o.mem_id = m.men_id
WHERE o.prod_id = 2 AND m.country = 25;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.