简体   繁体   English

SQL查询以从一个表中获取数据,其中特定列等于另一表中的值

[英]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.

相关问题 MySQL查询从1个表中获取行以及从其他表中获取where子句的特定列值 - Mysql query to get row from 1 table and specific column value with where clause from other table SQL查询从一个表中选择,其中不在另一个表中或在该表中具有特定值 - SQL query to select from one table where either not in another table OR in that table with a specific value 在SQL中,从表a中选择全部,其中列a的值等于表b中最新条目的值? - In SQL, select all from table a where column a value equals the value of the most recent entry in table b? 如果一个表列为空,如何从另一表获取值 - How to get the value from the other table if one table column is empty 从mysql表中删除WHERE列等于值AND列2等于value2 - DELETE from mysql table WHERE column equals value AND column2 equals value2 SQL-需要将数据从一个表插入到另一个中,其中1个字段等于另一个 - SQL - Need to insert data from one table into another where 1 field equals another 从2个表中获取数据,其中一个表与另一个表相关 - Get data from 2 tables where one table is dependent of the other 使用“特殊”WHERE子句从一个SQL查询中的表中选择数据 - Selecting data from a table in one sql query with 'special' WHERE clause SQL查询帮助:如何从其他表中获取列值? - SQL query help: How can i get the column value from other table? 从一个表中获取值,然后将其插入另一个表中。 如何? - Get value from one table, insert it in query of other. How to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM