如何编写一个mysql子查询,其中一个表根据返回一行的表返回多行?

时间:2012-03-07 06:49:30

标签: mysql select subquery

假设我有一个这样的表:

**tbl_purchase**
PurchaseID
Date
Supplier

**tbl_purchaseitem**
PurchaseItemID
PurchaseID

我想创建一个查询,它将返回如下内容:

**Header**
PurchaseID = 2
Date: 4/26/12
Supplier:stackoverflow

*Items*
PurchaseItemID:3
PurchaseID:2

PurchaseItemID:4
PurchaseID:2

PurchaseItemID:5
PurchaseID:2

先生/女士,您的回答将会非常有用,非常感谢。

2 个答案:

答案 0 :(得分:1)

Select * from tbl_purchase 
inner join 
tbl_purchaseitem
on 
tbl_purchase.PurchaseID = tbl_purchaseitem.PurchaseID
where 
PurchaseID = 2 
and 
Date = '4/26/12' 
and 
Supplier = 'stackoverflow'

答案 1 :(得分:1)

像这样:

SELECT pi.*
FROM tbl_purchase p
INNER JOIN tbl_purchaseitem pi
     ON p.PurchaseID = pi.PurchaseID
WHERE p.PurchaseID = 2 AND
      p.Date       = '2012-04-26' AND
      p.Supplier   = 'stackoverflow'