SQL查询返回多条记录

时间:2010-06-30 19:07:27

标签: sql

  

可能重复:
  SQL query with duplicate records

您好。

我正在尝试编写一个查询,以便从Customer表中返回多个订单的行。

目前我正在编写2个查询来解决此问题。无论如何我可以把它放在一个查询中吗?

//Get all customers
select customerID from Customer 

//For each customer
select * from Orders where orderID in
      (select OrderId from Customer where customerID = 123456) 
                and success = 1

这是表结构

Customer table 
----------------------------------------- 
orderID          CustName      CustomerID 
--------------------------------------- 
100               test           123456     
101               test           123456 


Orders table 
------------------------------------ 
pID               OrderID      Success
----------------------------------- 
1                 100            1
2                 101            1

3 个答案:

答案 0 :(得分:1)

select c.CustName, c.CustomerID, o.pID, o.OrderID
from Customer c
inner join Order o on c.orderID = o.OrderID
where o.Success = 1

答案 1 :(得分:0)

select c.orderID, c.CustName, c.CustomerID, o.pID, o.OrderID, o.Success
FROM Customer c, Orders o
WHERE o.OrderID = c.orderID

答案 2 :(得分:0)

试试这个:

SELECT * FROM Orders JOIN Customers ON Orders.OrderID = Customers.orderID