我试图执行收集所有数据的查询。
因此,我的条件是if
client_type
行的corporation field
查询将获取fullname
的数据,并将其分配给else
或firstname
它会获取lastname
,middlename
和fullname
并将其指定为 (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;
答案 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
的整点,它为您处理分隔符。