I am not having much knowledge on mysql queries.. and i'm trying to get values from database from last two hours from now ..I have searched about it and i have found some related posts too.. but unfortunately i am unable to implement the logic.. please help me solve this issue..
here is the mysql query
select *
from `ordermaster`
where ((
`ordermaster`.`Pick_date`
= curdate())
and (`ordermaster`.`Pick_time`
<= (now() - interval 2 hour))
and (`ordermaster`.`Status`
= 2))
were, Pick_Date = "2017-04-19" (today date) and Pick_Time = "10:00:00" (24 hours format)
Try the Following:
Select * From ordermaster om
Where Concat(om.Pick_date,' ', om.Pick_time) as date Between
(now() - interval 2 hour) and now()
AND Status = 2
You can use the between keyword
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` between (now() - interval 2 hour) and now() and
`Status` = 2
Edit
Based on our chat, where we found out the GoDaddy server you have your db hosted on is 12.5 hours ahead of your local time, the final query you should use is
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` <= now() + interval 10 hour + interval 30 minute and
`Status` = 2
Related
Events can be a 1 day event or be an on-going event. This means that sometimes events can go for multiple days, weeks, or months.
As it is now, it is possible to sort the query result by END in ascending order (those expiring earlier shows first) or START in ASC (events based on start date). However, in both cases I have limitations that I am trying to reduce as much as I can.
When sorting by END, sometimes events that are ongoing and have already started get pushed to later in the list.
When sorting by START, events that have already started and are ongoing end up taking up the first sections of the list.
Is it possible to chain multiple ORDER BY statements based on logic rather than columns?
For example:
Get events that are expiring within the next 7 days:
SELECT * FROM data WHERE end < NOW() + INTERVAL 7 DAY;
Get events that are still ongoing between 7 days from today and ending within 14 days:
SELECT * FROM data WHERE NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY;
Get all remaining events...
SELECT * FROM data WHERE end >= NOW() + INTERVAL 14 DAY;
Basically, is it possible to join these into one query?
SELECT * FROM data
ORDER BY (logic 1), (logic 2), (logic 3);
Alternatively, I did get it working with running 3 separate queries and building up the result array on the server-side, but would like to simplify my code if possible.
Hoping that an end result will always show a list of events that will be expiring within 7 days first, then events that are happening between 7 - 14 days (could be starting or ongoing), then events that are still ongoing or starting after 14 days from today.
Depending on your SQL database, you can do something like this:
SELECT * FROM data
WHERE (end < NOW() + INTERVAL 7 DAY) -- logic 1
or (NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY) -- logic 2
or (NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY) -- logic 3
order by
case
when (end < NOW() + INTERVAL 7 DAY) then 1
when (NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY) then 2
when (NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY) then 3
else 4
end asc
;
You may also use union all:
SELECT 1 as sort_order, * FROM data
WHERE (end < NOW() + INTERVAL 7 DAY)
union all
SELECT 2 as sort_order, * FROM data
WHERE (NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY)
union all
SELECT 3 as sort_order, * FROM data
where NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY
The sort_order is probably not needed, but if you get your result not in the order of select, you may then use a subquery; also your database might forbids you from using order by in an union all.
select *
from (
SELECT 1 as sort_order, * FROM data
WHERE (end < NOW() + INTERVAL 7 DAY)
union all
SELECT 2 as sort_order, * FROM data
WHERE (NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY)
union all
SELECT 3 as sort_order, * FROM data
where NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY
) order by sort_order asc -- and any other key
I would personally go for the union all if possible (it is more readable).
You could try using a UNION where you select the data set and have a column that has the order you want. e.g.
SELECT 1 as orderby,* FROM data WHERE end < NOW() + INTERVAL 7 DAY;
UNION ALL
SELECT 2, * FROM data WHERE NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY
UNION ALL
SELECT 3, * FROM data WHERE end >= NOW() + INTERVAL 14 DAY
ORDER BY orderby, end
P.S. I would suggest you don't use SQL Keywords such as end for column names in your database, that can sometimes cause issues, enddate would be a better column name.
P.P.S. Avoid doing SELECT *, it is better to explicitly list the columns that you want.
I assume you are using MySQL.
use the case when .. then .. end in the select clause, then order by this column.
select *, case
when end < NOW() + INTERVAL 7 DAY then 1
when NOW() + INTERVAL 7 DAY >= start AND end < NOW() + INTERVAL 14 DAY then 2
else 3 as priority
from data
order by priority
Also, you can use the case in the order by clause.
Note: I didn't take care of your business logic, so test it well, just giving you how you can achieve it, hope it helps.
I am trying to query a table and select the rows on the current days. I am using the CURDATE() function but it doesn't seem to be working. Previously on mysql 5.1 it worked perfectly fine. But now it doesn't work.
Below is my query statement. Any help would be really appreciated. When I run this on PHP admin it brings 0 results.
When I remove the "AND DATE(checkout_date - INTERVAL 1 HOUR ) = CURDATE( )" it works fine. Thanks
SELECT * FROM `checkout` WHERE
is_confirmed = 0
AND sent_to_reminder = 1
AND DATE(checkout_date - INTERVAL 1 HOUR ) = CURDATE( )
You have a ) in the wrong place. Try
DATE(checkout_date) - INTERVAL 1 HOUR = CURDATE( )
- INTERVAL 1 HOUR needs to go outside of your function call:
SELECT * FROM `checkout`
WHERE is_confirmed = 0
AND sent_to_reminder = 1
AND DATE(checkout_date) - INTERVAL 1 HOUR = CURDATE()
I'm having problems with timediff and DATE_SUB. Here is my MYSQL query:
SELECT id, clock_user_id, clock_date,
(Select clock_time from aura_clock where aura_clock.clock_type = 'Start' and
aura_clock.clock_date = t1.clock_date) as start, (Select clock_time
from aura_clock where aura_clock.clock_type = 'Stop'
and aura_clock.clock_date =
t1.clock_date) as Stop,
THE PROBLEM START HERE
TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time)) as spent
FROM aura_clock t1 WHERE t1.clock_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL
WEEKDAY(CURDATE()) DAY),INTERVAL 15 day)
AND t1.clock_date < DATE_SUB(curdate(),INTERVAL DAYOFWEEK(curdate()) + 6 day)
GROUP BY clock_date
The result is :
Now, I want to subtract 1 hour from the time spent using DATE_SUB but it didn't work.
as mirkobrankovic wrote:
((TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time))) - INTERVAL 1 HOUR) AS new_spent
should work
EDIT:
The best i got is time in seconds :
http://sqlfiddle.com/#!2/18160/66/0
a lot depends on MYSQL version
I have a MySql table having a lot of records. But i want to fetch seperate records of current & previous week.
Query for Current Week Records which works perfectly
SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE())
Query for Previous Week but it dosn't work
SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE()) INTERVAL 7 DAY
SELECT * FROM tbl_track
WHERE WEEK(`date_created`) = WEEK(DATE_SUB(CURDATE(), INTERVAL 7 DAY))
OR:
SELECT * FROM tbl_track
WHERE WEEK(`date_created`) = WEEK(CURDATE() - INTERVAL 7 DAY)
That doesnt look like an index can be used but thats a side issue really. You want your curdate to be - 7 days
SELECT * FROM tbl_track WHERE WEEK(date_created) = WEEK(ADDDATE(CURDATE(), INTERVAL -7 DAY));
I need to find the account created for the current day, et for the last 7 days.
To find my results for today, it works, and I do this :
SELECT * FROM `account` where DATE(created_at) = DATE(NOW())
But I don't know how to do to get the last 7days account.
I tried something like this, but without success :
SELECT * FROM `account` where DATE(created_at) BETWEEN DATE(NOW()) AND DATE(NOW()-7)
Have you an idea ?
in mysql:
SELECT * FROM `account`
WHERE DATE(created_at) > (NOW() - INTERVAL 7 DAY)
Try:
BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
If created_at has an index and you wouldn't like to prevent the optimiser from using it, I would recommend the following pattern (assuming created_at contains both date and time):
WHERE created_at >= CURRENT_DATE - INTERVAL 7 DAY
AND created_at < CURRENT_DATE + INTERVAL 1 DAY
This spans the range from the day exactly one week ago till today (inclusive), so 8 days in total.
also have a look at MySQL functions ADDDATE(), DATE_ADD(), DATE_SUB()
e.g.
ADDDATE(DATE(NOW()), -7)