在 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 连接。