2016-02-19 36 views
0

我想在我的SQL运行的IF语句: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

+2

什么数据库您使用的?你遇到了什么错误? – David

+2

你可能想要一个CASE表达式。在大多数RDBMS中,IF通常是控制流。 –

+0

请标记您使用的数据库,显示您正在尝试运行的完整声明,并包含您收到的任何确切的错误消息。 http://stackoverflow.com/help/how-to-ask –

回答

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'