2013-06-06 37 views
0

我想執行一個如果在查詢的中間顯示,我需要找出是否oc.product_type =='clothing'或'other'如果是是服裝,那麼我需要選擇特定服裝表而不是非服裝表?我真的有這個if語句裏面的mysql查詢來確定表的選擇

SELECT o.total, o.shipping, o.order_date 
     ,oc.product_type, oc.product_id, oc.quantity, oc.price_per 
     ,cu.first_name, cu.last_name, CONCAT(cu.address1, cu.address2) AS address, 

//丟在這裏就是我嘗試如果

if(oc.product_type = 'clothing' 
     SELECT style 
     FROM specific_clothing 
     where oc.product_id = specific_clothing_id) as item 

    FROM order_contents AS oc 
    INNER JOIN `orders` as o ON oc.order_id = o.id 
    INNER JOIN `customers` AS cu ON o.customer_id=cu.id 
    WHERE o.customer_id = '214'; 

回答

1

我認爲你只是在尋找增加一個外部聯接specific_clothing使用。事情是這樣的:

SELECT o.total, o.shipping, o.order_date 
     ,oc.product_type, oc.product_id, oc.quantity, oc.price_per 
     ,cu.first_name, cu.last_name, CONCAT(cu.address1, cu.address2) AS address 
     ,sc.style AS item 

    FROM order_contents AS oc 
    INNER JOIN `orders` as o ON oc.order_id = o.id 
    INNER JOIN `customers` AS cu ON o.customer_id=cu.id 
    LEFT OUTER JOIN specific_clothing sc ON (
     oc.product_id = sc.specific_clothing_id AND 
     oc.product_type = 'clothing') 
    WHERE o.customer_id = '214'; 
+0

即時得到一個語法錯誤......我做了這是正確的選擇o.total,o.shipping,o.order_date ,oc.product_type,oc.product_id,oc.quantity,OC .price_per ,cu.first_name,cu.last_name,CONCAT(cu.address1,cu.address2)AS地址,sc.style AS項目 FROM order_contents AS oc INNER JOIN'orders' as o ON oc.order_id = o .id INNER JOIN'customers' as cu ON o.customer_id = cu.id OUTER JOIN specific_clothing AS sc ON( oc.product_id = sc.specific_clothing_id AND oc.product_type ='clothing') WHERE o.customer_id ='214'; – Brett

+0

只是看着它,它看起來沒問題。具體的錯誤是什麼?你可以嘗試一次向後註釋1個表,直到它工作,然後你知道在哪裏集中(即如果你取出外部連接部分(和select子句中的相關「sc」列),它會運行嗎? – Glenn

+0

我得到它與左外連接的工作,但如果oc.product_id =其他我需要選擇一個不同的表..我需要一個如果說明? – Brett