在 SQL 中,字符串連接(concatenate)是將兩個或多個字符串組合成一個單獨的字符串。這種操作在許多不同的場景下都非常有用。以下是一些常見的 SQL 連接應用場景:
SELECT first_name || ' ' || last_name AS full_name FROM users;
SELECT 'Hello, ' || first_name || '! Your email is ' || email || '. Please contact us if you have any questions.' AS custom_message FROM users;
SELECT 'https://example.com/user/' || user_id || '/profile' AS profile_url FROM users;
SELECT street || ', ' || city || ', ' || state || ' ' || zip_code AS full_address FROM addresses;
SELECT 'User ' || user_id || ' logged in at ' || login_timestamp AS log_entry FROM login_history;
SELECT product_id || '_' || color_id || '_' || size_id AS unique_key FROM products;
這些場景只是 SQL 連接操作的一小部分應用。實際上,只要需要將多個字符串組合成一個字符串,就可以使用 SQL 連接。