I have a select query that retrieves all entries that are 3 weeks old.
I want to know if there is an expression like where date> Expr (CURDATE () - 3 WEEKS) or if I must first make calculations of differences in my php script.
The format of my date is a timestamp like that : 2010-06-21 16:59:59
Sincerely,
you can use the following line of code
where date>DATE_SUB(curdate(),INTERVAL 3 WEEK);
You can use the DATEDIFF() function like so: WHERE DATEDIFF(CURDATE(), date) = 21.
Use the actual input of timestamp is better,
as it allow query cache to take effect
From :- http://www.dangrossman.info/2007/04/26/mysql-tuning-disable-query-cache-on-frequently-updated-databases/
Queries that contain non-deterministic functions aren’t cached. That includes CURDATE(), RAND(), or any other function where the output isn’t always the same.
From documentation :- http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html
Related
In my database table, there is a date column i.e. EXPECTED DATE which is in dd-mm-yyyy format, and the datatype of the column is text. Now, I want to convert the format to yyyy-mm-dd. But the date is not changing at all and also when I tried to get the timestamp for the expected date column . I am getting some errors. For date coming I have used this STR_TO_DATE. But the year is not coming like what I expect and the timestamp also.
For example:
select STR_TO_DATE ('30-11-2011', '%d,%m,%y') as date ;
returns a result as
2020-11-30
And for timestamp
select STR_TO_DATE ('2011,11,30 12,30,45', '%y,%m,%d, %H,%I,%S');
I am not getting errors.
Please help me get the correct answers for this problem.
For the first query you need to use the %Y. Remember that it is always better to use "Y" for the years when you are writing a query for year.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For the second one also, you can use '%Y' in the place of '%y'. For minutes, use '%i' not '%I'. For hours and minutes, you can use whatever you like.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");
Refer to the below documentation for more clarification on SQL commands.
You need %Y (capital Y) for the 4 digit year, when using MySQL's STR_TO_DATE. Also, minutes is represented by %i, not %I, the latter which is hours on a 0 to 12 scale. So use:
SELECT STR_TO_DATE('30-11-2011', '%d-%m-%Y');
SELECT STR_TO_DATE('2011,11,30 12,30,45', '%Y,%m,%d %H,%i,%S');
For the first query you need to use the %Y'.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For minutes, use this one only '%i'.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");
I need to get number of days between 2 dates, a given one and current date.
But in pure SQL, I mean without usign functions, it is possible?
For exaple
SELECT days (t.givenDate) - days (current date) FROM table t
Have you any idea?
Thaks a lot.
The built-in function is datediff(). The equivalent for the above is:
SELECT datediff(t.givenDate, curdate()) FROM table t;
Normally, givenDate would be in the past and you would want the arguments in the other order.
I am having a table as follows in MYSQL:
proj_id|hoursWorked|Date.
The date field is of type Date; I want to retrieve all the entries from a table depending on a given week number for the project in my java based web application. Please help me to achieve this.
I am unable to write a single query that will allow me to do so.
Do not use something like WHERE WEEK(column)=something - this is a performance killer: It will calculate the week number on all rows, even if they don't match. In addition to that it will make it impossible to use an index ont this column.
Instead calculate an absolute begin and end date or point in time, depending on your data type, then use BETWEEN. This will do no calculations on non-matching rows and allow the use of an index.
Rule of thumb: If you have the choice between a calculation on a constant and on a field, use the former.
use MySQL WEEK() function.
SELECT WEEK(dateColumn)
FROM...
WHERE WEEK(dateColumn) = 1
WEEK()
from MySQL Docs
This function returns the week number for date. The two-argument form
of WEEK() enables you to specify whether the week starts on Sunday or
Monday and whether the return value should be in the range from 0 to
53 or from 1 to 53.
Use WEEK
select * from your_table
where week(`Date`) = week('2012-12-01')
If you want to get only records from the current week you can do
select * from your_table
where week(`Date`) = week(curdate())
Hello i have a table like this:
|user_name|pw|register_date|last_time_entered
I want to get all the rows where last_seen_date - register_date < 7
I dont know how to write this query i thought about something like this
SELECT * FROM workoutlog_1.personal
WHERE DATEDIFF(day, workoutlog_1.personal.register_date, workoutlog_1.personal.last_time_entered) < 7;
But i get this error:
Error Code: 1582. Incorrect parameter count in the call to native function 'DATEDIFF'
Thanks for helping.
Your error code seems to come from mysql.
With mysql, datediff takes only 2 parameters (day is not needed)
I think that in (really) old versions, it took 3 parameters, now it works only with 2, and it will return days.
If you had to work with another unit (hour for example), you could use TIMESTAMPDIFF
MySql's DATEDIFF differs from SqlServers DATEDIFF in that it takes only 2 date parameters, and returns the difference in days. Since you want days anyway, just remove the day parameter, i.e.
SELECT * FROM workoutlog_1.personal
WHERE DATEDIFF(workoutlog_1.personal.register_date,
workoutlog_1.personal.last_time_entered) < 7;
Query 1 works but query 2 doesn't:
Query #1:
SELECT * FROM `users` WHERE users.dob <= '1994-1-14' AND users.dob >= '1993-1-14' LIMIT 10
Query #2:
SELECT * FROM `users` WHERE users.dob BETWEEN '1994-1-14' AND '1993-1-14' LIMIT 10
The 2nd one should be able to do the same thing as the first but I don't understand why it's not working.
The dob (date of birth) field in the users table is a type date field with records that look like this:
1988-11-08
1967-11-14
1991-03-09
1958-03-08
1967-06-30
1988-10-19
1986-01-23
1965-09-20
YEAR - MONTH - DAY
With either query #1 or #2 I'm trying to get back all users who are between 18 and 19 years of age, because 1994-1-14 is exactly 18 years from today and 1993-1-14 is 19 years from today. So is there a way to get the between query to work?
By not working I mean it doesn't return any records from the db while the working query does.
Also is the between query more efficient or is the performance difference negligible?
To answer the first part: "expr BETWEEN min AND max". Try switching those 2 dates in the second query.
The usage is wrong. See the BETWEEN documentation:
expr BETWEEN min AND max is equivalent to (min <= expr AND expr <= max).
Therefore, users.dob BETWEEN '1994-1-14' AND '1993-1-14' is the same as ('1994-1-14' <= users.dob AND users.dob <= '1993-1-14'), of which there will never be more than 0 results.
Simply reverse the order :)
There will be no performance difference when using either form, possibly subject to the note below. This transformation happens at the query planner level. However, if you have concerns, remember to profile, profile, profile. Then you can see for yourself and appease the premature-optimization demons.
Also note the ... note:
For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.