LEFT OUTER JOIN EQUIVALENT

孤街浪徒 提交于 2019-12-01 22:08:20

问题


I have a tables contains null values. In ORDER table i have 2 null in PART_ID section and 2 null values in CUSTOMER_ID.

And i have that kind of query:

SELECT O.ORDER_ID , O.ORDER_DATE , O.CUST_ID, O.QUANTITY ,O.PART_ID ,
       C.CUST_NAME, C.CUST_CODE, P.PART_NAME, P.PART_CODE
FROM [ORDER] O
     LEFT OUTER JOIN PART P ON P.PART_ID = O.PART_ID
     LEFT OUTER JOIN CUSTOMER C ON C.CUST_ID = O.CUST_ID 

So here is my question. How can i do it without using outer join ? I tried too many things including where not exists or this ;

SELECT *
FROM [ORDER] O ,CUSTOMER C, PART P
WHERE C.CUST_ID = (
  SELECT CUST_ID FROM CUSTOMER C WHERE O.CUST_ID = C.CUST_ID
) AND P.PART_ID = (SELECT PART_ID FROM PART P WHERE O.PART_ID = P.PART_ID) 

but i couldn't find solution. If there is a solution how it will be ?

(Note: this is homework.)

I have that kind of table :

and left outer join gives that :

the hw said do it without using outer join and get same table as left outer join gives. But like a said i coulnd't. I'm also using MSSQL.


回答1:


Outer join produces super-set over inner join. Indeed, from Wikipedia: A left outer join returns all the values from an inner join plus all values in the left table that do not match to the right table.

So to model left outer join using inner join one could use UNION of inner join SELECT between same tables with same join condition and another SELECT from 1st table that returns all rows without a match from the right table (I reduced your case to a single left join):

SELECT O.ORDER_ID , O.ORDER_DATE , O.CUST_ID, O.QUANTITY ,O.PART_ID ,
       P.PART_NAME, P.PART_CODE
FROM [ORDER] O JOIN PART P ON P.PART_ID = O.PART_ID
UNION
SELECT O.ORDER_ID , O.ORDER_DATE , O.CUST_ID, O.QUANTITY ,O.PART_ID ,
       NULL, NULL 
FROM [ORDER] O
WHERE NOT EXISTS (SELECT 'found' FROM PART P WHERE P.PART_ID = O.PART_ID)



回答2:


Presumably, you want to get matches to the columns with NULL values, instead of having them fail. If so, just modify the join conditions:

FROM [ORDER] O
     LEFT OUTER JOIN PART P
     ON P.PART_ID = O.PART_ID or (p.Part_id is NULL and o.Part_id is null)
     LEFT OUTER JOIN CUSTOMER C
     ON C.CUST_ID = O.CUST_ID or (c.cust_id is null and o.cust_id is null)

The major issue with this approach is that many (most?) SQL engines will not use indexes for the join.




回答3:


Is there a specific reason why you don't want to use outer join? Isn't this the result you want? :

FROM [ORDER] O 
LEFT JOIN PART P 
ON P.PART_ID = O.PART_ID and P.PARTID is not null
LEFT JOIN CUSTOMER C 
ON C.CUST_ID = O.CUST_ID and C.CUSTID is not null



回答4:


So complete answer should be like this (after my teacher gave results) :

 SELECT O.ORDER_ID,O.ORDER_DATE,O.CUST_ID,
(SELECT C.CUST_CODE FROM CUSTOMER C WHERE C.CUST_ID=O.CUST_ID) AS CUST_CODE,
(SELECT C.CUST_NAME FROM CUSTOMER C WHERE C.CUST_ID=O.CUST_ID) AS CUST_NAME,
O.PART_ID,
(SELECT P.PART_CODE FROM PART P WHERE P.PART_ID = O.PART_ID ) AS PART_CODE,
(SELECT P.PART_NAME FROM PART P WHERE P.PART_ID = O.PART_ID ) AS PART_NAME,
O.QUANTITY

FROM [ORDER] O


来源:https://stackoverflow.com/questions/15572333/left-outer-join-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!