SQL内联IF语句,用于显示列名和字符串

时间:2016-02-19 21:11:03

标签: mysql sql

我正在尝试在我的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 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   靠近'(' contact_email')','内部'))作为票据来自票据WHERE   公司=' 276' '在第1行

1 个答案:

答案 0 :(得分: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'