原始查询 - MYSQL(如果在SQL内部进行)

时间:2018-02-13 06:35:02

标签: php mysql sql laravel laravel-5

我试图执行收集所有数据的查询。
因此,我的条件是if client_type行的corporation field查询将获取fullname的数据,并将其分配给elsefirstname它会获取lastnamemiddlenamefullname并将其指定为 (SELECT SA.id,SA.sa_code,S.importer_name as name, CONCAT_WS(" ",(IF(C.client_type = 'Corporation',C.corporation), C.firstname,' ',C.middlename,' ',C.lastname)) as fullname, C.client_type,C.corporation, CI.invoice_code,CI.created_at as invoice_date, SA.created_at as salesagreement_date, S.id as seller_id, C.id as buyer_id,SA.product_id,P.unit_code,SA.purchase_price FROM sales_agreement SA LEFT JOIN charge_invoice CI ON CI.id = SA.invoice_id LEFT JOIN importer S ON S.id = SA.seller_id LEFT JOIN clients C ON C.id = SA.buyer_id LEFT JOIN products P ON P.id = SA.product_id WHERE SA.deleted = 0 ORDER BY SA.created_at DESC )P 。我设法做这个查询。但我有一个错误。

UnityEngine.Text myText;

enter image description here

1 个答案:

答案 0 :(得分:2)

为了清楚起见,我会将CONCAT_WS的来电替换为明确的CASE表达式:

SELECT
    SA.id,
    ...
    CASE WHEN C.client_type = 'Corporation'
         THEN C.corporation
         ELSE CONCAT_WS(' ', C.firstname, C.middlename, C.lastname) END AS fullname,
    ...
FROM sales_agreement SA
...

当前对CONCAT_WS的调用问题是IF函数调用没有else条件,并且您还有一个包含在括号中的事物列表作为第一个传递(和only)参数CONCAT_WS。此外,如果将空格分隔符传递给CONCAT_WS,则不必在调用本身中包含空格。这是CONCAT_WS的整点,它为您处理分隔符。