如何连接第二个表有条件的两个表

时间:2014-12-03 08:14:34

标签: sql oracle

我有两张表,比如

shopping_cart_tablecustomer_table

shopping_cart_table包含字段shoppingcart_id | home_No|

customer_table包含字段shoppingcart_id | status| customer_type|

我希望从home_No

获取shopping_cart_table WHERE (customer_table.status is null OR customer_table.status='Y') AND customer_table.customer_type='X'

这两个表都可以从shoppingcart_id

加入

3 个答案:

答案 0 :(得分:1)

实际上这只是基本的。 你可以使用join&把条件放在哪里。

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'

答案 1 :(得分: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'

答案 2 :(得分: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'not'被错误地放在那里