gpt4 book ai didi

mysql - SQL 内联 IF 语句显示列名和字符串

转载 作者:行者123 更新时间:2023-11-29 11:42:38 24 4
gpt4 key购买 nike

我正在尝试在 SQL 中运行 IF 语句:

IF(customer = 'Y', contact_name + '(' + contact_email + ')', 'Internal')

该语句适用于其正确和错误,但我希望能够显示

contact_name(表格列)

然后在旁边的括号中,它将显示 contact_email,这也是一个表格列。

我在使用 contact_email 周围的括号时遇到问题。

我的完整查询是:

SELECT datetime as datetime, notes as notes 
FROM customer_communication
WHERE company = '276'
UNION ALL
SELECT datetime as datetime,
CONCAT('Ticket #', + ticketnumber, +
' () Opened By ', +
IF(customer = 'Y', contact_name '(' contact_email ')',
'Internal')) as notes
FROM tickets
WHERE company = '276'

我看到这个错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''(' contact_email ')', 'Internal')) as notes FROM tickets WHERE company = '276' ' at line 1

最佳答案

这应该有效:

SELECT datetime as datetime, notes as notes 
FROM customer_communication
WHERE company = '276'

UNION ALL

SELECT datetime as datetime,
CONCAT('Ticket #',
ticketnumber,
' () Opened By ',
IF(customer = 'Y', contact_name + '(' + contact_email + ')'
, 'Internal')) as notes
FROM tickets
WHERE company = '276'

关于mysql - SQL 内联 IF 语句显示列名和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com