如果地址值不等于默认值,则MySQL加入

时间:2017-10-07 11:32:07

标签: php mysql

数据:

[ID] => 4
[Create_date] => 2017-10-07
[Client] => 17
[Address] => default
[GST] => Regular
[Rate_group] => D
[Sales_man] => 0
[CGST] => 91.12
[SGST] => 91.12
[IGST] => 0
[Discount] => 10
[Total] => 1397.25
[LR_number] => 0
[LR_date] => 0000-00-00
[Transport] => 5
[Transport_branch] => default
[Remark] => Hi, my name is Rushabh Shah.
[Payment_mode] => Cash
[Status] => Open
[Customer_name] => Rushabh Infotech
[Transport_name] => Shah Infotech

MYSQL查询:

SELECT `sale`.*, 
       `customer`.`english_company_name`  AS `Customer_name`, 
       `transport`.`english_company_name` AS `Transport_name` 
FROM   `sale` 
       LEFT JOIN `customer` 
              ON `customer`.`id` = `sale`.`client` 
       LEFT JOIN `transport` 
              ON `transport`.`id` = `sale`.`transport`

如果Transport_branch,MySQL加入Transport_branch != 'default'。任何人都可以告诉我如何实现这一目标。

1 个答案:

答案 0 :(得分:0)

时可以使用案例
SELECT `sale`.*, 
       `customer`.`english_company_name`  AS `Customer_name`, 
       `transport`.`english_company_name` AS `Transport_name` ,
        case when `transport`.`transport_branch` = 'default' 
                    THEN  `transport`.`english_company_name`
                    ELSE `transport_brach`.`transport_btanch__company_name`
        end AS my_transport_name
FROM   `sale` 
       LEFT JOIN `customer` 
              ON `customer`.`id` = `sale`.`client` 
       LEFT JOIN `transport` 
              ON `transport`.`id` = `sale`.`transport`
       LEFT JOIN `transport_branch` 
           ON `transport_branch`.`id` = `transport`.`id`