'if statement inside mysql query to determine table to select
I would like to perform a if statment in the middle of a query, I need to find out if oc.product_type == 'clothing' or 'other' If it is clothing, then i need to select the specific_clothing table instead of the non_clothing table? I really lost with this one
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,
//here is where im trying to use if
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';
Solution 1:[1]
I think you are just looking at adding an outer join to specific_clothing
. Something like this:
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';
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |