[英]sql query greater than DATE (year) A but less than DATE (year) B
I have a list of items like so: 我有一个像这样的项目清单:
Field: Name & Field: DOB
- David - 1962
- Robert - 1955
- Tracy - 1980
- Bono - 1964
- Betty - 1968
I need a simple SQL query that will return all name whose birth years ate in the 1960s (1960-1969). 我需要一个简单的SQL查询,该查询将返回所有出生年份在1960年代(1960-1969)的名字。 something along the lines of this logic:
遵循此逻辑的一些东西:
$sql = "SELECT * FROM employees WHERE DOB => '1960' AND DOB =< '1969' AND ORDER BY name";
FYI: Currently the DOB field in the employee database is a VARCHAR field with 4 character designation. 仅供参考:当前雇员数据库中的DOB字段是VARCHAR字段,具有4个字符的名称。
Can someone point me to the correct syntax on such a query? 有人可以在这样的查询中为我指出正确的语法吗?
You should not be storing date/time information as varchar
, but since you have one way you can screen for 1960s birthdays is to just check the first three characters of the DOB
field: 您不应该将日期/时间信息存储为
varchar
,但是由于可以通过一种方法来筛选1960年代生日,因此只需检查DOB
字段的前三个字符即可:
SELECT *
FROM employees
WHERE SUBSTRING(DOB, 1, 3) = '196'
ORDER BY name
You could also use your original approach, with the following WHERE
clause: 您还可以使用原始方法,并使用以下
WHERE
子句:
WHERE DOB >= '1960' AND DOB <= '1969'
However, I don't like treating strings as numbers, because you get weird things happening if the strings should ever change length. 但是,我不喜欢将字符串视为数字,因为如果字符串应该更改长度,则会发生奇怪的事情。 I would advise you to use a
datetime
to store your DOB
information. 我建议您使用
datetime
来存储您的DOB
信息。
Change the query from: 将查询从以下位置更改:
"SELECT * FROM employees WHERE DOB => '1960' AND DOB =< '1969' AND ORDER BY name";
to 至
"SELECT * FROM employees WHERE DOB >= '1960' AND DOB <= '1969' AND ORDER BY name";
// Its >= or <= not => or =<
and try again 然后再试一次
Assuming your varchar DOB column contains only numbers: 假设您的varchar DOB列仅包含数字:
SELECT *
FROM employees
WHERE CONVERT(DOB, INTEGER) >= 1960
AND CONVERT(DOB, INTEGER) <= 1969
Did You try this ? 你尝试过这个吗?
$sql = "SELECT * FROM employees WHERE DOB Like '%YYY%'AND ORDER BY name";
Consider yyy is 196X so all dob starting with 196 will result. 考虑到yyy是196X,因此将产生所有以196开头的dob。
But this can't be specific enough 但这还不够具体
select * from tt1 where year >='1960' and year <='1969' order by name
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.