我正试图在佛蒙特州和纽约州的会员生日表中找到最年长的人。我的会员类似于以下内容:
Members
-------
MemberID, Firstname, Lastname, Birthday, Region
我制定了以下子查询:
SELECT lastname
FROM members
WHERE region = 'VT'
AND year(birthday) > (SELECT year(birthday)
FROM members
WHERE region = 'NY')
SQL查询系统告诉我它返回多行。我错过了什么,它在逻辑上是否正确?我再问一遍,我怎么能找到佛蒙特州的每个成员都比纽约的所有成员都年长。
答案 0 :(得分:1)
select top 1 * from table where region='vermont' or region ='newyork' order by year(birthday) desc
答案 1 :(得分:1)
您需要找到NY成员的最大生日,这样您才能获得一个值。然后使用该值查找大于NY
最大值的VT成员你可以试试这个:
select lastname
from members where region = 'VT' and year(birthday) >
(select max(birthday) from members where region = 'NY')
逻辑运算符>
要求运算符右侧只有1个值。
答案 2 :(得分:1)
做的:
select lastname from members where region = 'VT' and year(birthday)
>
(select year(birthday) from members where region = 'NY' ORDER BY birthday ASC LIMIT 1)
答案 3 :(得分:1)
我没有得到大部分答案。它们没有涉及纽约和纽约。 VT要求(除了重现模棱两可的问题vs“我试过的”)。 更小生日的人也一定年纪越大。
SELECT lastname
FROM members
WHERE region in('VT' ,'NY') and birthday =
(SELECT MIN(birthday)
FROM members where
region in('VT' ,'NY') )