I have a query like below
SELECT * FROM mdata ORDER BY ABS(DATEDIFF(NOW(), orderdate)) LIMIT 1
returns the Nearest - Closest Time from a DateTime field type (orderdate) and this includes fields after current time, as well. How can I retrieve the fields AFTER now?
How can I retrieve the fields AFTER now?
This simple query will do that:
-- Return all rows after now
SELECT * FROM mdata WHERE orderdate > NOW()
If you want to return just one after now but not equal to now, do this:
-- Return first row AFTER now
SELECT * FROM mdata WHERE orderdate > NOW() ORDER BY orderdate ASC LIMIT 1
NOW() reference
Related
I need a way to show the rows ordered by date ( ascending ) but with the date from now. I am using this query:
SELECT * FROM status
ORDER BY YEAR(datestart), MONTH(datestart), DAY(datestart) ASC
but it still shows the rows older than today.
This is the database structure:
id, status, datestart
database:
0,blabla,2015-02-12 16:15:12
1,blabla,2017-02-12 16:15:12
2,blabla,2016-08-11 19:13:22
4,blabla,2016-01-27 11:12:02
5,blabla,2016-07-21 18:12:02
6,blabla,2018-03-22 13:35:22
8,blabla,2016-08-15 17:12:32
expected results:
5,blabla,2016-07-21 18:12:02
2,blabla,2016-08-11 19:13:22
8,blabla,2016-08-15 17:12:32
1,blabla,2017-02-12 16:15:12
5,blabla,2018-03-22 13:35:22
You can simply add a WHERE clause:
SELECT * FROM status WHERE datestart > now()
ORDER BY YEAR(datestart), MONTH(datestart), DAY(datestart) ASC
This is a bit difficult to describe, and I'm not sure if this can be done in SQL. Using the following example data set:
ID Count Date
1 0 1/1/2015
2 3 1/5/2015
3 4 1/6/2015
4 3 1/9/2015
5 9 1/15/2015
I want to return records where the Date column falls into a range. But, if the "from" date doesn't exist in the table, I want to use the most recent date as my "From" select. For example, if my date range is between 1/5 and 1/9, I would expect to have records 2,3, and 4 returned. But, if I have a date range of 1/3 - 1/6 I want to return records 1,2,and 3. I want to include record 1 because, as 1/3 does not exist, I want the value of the Count that is rounded down.
Any thoughts on how this can be done? I'm using MySQL.
Basically, you need to replace the from date with the latest date before or on that date. Let me assume that the variables are #v_from and #v_to.
select e.*
from example e
where e.date >= (select max(e2.date) from example e2 where e2.date <= #v_from) and
e.date <= #v_to;
EDIT AFTER EDIT:
SELECT *
FROM TABLE
WHERE DATE BETWEEN (
SELECT Date
FROM TABLE
WHERE Date <= #Start
ORDER BY Date DESC
LIMIT 1
)
AND #End
Or
SELECT *
FROM TABLE
WHERE DATE BETWEEN (
SELECT MAX(Date)
FROM TABLE
WHERE Date <= #Start
)
AND #End
I have a table that contains tasks, each task has a date_start and date_finish field.
I need to construct a query which will take a passed in date and return all rows if that passed in date falls between the date_start and date_finish.
Does this make sense?
I have been trying to use standard date type querys such as:
SELECT *
FROM project_task
WHERE project_task.date_start >= '2013-10-10' AND project_task.date_finish <= '2013-10-10'
but it doesn’t return the correct results and using BETWEEN does not work either because I need it to take into account both fields (date_start and date_finish) not just the one.
I think it may only be the WHERE part of the query I need.
You've made a simple mistake, you got the order of your operators wrong:
SELECT *
FROM project_task
WHERE
project_task.date_start <= '2013-10-10' -- start should before the test date
AND
project_task.date_finish >= '2013-10-10' -- and finish after the test date
Explanation
If the date checked is between date_start and date_finish, then we must have reached at last the start date. We could have a later date too. That means the
date_start will be lower or equal than the date checked
And the second check said: the finish date musn't have passed. So
date_finish has to be greater or equal than the date checked.
With your original query you will only get projects that start and end on '2013-10-10'.
Demo
DECLARE #Now datetime = '2013-10-10T00:00:00'
SELECT *
FROM project_task a
WHERE #Now >= a.Date_Start
AND #Now < isnull(a.Date_Finish, '9999-12-31T00:00:00')
I have a DB full of deals, and a website which will present just one of these deals if the deal is a featured deal, however I am struggling to get the right logic...
the deal that is shown on the site must
a. be within a valid date range
b. be the most recently added deal to the database
by using the following query, I am able to accomplish this:
"SELECT * FROM deals WHERE datestart < now() AND dateend > now() ORDER BY deals.deal_id DESC"
Great. however... on rare occasions a whole bunch of deals are added at once, so I need some kind of override to specify which one should be 'featured'.
I added a boolean value [featured] and tested the following query:
"SELECT * FROM deals WHERE datestart < now() AND dateend > now() ORDER BY deals.featured DESC"
It worked, but now I need to specify the featured deal, or else the featured deal will be randomly selected? whereas I only want to have it as an override.
SO I need to combine the above 2 scripts somehow.
any ideas?
thanks guys.
Add the boolean comparison to the WHERE clause and limit the number of results to 1. When ordering by the deal_id this will always return the same result.
SELECT *
FROM deals
WHERE datestart < now() AND dateend > now() AND deals.featured IS TRUE
ORDER BY deals.deal_id DESC
LIMIT 1
EDIT:
Here is an alternative so you don't have to add more to the WHERE clause.
SELECT *
FROM deals
WHERE datestart < now() AND dateend > now()
ORDER BY deals.featured DESC, deals.deal_id DESC
LIMIT 1
The order of the ORDER clause is important, if deal_id's are unique, as i presumed they were, the featured boolean would not be used if they were ordered the opposite way round.
If you just want to combine the two queries, you can just combine the where clauses - order by deal_id first, then if there are multiples with the same deal_id, it'll then sub order by whether it's featured or not:
SELECT *
FROM deals
WHERE datestart < Now()
AND dateend > Now()
ORDER BY deals.deal_id DESC,
deals.featured DESC
To get featured deals you need to check for featured IS TRUE and use UNION ALL operation to override it:
SELECT *
FROM deals
WHERE datestart < Now()
AND dateend > Now()
AND featured IS TRUE
ORDER BY deal_id DESC
UNION ALL
SELECT *
FROM deals
WHERE datestart < Now()
AND dateend > Now()
ORDER BY deal_id DESC;
I think what you 're trying to do is the following:
UPDATE deals
SET featured=1
WHERE datestart < now() AND dateend > now()
ORDER BY deal_id DESC
LIMIT 1;
Ok, so now you are trying to update the most recent valid featured deal and mark it as featured. If the rows affected count is 0 then you already have the most recent valid featured deal or there is no valid deal at all in the table (it's your choice how to react to this).
If the rows affected count is 1 then you just found a new one and you need to select it (perhaps to fill it in your site). The select statement:
SELECT *
FROM deals
WHERE datestart < now() AND dateend > now() AND featured=1
ORDER BY deal_id DESC
LIMIT 1;
Ideally you would combine these two queries into only one update-select but no way to do just that at the time.
I have a DATETIME column on my table which stores when a record was created. I want to select only the records created on a particular date. If I try:
SELECT *
FROM myTable
WHERE postedOn = '2012-06-06'
It returns no rows even though there are many rows in the table with the postedOn set as
2012-06-06 21:42:02, 2012-06-06 07:55:17 , and so forth.
Any ideas?
Use the DATE scalar:
SELECT *
FROM myTable
WHERE date(postedOn) = '2012-06-06'
SELECT *
FROM myTable
WHERE DATE(postedOn) = '2012-06-06'
DATE() returns the date part of a datetime field.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
Create a time range by adding a day to the date:
SELECT *
FROM myTable
WHERE postedOn >= '2012-06-06' and postedOn < '2012-06-07'
This is the most efficient way, as the query can use an index on the field.