【发布时间】:2012-10-24 21:14:32
【问题描述】:
我有一个包含名称、成绩和分数的数据表。例如,“约翰”获得了 B 级,其分值为 3.0。
如何选择低于“B”级的条目?
所以我需要做这样的事情
Value = select Point from MyTable where Grade="B"
然后
select * from MyTable where Point < value
但显然 SQL 必须是一个语句...
【问题讨论】:
我有一个包含名称、成绩和分数的数据表。例如,“约翰”获得了 B 级,其分值为 3.0。
如何选择低于“B”级的条目?
所以我需要做这样的事情
Value = select Point from MyTable where Grade="B"
然后
select * from MyTable where Point < value
但显然 SQL 必须是一个语句...
【问题讨论】:
您可以嵌套选择并添加子查询:
SELECT realtable.*
FROM (SELECT Point FROM MyTable WHERE Grade="B" LIMIT 1) subquery, MyTable realtable
WHERE subquery.Point > realtable.Point
【讨论】:
尝试在子查询中使用如下:
select *
from MyTable
where Point < (select Point
from MyTable
where Grade="B")
但如果您的子查询返回多于一行,请尝试使用聚合函数,例如 min
select *
from MyTable
where Point < (select min(Point)
from MyTable
where Grade="B")
或使用LIMIT 和join:
select *
from MyTable mt
join (select Point from MyTable
where Grade="B"
order by Point
LIMIT 1) mt2 on mt.Point < mt2.Point
【讨论】:
limit 1。