例如我在数据库中有这个名字
LastName
Amy
Betty
Camy
Daisy
David
Johnson
这仅在想要选择从 Amy 到 David 时才有效:
select * from Student
where LastName >= 'A' and LastName <= char(ascii('D') + 1)
order by LastName
如果我只想选择 From Amy 到 Daisy,我该怎么做?
例如我在数据库中有这个名字
LastName
Amy
Betty
Camy
Daisy
David
Johnson
这仅在想要选择从 Amy 到 David 时才有效:
select * from Student
where LastName >= 'A' and LastName <= char(ascii('D') + 1)
order by LastName
如果我只想选择 From Amy 到 Daisy,我该怎么做?
(我可能遗漏了一些你忘记包含在你的问题中的东西,但是)为什么要让生活变得更加艰难:
SELECT *
FROM Student
WHERE LastName BETWEEN 'Amy' AND 'Daisy'
ORDER BY LastName
要回复评论,您可以这样做的一种方法是:
SELECT *
FROM Student
WHERE LastName >= 'Amy'
AND (LastName <= 'Dai' OR LastName LIKE 'Dai%')
SQL Fiddle证明了这一点仍应导致单个索引查找。