MySQL拼接函數(shù)(concat函數(shù))可以用來連接兩個(gè)或多個(gè)字段或字符串。
語法:concat(str1,str2,...) 參數(shù):需要連接的字符串或字段 返回值:連接后的字符串
例1:連接兩個(gè)字段
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM employees; 結(jié)果:full_name Smith, John Wong, Anna
例2:連接字符串和字段
SELECT CONCAT('Name: ', last_name,', ',first_name) AS full_name FROM employees; 結(jié)果:full_name Name: Smith, John Name: Wong, Anna
concat函數(shù)可以連接多個(gè)字段或字符串,需要用逗號(hào)分隔。
例如,可以連接三個(gè)字段:
SELECT CONCAT(last_name,', ',first_name,' (',title,')') AS full_info FROM employees; 結(jié)果:full_info Smith, John (Manager) Wong, Anna (Developer)
注意:
- 如果任何一個(gè)參數(shù)是NULL,則結(jié)果為NULL。
- 如果參數(shù)中有任何一個(gè)是數(shù)字,則會(huì)被隱式轉(zhuǎn)換為字符串。