[英]SQL Query to get data from one table where a specific column equals value from other table
I have two tables containing the following columns (subset of columns relative to this question) 我有两个包含以下列的表(相对于此问题的列子集)
Name: ORDERS
order_id
order_number
grand_total
Name: PURCHASE_ORDERS
purchase_order_id
order_number
supplier
Basically I am creating a search function that can filter out rows in a DataGridView
based on input and I was wondering how I get a query which would return the following: 基本上,我正在创建一个搜索函数,该函数可以根据输入过滤掉
DataGridView
的行,我想知道如何获取查询,该查询将返回以下内容:
pseudocode example - 伪代码示例-
select * from orders WHERE order_number = (select order_number from purchase_orders where supplier = 'test')
从订单中选择* WHERE订单编号=(从采购订单中选择订单编号,其中供应商=“测试”)
sometimes more than one order number can be returned from the purchase_orders
table so will that influence the above query? 有时,可以从
purchase_orders
表中返回多个订单号,这会影响上述查询吗?
You should use 你应该用
SELECT*
from orders
WHERE
order_number
IN (SELECT
order_number
FROM
purchase_orders
WHERE supplier = 'test')
See: http://www.tutorialspoint.com/mysql/mysql-in-clause.htm 参见: http : //www.tutorialspoint.com/mysql/mysql-in-clause.htm
SELECT
*
FROM
orders
WHERE order_number IN (SELECT order_number FROM purchase_orders WHERE supplier = 'test')
Or Better 或更好
SELECT
*
FROM
orders
LEFT OUTER JOIN purchase_orders ON purchase_orders.order_number = orders.order_number
WHERE
purchase_orders.supplier = 'test'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.