การใส่รายการ string แบบ VALUES ('A') , ('B') , ... จะยาวและแก้ไขยากเมื่อข้อมูลเพิ่มขึ้น วิธีที่ง่ายและสะอาดกว่าคือ ใช้ string เดียวคั่นด้วยเครื่องหมาย แล้วใช้ฟังก์ชันแยกเป็นแถว (Split) ซึ่ง syntax จะแตกต่างกันตาม Database ที่คุณใช้ครับ
PostgreSQL
WITH RawData (tag_name) AS (
SELECT DISTINCT TRIM(unnest(string_to_array(
'Access,Admin,Algorithm,Application,Authentication,Backup,Broken,Brute,Code,Components,Control,Cryptographic,Data,Database,Default,Design,Digital,Error,Failures,Firewall,Force,Forgery,Framework,HTTP,HTTPS,ID,Identification,Injection,Insecure,Integrity,Library,Log,Logging,Login,Misconfiguration,Module,Monitoring,Open,Outdated,OWASP,password,Patch,Plaintext,Project,Request,Security,Server-Side,Session,Signature,Software',
','
)))
)
SELECT * FROM RawData;
SQL Server (2016+)
WITH RawData (tag_name) AS (
SELECT DISTINCT TRIM(value)
FROM STRING_SPLIT(
'Access,Admin,Algorithm,Application,Authentication,Backup,Broken,Brute,Code,Components,Control,Cryptographic,Data,Database,Default,Design,Digital,Error,Failures,Firewall,Force,Forgery,Framework,HTTP,HTTPS,ID,Identification,Injection,Insecure,Integrity,Library,Log,Logging,Login,Misconfiguration,Module,Monitoring,Open,Outdated,OWASP,password,Patch,Plaintext,Project,Request,Security,Server-Side,Session,Signature,Software',
','
)
)
SELECT * FROM RawData;
BigQuery
WITH RawData AS (
SELECT DISTINCT TRIM(tag) AS tag_name
FROM UNNEST(SPLIT(
'Access,Admin,Algorithm,Application,Authentication,Backup,Broken,Brute,Code,Components,Control,Cryptographic,Data,Database,Default,Design,Digital,Error,Failures,Firewall,Force,Forgery,Framework,HTTP,HTTPS,ID,Identification,Injection,Insecure,Integrity,Library,Log,Logging,Login,Misconfiguration,Module,Monitoring,Open,Outdated,OWASP,password,Patch,Plaintext,Project,Request,Security,Server-Side,Session,Signature,Software',
','
)) AS tag
)
SELECT * FROM RawData;
Snowflake
WITH RawData AS (
SELECT DISTINCT TRIM(f.value::STRING) AS tag_name
FROM TABLE(FLATTEN(input => SPLIT(
'Access,Admin,Algorithm,Application,Authentication,Backup,Broken,Brute,Code,Components,Control,Cryptographic,Data,Database,Default,Design,Digital,Error,Failures,Firewall,Force,Forgery,Framework,HTTP,HTTPS,ID,Identification,Injection,Insecure,Integrity,Library,Log,Logging,Login,Misconfiguration,Module,Monitoring,Open,Outdated,OWASP,password,Patch,Plaintext,Project,Request,Security,Server-Side,Session,Signature,Software',
','
))) AS f
)
SELECT * FROM RawData;
MySQL (8.0+)
MySQL ไม่มีฟังก์ชัน SPLIT ตรง ๆ แต่สามารถแปลง Comma-separated string ให้เป็น JSON Array แล้วคลี่ด้วย JSON_TABLE ได้ทันทีครับ
WITH RawData AS (
SELECT DISTINCT TRIM(j.tag) AS tag_name
FROM JSON_TABLE(
CONCAT('["', REPLACE('Access,Admin,Algorithm,Application,Authentication,Backup,Broken,Brute,Code,Components,Control,Cryptographic,Data,Database,Default,Design,Digital,Error,Failures,Firewall,Force,Forgery,Framework,HTTP,HTTPS,ID,Identification,Injection,Insecure,Integrity,Library,Log,Logging,Login,Misconfiguration,Module,Monitoring,Open,Outdated,OWASP,password,Patch,Plaintext,Project,Request,Security,Server-Side,Session,Signature,Software', ',', '","'), '"]'),
'$[*]' COLUMNS (tag VARCHAR(100) PATH '$')
) AS j
)
SELECT * FROM RawData;
💡 เคล็ดลับเพิ่มเติม
- ถ้าอยากคงรูปแบบ
VALUESไว้แต่ไม่อยากพิมพ์เอง:- ใช้ Excel/Google Sheets → วางรายการในคอลัมน์ A → คอลัมน์ B ใส่สูตร
=" ('"&A1&"') ,"→ ลากลง → คัดลอกไปวางใน SQL - ใช้ VS Code: เลือกข้อความทั้งหมด →
Ctrl+Shift+L→ พิมพ์('→End→ พิมพ์') ,
- ใช้ Excel/Google Sheets → วางรายการในคอลัมน์ A → คอลัมน์ B ใส่สูตร
- ถ้าข้อมูลยาวมากหรือเปลี่ยนบ่อย
ควรเก็บไว้ใน Table จริง (tags_master) หรืออ่านจาก CSV (COPY/LOAD DATA INFILE) จะดูแลง่ายกว่า - ประสิทธิภาพการ Query
ตัวอย่างด้านบนคงTRIMและDISTINCTไว้เพื่อความปลอดภัย หากข้อมูลต้นทางสะอาดและไม่มีค่าซ้ำอยู่แล้ว สามารถตัดออกเพื่อเพิ่มความเร็วในการประมวลผลได้
อ่านเพิ่มเติม