如果主表有2个主键,如何用两个表增强子查询以使用3个表进行搜索?

问题描述 投票:0回答:3

主表是“M_Order”,其中包含订单及其详细信息

M_order
row_id |Order_Num | Order_Status | created     | created_by | Account_ID
--------------------------------------------------------------
1-a    | 1        | complete     | 2016-08-25  |    jhon    | 3-a
1-b    | 2        | pending      | 2016-08-26  |    mike    | 3-b
1-c    | 3        | failed       | 2015-08-27  |    mike    | 3-c
1-d    | 4        | pending      | 2015-08-25  |    jhon    | 3-d
1-e    | 5        | failed       | 2016-08-26  |    mike    | 3-e
1-f    | 6        | New          | 2017-03-27  |    jhon    | 3-f

第二个表是“Order_itmes”,其中包含每个订单的产品列表

Order_itmes
row_id |Order_id | Product_ID
--------------------------------------------------------------
2-a    | 1-a     |  Prod_1    
2-b    | 1-b     |  Prod_2   
2-c    | 1-c     |  Prod_1 
2-d    | 1-d     |  Prod_1    
2-e    | 1-e     |  Prod_1   
2-f    | 1-f     |  Prod_3  

“M_order”表中的 row_id 是唯一的,并且在表“Order_items”中可用作 Order_id

我有一个查询,显示来自“Order_items”的具有特定product_id的订单

Select Order_Num,Order_status,created ,created_by
  from M_order where row_id in (select Order_id from Order_itmes where 
Product_ID='Prod_1')
  where Order_Status='Failed'
     Order By created desc

如果我有第三个表“Accounts”,其 row_id 列与“M_Order”的 account_id 中的列相同

 Accounts
 row_id  | Name 
--------------------------------------------------------------
 3-a     | cutomer1
 3-b     | cutomer2
 3-c     | cutomer3
 3-d     | cutomer4
 3-e     | cutomer5
 3-f     | cutomer6

如何增强上述查询以在结果中列出客户名称并显示如下

Order_Num | Order_status | created   | created_by  |  name
-------------------------------------------------------------   
5         | failed       |2016-08-26 | mike        | customer5
3         | failed       |2015-08-27 | mike        | customer3
mysql sql sql-server subquery
3个回答
0
投票

我相信在这种情况下最好使用

JOIN
,因为一旦我们有两个以上的表,子查询就会变得复杂。在这里可以使用简单的
JOIN
,例如:

SELECT o.order_num, o.order_status, o.created, o.created_by, a.name
FROM m_order o JOIN order_items oi ON o.row_id = oi.order_id
JOIN accounts a ON o.account_id = a.row_id
WHERE oi.product_id = 'Prod_1';

0
投票

您可以将

join
添加到
Accounts

select o.Order_Num, o.Order_status, o.created, o.created_by, a.name
from M_order o
  inner join Accounts a
    on o.Account_ID = a.row_id
where o.row_id in (
    select Order_id 
    from Order_items 
    where Product_ID='Prod_1'
    )
  and o.Order_Status='Failed'
order by o.created desc

0
投票

桌子怎么可以2人PK?

我不会评论索引,因为您发布了真实的表结构和真实的数据类型。

ID 1-a、2-a 或 3-a 的用途是什么?

表应自动递增 id 作为 CI。

此外,订单表包含数百万条记录,因此您无法在不使用分页的情况下简单地查询任何内容。

试试这个,

declare @M_order table(row_id varchar(30),Order_Num int,Order_Status varchar(30)
,created date,created_by varchar(30),Account_ID varchar(30))

insert into @M_order 
values 
('1-a',1,'complete','2016-08-25','jhon','3-a')
,('1-b',2,'pending','2016-08-26','mike','3-b')
,('1-c',3,'failed','2015-08-27','mike','3-c')
,('1-d',4,'pending','2015-08-25','jhon','3-d')
,('1-e',5,'failed','2016-08-26','mike','3-e')
,('1-f',6,'New ','2017-03-27','jhon','3-f')

declare @Order_itmes table(row_id varchar(30),Order_id varchar(30), Product_ID varchar(30))

insert into @Order_itmes 
values 
('2-a','1-a','Prod_1'),('2-b','1-b','Prod_2'),('2-c','1-c','Prod_1') 
,('2-d','1-d','Prod_1'),('2-e','1-e','Prod_1'),('2-f','1-f','Prod_3') 

declare @Accounts table(row_id varchar(30),Name varchar(30)) 

insert into @Accounts 
values
 ('3-a','cutomer1') ,('3-b','cutomer2') ,('3-c','cutomer3')
 ,('3-d','cutomer4') ,('3-e','cutomer5') ,('3-f','cutomer6')


 select 
     mo.Order_Num ,mo.Order_status, mo.created, mo.created_by, name
 from 
     @M_order MO
 inner join 
     @Accounts A on mo.Account_ID = a.row_id
 where 
     Order_status = 'failed'
     and exists (select Order_id from @Order_itmes OI 
                 where Product_ID = 'Prod_1' and oi.Order_id = mo.row_id)
© www.soinside.com 2019 - 2024. All rights reserved.