เวลาทำงานกับ Queries บางครั้งเราต้องการนำ Raw Data จาก Database มา Reformat ให้ต่างออกไปเพื่อใช้ที่อื่น ในบทความนี้ เราดู Functions ใน MySQL ช่วยทำงานกับ String Types ใน Queries
Building the Schema: สร้าง Schema
Section นี้เตรียม Quick Schema illustrate SQL Concepts ใน Post นี้ เราใช้ MySQL สำหรับ Functions แนะนำ SQL Fiddle Practice Syntax และ Run Search Queries ที่นั่น Test Queries ใน Relational Databases อื่นได้
Create Table and Insert Values
CREATE TABLE birthdays ( name varchar(200) NOT NULL, birthday varchar(10) NOT NULL ); INSERT INTO birthdays (name, birthday) VALUES("Jane", "11/20/1993"); INSERT INTO birthdays (name, birthday) VALUES("Duncan", "01/15/1987"); INSERT INTO birthdays (name, birthday) VALUES("Lucas", "07/21/1996"); INSERT INTO birthdays (name, birthday) VALUES("Alexa", "12/31/1988");
4 บรรทัดบนสุดสร้าง Table "birthdays" กับ Columns "name" และ "birthday" ส่วนที่เหลือ Insert Values เข้า Database เราใช้ตอน Build Query Statements
ถ้ารัน Basic Query บน Table นี่คือ Query Statement และ Results:
SELECT name, birthday FROM birthdays;
Results:
| name | birthday |
| Chris | 11/20/1993 |
| Duncan | 01/15/1987 |
| Lucas | 07/21/1996 |
| Alexa | 12/31/1988 |
Building the Query Statements: สร้าง Query Statements
CONCAT()
ฟังก์ชัน CONCAT() คืน Result หลัง Join Arguments เข้าเป็น String เดียว มีอย่างน้อย 1 Argument แต่อาจมี Multiple Functions Arguments ใน Function อาจเป็น:
- Binary String – หาก Argument 任何一個เป็น Binary String Return Value เป็น Binary String
- Nonbinary String – Typical String Value หาก Arguments ทั้งหมดเป็น Nonbinary String CONCAT() คืน Nonbinary String Value
- Numeric – หาก Input 任何一個เป็น Numeric Type จะ Coerced เป็น Nonbinary String ก่อน Join กับ Arguments อื่นๆ
- Null – หากมี NULL Values Values เหล่านั้นจะ Ignored
Formal Syntax:
CONCAT( <str value 1>, <str value 2>, [ … <str value N> ]);
เราใช้ CONCAT Keyword พร้อม Parentheses บอกว่าเป็น Function ภายใน Parentheses คือ N Input Strings ตัวอย่าง MySQL:
SELECT CONCAT(name, " ", birthday) AS Result FROM birthdays;
เรา "SELECT" Return Value จาก Concatenation ของ name, Space Char, และ birthday Assign ชื่อ "Result"
Result ของ Query นี้:
| Result |
| Chris 11/20/1993 |
| Duncan 01/15/1987 |
| Lucas 07/21/1996 |
| Alexa 12/31/1988 |
เปรียบเทียบ Result นี้กับ Original Query Statement ที่ Test ใน Section Building the Schema ต่างอย่างไร? ข้อดีของการตั้ง Data แบบนี้คืออะไร?
ไม่มี Right หรือ Wrong จำเป็น ทุกอย่างเกี่ยวกับการพึ่งพา Data นี้ใน Application ของคุณ
CONCAT_WS()
ฟังก์ชัน CONCAT_WS() เหมือน CONCAT() แต่มี Required First Argument ที่ไม่มีใน CONCAT() ต้นฉบับ Argument นี้คือ Separator ที่ไประหว่าง Arguments ที่เหลือใน Function Rules ที่เหลือของ CONCAT() Apply
Formal Syntax:
CONCAT_WS(<separator>, <str value 1>, <str value 2>, [ … <str value N> ]);
เราใช้ CONCAT_WS() Keyword พร้อม Parentheses บอกว่าเป็น Function Argument แรกใน Parentheses คือ Separator ที่ไประหว่าง Input Strings แต่ละตัว Arguments ที่เหลือคือ Strings ที่ Join เข้าด้วยกัน ตัวอย่าง MySQL:
SELECT CONCAT_WS(", ", name, birthday) AS Result FROM birthdays;
Result คือ String Concatenation ด้วย Argument แรกระหว่าง Arguments อื่นๆ ทั้งหมด
| Result |
| Chris, 11/20/1993 |
| Duncan 01/15/1987 |
| Lucas 07/21/1996 |
| Alexa 12/31/1988 |
เหมือน CONCAT() Function CONCAT_WS() Function คืนแค่ 1 Column ชื่อ Result ที่ Value คือ Input String(s) Concatenated ด้วย Separator Character
สรุป
ในบทความนี้ เราครอบคลุม SQL CONCAT() และ CONCAT_WS() Functions เกี่ยวกับการใช้ใน MySQL โปรดทราบอาจมี Minor Differences ใน Syntax เมื่อใช้ Relational Databases อื่น เพราะอาจมี SQL Flavor ของตัวเอง Concept ของ Function เหมือนกัน แต่อาจ Wording ต่างกัน