简体   繁体   English

选择比较另一个表中的字段的记录

[英]Select records comparing a field from another table

I have three tables 我有三张桌子

  1. orders 命令
  2. members 成员
  3. products 产品展示

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.

相关问题 比较记录(如果存在于另一个表中) - Comparing records if exist in another table Mysql通过比较另一个表中的值从表中选择 - Mysql select from table by comparing value in another table 从数据库中选择项目与另一个表中的最新日期进行比较 - SELECT items from DB comparing to latest date in another table 从一个表中选择不在另一表中但具有特定条件的记录 - Select records from one table that are not in another table but with specific conditions Laravel根据另一个字段从表中选择随机行 - Laravel select random rows from table based on another field Joomla-使用来自另一个SELECT查询的变量更新表字段 - Joomla - Updating a table field with variable from another SELECT query 从另一个表中选择多个列,其中字段包含数组 - select multiple columns from another table where field contains array 如何在cakephp 3.6中从一个表中选择所有记录,并从另一个表中选择一些记录 - how to select all records from one table and some from another table in cakephp 3.6 如何从一个表中选择数据并从另一表中获取所有相关记录? - How to select data from one table and get all associated records from another table? 从一个表中选择记录,并使用另一表中的列对结果进行排序 - Select records from one table and sort the result using a column from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM