if语句在mysql查询中确定要选择的表

时间:2013-06-06 02:23:42

标签: mysql mysqli

我想在查询中间执行 if statment ,我需要找出oc.product_type =='服装'或'其他'如果是衣服,那么我需要选择 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';

1 个答案:

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