简体   繁体   English

sql查询大于DATE(年)A但小于DATE(年)B

[英]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.

相关问题 检查日期是否小于一年前的MySQL - Check if date is less than a year ago MySQL MySQL查询返回等于或大于特定日期的行,其中日期以年,月和日列分隔 - MySQL query to return rows that are equal to or greater than a certain date, where the date is separated in year, month and day columns SQL查询 - 在小于或等于日期时加入 - SQL Query - join on less than or equal date MySQL大于日期返回的日期小于给定的日期 - MySQL Greater Than Date Returning A Date Less Than Date Given 如果保存日期超过1年,则从mysql进行PDO查询行 - PDO Query row from mysql if date saved is more than 1 year SQL Select查询以选择截止日期之间的数据,其中起始日期月份大于截止日期月份,年份差&gt; 1 - SQL Select query to select data between to dates where from date month is higher than the to date month and year difference is >1 当Yii2中的年份大于月份小于指定的年份时,如何获取大于选定年份和月份的记录 - How to get records greater than selected year and month when the year is greater and month is less than specified one in Yii2 SQL:查询大于X个月和Y天之前的日期 - SQL: Query for date greater than X months and Y days ago Codeigniter日期比较-大于或小于日期时间字段不起作用 - Codeigniter Date comparison - Greater than or Less than datetime field not working 大于和小于日期在 sequelize 中没有记录,但适用于 heidisql - greater than and less than a date gives no records in sequelize but works with heidisql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM