I am trying to write a query to select all records from users table where User_DateCreated (datetime field) is >= 3 months from today.
Any ideas?
SELECT *
FROM users
WHERE user_datecreated >= NOW() - INTERVAL 3 MONTH
If you want to ignore the time of day when a user was created you can use the following. So this will show someone created at 8:00am if you run Quassnoi's example query at 2:00pm.
SELECT *
FROM users
WHERE DATE(user_datecreated) >= DATE(NOW() - INTERVAL 3 MONTH)
Using DATE(user_datecreated) prevents mysql from using any indexes on the column, making the query really slow when the table grows.
You don't have to ignore the time when the user was created if you remove the time from the "3 months ago" date, as all the users created that day will match the condition.
SELECT *
FROM users
WHERE user_datecreated >= DATE(NOW() - INTERVAL 3 MONTH);
Related
I am creating a booking management system. I got stuck in the booking cancellation part. I want to allow users to cancel their orders if their booking time and the current time duration is between 2 hours because I want to restrict the users to cancel their booking if their booking time and current time duration is greater than or equal to 2 hours.
I want to generate a query that returns all the bookings whose booking time is less than 2 hours. How can I achieve this?
This is my database structure.
SELECT * FROM `TableName` where TIMEDIFF(NOW(),Your_date_ColumnName) < '02:00:00.000000'
Assuming that booking_time is in MySQL standard format.
Try this and the below query will use index if you have one in booking_time column
SELECT *
FROM booking_table
WHERE booking_time BETWEEN CURRENT_TIMESTAMP() - INTERVAL 2 HOUR AND CURRENT_TIMESTAMP()
You can extract the hour part of your date. Refer to this link
Then using this query to get those less than 2 hours.
SELECT * FROM table1 WHERE tdate - 120 < EXTRACT(MINUTE FROM now())
I am working on a script to select milestones from the DB that are only within 5 minutes (or any other set time amount) of the query. I'm having a few problems.
The dates are stored in the database in this format:
example: 2016-12-05 21:19:00
when I run this query:
SELECT * FROM milestones WHERE status="planned" and time >= NOW()
I am able to get the results for everything in the future, but how would I go about limiting the query to say only 5 minutes for example.
You can use BETWEEN and DATE_ADD MySQL functions to achieve the desired result, e.g.:
SELECT *
FROM milestones
WHERE status="planned" and time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 MINUTE)
This would print all the entries having date between current date and next 5 minutes.
If you want to print the same for last 5 minutes, you can use the following:
SELECT *
FROM milestones
WHERE status="planned" and time BETWEEN DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND NOW()
Here's MySQL's documentation for DATE_ADD and BETWEEN.
I want to get the product created five days before. From start of fifth day to end of fifth day. I have shown my table structure.
What will be SQL query for getting for products created exactly before five days.
I guess you are storing UNIX TIME.
To get the records of 5th day backward from current date:
SELECT
*
FROM your_table
WHERE `timestamp` >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 5 DAY)
AND `timestamp` < UNIX_TIMESTAMP(CURDATE() - INTERVAL 4 DAY)
Use like this,
SELECT * FROM table_name WHERE DATE(timestamp) = '2016-06-08'
in employees timeentry system each employee will enter the no of hours worked on a daily basis.To reduce the work i want to copy the last week time entries ie (from Mon - Fri) to the present week ie(mon-friday)
I am able to fetch the last week data from my table as mentioned below
$query = "select * from table_name where (date(start_time) >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND date(start_time) < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)";
Now i want to insert the data fetched from last week to be in the present week
lets for in this ie from(1july to 5july)
i am using php and mysql
plz suggest how do i insert for the present week
if you want a simple insert of new rows with same values.
do this,
create the select query,
and create an insert query above it that will simply insert new rows and will add 7 days to the time reported.
something like this:
insert into `theTable`
select DATE_ADD(theHour, INTERVAL 7 DAYS)
from `theTable`
syntax may be wrong but this is the idea.
Simply add seven days to the dates in your SELECT query and INSERT them
Something like
INSERT INTO timeentry
SELECT employeeId, DATE_ADD(date(start_time), INTERVAL 7 DAY), --...
FROM timeentry
WHERE (date(start_time) >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date(start_time) < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
hi i have players table in mysql .
i need to select inactive players who have not played a match for 6 months in the league .
for example ->
select * from players where player (matchplayed == true && (in last 6 months condition));
i am newbie in mysql
so will like some help how to deal with time .
some thing like this may work:
SELECT * from players where player_matchplayed == true
&& lastmatch_played_date > DATE_SUB(curdate(),INTERVAL 6 MONTH);
DATE_SUB(curdate(),INTERVAL 6 MONTH) will give you the interval of last six months from current date. Which you can use to check the lastmatch_playeddate is greater than it or not.
Hope this helps .
Try it with this query
SELECT * FROM players
WHERE matchplayed IS NOT NULL
AND last_match < NOW() - INTERVAL 6 MONTH
last_match is a mysql DATE field with the last match information
matchplayed is either NULL (not played) or another value for already played
Have a look to mysql date / time functions: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
I think that DATE_SUB will do the job or a DATE_DIFF for comparison.