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

标签 mysql

我想在查询中间执行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

  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/

相关文章:

Mysql TRIGGER 更新时不获取函数返回值

php - 在 foreach 循环中使用 SQL 结果

php - 简单的 HTML DOM 解析器和 Web 浏览器返回不同的 HTML

php - 如何使用 php mysql 识别数据库中未保存的值

带连接和条件语句的 MySql SELECT

mysql - 查找客户的所有记录,其中所有记录在价格字段中都有 NULL 值

php - MySQL 列插入 PHP 数组错误

MYSQL选择代表不同工作室的销售人员(不重复)

java - 如何使用 facebook 登录并获取用户信息以从 android 应用程序发送我的远程数据库

php - 仅显示数据库中的三个连续值