gpt4 book ai didi

mysql - UNION 语句中的 WHERE

转载 作者:行者123 更新时间:2023-11-29 15:43:48 25 4
gpt4 key购买 nike

问题提出:

Code a SQL statement that uses a UNION between the firstname and lastname of the editor and author tables where the 5th through 6th digits of the Social Security number are '72'.

IMPORTANT: Do NOT use the pattern matching characters, '%' and '_' in your answer!!

Also be sure to use a string expression to indicate whether or not someone is an author or editor in the result set.

到目前为止我有这个代码:

SELECT 'editor', firstname, lastname
FROM editor
WHERE ssn=
UNION
SELECT 'author', firstname, lastname
FROM author
WHERE ssn= ;

但是如果我不能使用模式匹配字符“%”和“_”,我应该在 WHERE 语句中添加什么

SSN 在数据库中写入为 123-45-6789

最佳答案

使用 SUBSTRING 可以满足您的解决方案不应包含任何引号或通配符的要求。该函数的第一个参数指定要搜索的表达式,第二个参数指定起始位置 (5),第三个参数指定子字符串的长度 (2)。

SELECT 'editor', firstname, lastname
FROM editor
WHERE SUBSTRING(ssn, 5, 2) = '72'
UNION
SELECT 'author', firstname, lastname
FROM author
WHERE SUBSTRING(ssn, 5, 2) = '72'

关于mysql - UNION 语句中的 WHERE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57300107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com