我想在查询中间执行if语句,我需要找出oc.product_type =='clothing'或'other'如果是服装,那么我需要选择specific_clothing表而不是non_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,
//这里是即时通讯尝试使用的地方
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';
最佳答案
我认为您只是在向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';
关于mysql - mysql查询中的if语句以确定要选择的表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16952750/