2014-12-03 47 views
-1

我有兩個表像如何加入兩個表,其中第二表有condtion

shopping_cart_tablecustomer_table

shopping_cart_table有場shoppingcart_id | home_No|

customer_table有場shoppingcart_id | status| customer_type|

我想要得到的home_No from shopping_cart_table WHERE (customer_table.status is null OR customer_table.status='Y') AND customer_table.customer_type='X'

這兩個表都可以加入shoppingcart_id

+0

您的具體問題在哪裏? – 2014-12-03 08:18:37

+0

問題是如何做到這一點 – user2771655 2014-12-03 08:19:45

+1

對不起,如果這聽起來很刺耳,但這確實是非常基本的SQL東西。我建議你抓住一個SQL教程,並通過它。 – 2014-12-03 08:27:13

回答

1

其實這只是基本的。 你可以使用加入&放在哪裏條件。

Select a.home_No 
    from shopping_cart_table as a 
inner join customer_table as b 
    on a.shoppingcart_id = b.shoppingcart_id 
where nvl(b.status,'Y') = 'Y' 
    and customer_type='X' 
+0

不應該是'b.status IS NOT NULL'而不是'b。 status ='Y'' - 'Y'不爲空...但是NOT NULL不是'Y' – Captain 2014-12-03 08:20:52

+0

@ AK47這不起作用。你也錯過了一個條件。 – user2771655 2014-12-03 08:21:09

+0

已更新。對不起應該是b.status爲空 – user2771655 2014-12-03 08:22:29

0

你可以試試這個查詢:

SELECT sct.home_no 
FROM shopping_cart_table AS sct 
, customer_table AS ct 
WHERE sct.shoppingcart_id = ct.shoppingcart_id 
AND (
    ct.status IS NOT NULL 
    OR ct.status = 'Y') 
AND ct.customer_type = 'X' 
+0

非ANSI語法?那是過時的伴侶。請看看這裏http://stackoverflow.com/questions/1599050/ansi-vs-non-ansi-sql-join-syntax – AK47 2014-12-03 08:26:02

+0

@Thomas Van Driessche這返回0行。 – user2771655 2014-12-03 08:31:31

0
select home_No 
from shopping_cart_table, customer_table 
WHERE shopping_cart_table.shoppingcart_id = customer_table.shoppingcart_id 
AND(customer_table.status is not null OR customer_table.status='Y') AND  
customer_table.customer_type='X' 

但是,這個條件看起來有點怪:

(customer_table.status is not null OR customer_table.status='Y') 

也許你會想改變它

nvl(customer_table.status,'Y')='Y' 

aqssuming'不'放在那裏由於錯誤

+0

嗨,是的,你是對的。它應該是null。如果我使用你的查詢,它不會返回任何東西。 – user2771655 2014-12-03 09:12:46

相關問題