可以使用子查询编写此查询吗?

我有的:

SELECT name, lifeExpectancy from country
WHERE lifeExpectancy IS NOT NULL
ORDER BY lifeExpectancy ASC
LIMIT 1


结果应该是:

name     | lifeExpectancy
-------------------------
Zambia   |           37.2

最佳答案

当然:

SELECT name, lifeExpectancy from country
WHERE lifeExpectancy = (SELECT MIN(lifeExpectancy) FROM country)
LIMIT 1


如果有多个国家/地区共享最大寿命期望值,则仍需要LIMIT 1。如果已知lifeExpectancy是唯一的,至少在最大程度上是唯一的,或者可以确定在并列第一名时返回多行,则可以省略LIMIT 1

SELECT name, lifeExpectancy from country
WHERE lifeExpectancy = (SELECT MIN(lifeExpectancy) FROM country)

10-05 20:29