简体   繁体   English

SQL查询当前日期在开始/结束日期之内的条目

[英]SQL Query for an entry where current date is inside start/end dates

Using MySQL 使用MySQL

The two fields I need to compare the current date to are: startDate, endDate 我需要比较当前日期的两个字段是:startDate,endDate

I need a query something like this: 我需要这样的查询:

SELECT * FROM `{$this->_sPrefix}` 
WHERE `profileID` = '{$iProfileId}' 
AND CUR_DATE() 
BETWEEN `startDate` AND `endDate` 
ORDER BY `added` DESC";

This is my new query but I don't know how to do both checks: profileID and the date together. 这是我的新查询,但我不知道如何同时执行两个检查:profileID和日期。

You should be able to do that, but you have to use BETWEEN: 您应该能够做到这一点,但是必须使用BETWEEN:

 WHERE CUR_DATE() BETWEEN `startDate` AND `endDate`

What SQL database are you using? 您正在使用什么SQL数据库?

Edit (in response to edited question, and please don't keep changing the question!): 编辑(针对已编辑的问题,请不要继续更改问题!):

Your query looks correct, assuming that profileID, startDate, and endDate are all columns in the same table. 假设profileID,startDate和endDate是同一表中的所有列,则查询看起来正确。

Also, if this is a web-facing application, please consider avoiding this kind of dynamic SQL generation as it makes your application open to SQL injection attacks. 另外,如果这是一个面向Web的应用程序,请考虑避免这种动态SQL生成,因为它会使您的应用程序容易受到SQL注入攻击。

... WHERE CUR_DATE() BETWEEN `startDate` AND `endDate` ORDER ...
...WHERE `startDate` <= CUR_DATE() and `endDate` >= CUR_DATE()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询以查找开始日期和结束日期之间的总小时数 - SQL query to find total hour of between start date and end date 从MySql中抓取当前日期在开始日期和结束日期之间的行(检查当前日期是否在开始日期和结束日期之间) - Grabbing rows from MySql where current date is in between start date and end date (Check if current date lies between start date and end date) SQL Query用于选择具有开始和结束日期的即将发生的事件 - SQL Query to select upcoming events with a start and end date sql-显示给定日期(date_start和date_end)的天数,如果end_date超过了,则应用罚款 - sql - display the span of days given dates(date_start and date_end), if the end_date is over the penalty is applied php mysql搜索开始日期和结束日期字段以获取今天的日期(如果它在范围内) - php mysql search start & end dates fields for todays date if it falls inside range mysql查询在单独的行上有开始和结束日期 - mysql query with start and end dates on individual rows MySQL / PHP查询-检查输入的开始/结束日期不与Db中的现有日期范围重叠 - MySQL / PHP Query - check that entered start/end dates don't overlap existing date ranges in Db 选择开始/结束日期两个表 SQL - Select start/end dates two tables SQL 使用DST获取当前的星期开始/结束日期 - Get current week start/end date with DST 从开始日期等于结束日期的sql中检索数据 - retrieve data from sql where start date is equal and between end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM