如何将sql查询合并为一个

时间:2016-07-05 06:01:37

标签: mysql select

我从以下2个不同的表中得到以下查询:

SELECT `TRANSPORTATION_NAME_OF_TRANSPORTATION`, `PARTNER_CODE`, `INCOTERM_NAME`,
    `SHIPPING_DATE`, `SHIPPING_ETA_TO_PORT_AIR_DATE`, `TOTAL_WEIGHT`, `ORDER_DESCRIPTION`,
    `MORE_DESCRIPTION`, `UNIT_OF_MEASURE`, `QUANTITY`,`PERFORMANCE_REMARKS`, `AWB_DATE`, `FFF_NAME`
FROM INBOUND_ORDER
WHERE `AWB_NUM` ='999129456'

SELECT `PARTNER_NAME`
FROM `PARTNER`
WHERE `PARTNER_CODE` ='$partnercode' 

我从第一个查询中获取合作伙伴代码,并希望在第二个查询中使用它,但是在一个单独的sql语句中。

表结构如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用嵌套查询

SELECT `PARTNER_NAME`
FROM `PARTNER`
WHERE `PARTNER_CODE` = (SELECT `PARTNER_CODE`
                        FROM INBOUND_ORDER
                        WHERE `AWB_NUM` ='999129456')

或内部联接

SELECT p.`PARTNER_NAME`
FROM INBOUND_ORDER o
INNER JOIN `PARTNER` p ON p.`PARTNER_CODE` = o.`PARTNER_CODE`
WHERE o.`AWB_NUM` = '999129456'