I have problem with my MySQL query.
I would like to query for delete rows oldest by 7 day from my table. This table has day, month and year in different rows.
This is my query:
DELETE FROM
`logowanie_dj`
WHERE
`miesiac`
IN (
SELECT CONCAT(dzien,':', miesiac,':', rok) < 21:1:2014'
)
However mysql_query() is deleting all the rows.
I presume that you mean year, month and day are in different columns?
Then this might work:
DELETE FROM `logowanie_dj` WHERE CAST(CONCAT(dzien,':',miesiac,':',rok) AS DATE) < CAST('21:1:2014' AS DATE)
It creates a date value in the form dd:mm:yyyy from the cells of the row and deletes it if it is before 21:1:2014.
Please note that you obviously use some date format that is non-english. MySQL might have a problem with that, so that it could be you need to do this:
DELETE FROM `logowanie_dj` WHERE CAST(CONCAT(rok,'-',miesiac,'-',dzien) AS DATE) < CAST('2014-01-21' AS DATE)
check the date and time format is correct or not. Try with this format 'yyyy:mm:dd hh:mm:ss'
You may want to try casting the two strings into dates so it knows to do the comparison as dates.
"For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE."
Source
* Backup your database before trying this *
DELETE FROM `logowanie_dj`
WHERE ABS(UNIX_TIMESTAMP()-UNIX_TIMESTAMP(CONCAT(rok,'-',miesiac,'-',dzien))) > 604800
Try with below code
DELETE FROM
`logowanie_dj`
WHERE
`miesiac`
IN (
SELECT CONCAT(dzien,':', miesiac,':', rok) < '2014:01:21 00:00:00'
)
Related
I have a problem related to MySQL query, I use WAMPServer.
I have data in database which have range of dates but when I select data for example
select * from CHD WHERE addtime>='2018-06-15' and addtime<='2018-06-21';
It displays data from '2018-06-15' to '2018-06-20', data of 2018-06-21 are not displayed even if I do
select * from CHD where addtime='2018-06-21';
is not working
Please anyone can help me
This assumes that your column is of type datetime.
The shorthand version of your date in the filter clause is assumed to be at midnight of the date. Your values that you are attempting to retrieve have times after midnight of that date. You either need to define a timestamp along with the date, or you need to filter by the day after for less than equal to or the day before for greater than equal
I have stored dates (dd/mm/yyyy) in text format in a table in a field called dates. I want to compare those dates with the current date and if the dates are smaller (if the dates have passed) to move the entire row into a new table called archive. Tried something with the DATEDIFF() but I'm new to MySQL and can't figure it out.
I'm going to preface my answer with a short remark: storing "date" values in SQL database in VARCHAR columns is an anti-pattern. MySQL provides native datatype DATE which is designed to handle "date" values. But that's just a remark, doesn't answer your question.
You can use the convenient MySQL STR_TO_DATE function to convert strings into DATE values. For example:
STR_TO_DATE('15/05/2015','%d/%m/%Y')
You could use a column reference in place of the literal, e.g.
STR_TO_DATE(t.mycharcol,'%d/%m/%Y')
and that will return a DATE value you can compare to another DATE value, using the standard inequality operator < for example.
To return the current date from the database, you can use an expression such as
DATE(NOW())
Putting that together, you could write a query like this:
SELECT t.*
FROM t
WHERE STR_TO_DATE(t.mycharcol,'%d/%m/%Y') < DATE(NOW())
If you want to take the result from a SELECT statement and insert those rows into another table, you can use the INSERT ... SELECT form of the INSERT statement.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Beware of the behavior with badly formatted or invalid dates, e.g.
SELECT STR_TO_DATE('35/05/2015','%d/%m/%Y')
, STR_TO_DATE('15-05-2015','%d/%m/%Y')
I have a table with a few records and for each of these records I've also added a UNIX_TIMESTAMP. Now, I also have a search engine for those records in which I can choose a date using a jQuery datapicker. My question is how do I make the request so that to select all timestamps from the database for a certain date.
With an index on your timestamp column you will get a faster result with:
SELECT *
FROM table_name
WHERE time_stamp_column_name >= :date_picked
AND time_stamp_column_name < :date_picked + INTERVAL 1 DAY
Where :date_picked is your bound-in picked date as a string in the format 'YYYY-MM-DD'.
You can use from_unixtime to convert it into a date
select *
from
table
where
date(from_unixtime(your_timestamp_col)) = '2014-10-01'
use unix timestamp
you can also see this
Hi there please help me if you can. Here is my senario:
I have a MySQL database with a column that holds a date in the form of a varchar. The format of the date is the following 29/05/2014 (i.e. d/m/Y).
I'm trying to compare the value of this column with todays date and return any rows where the date is earlier than todays date.
I'm using a php variable to store todays as follows:
$date = date("d/m/Y");
Here is my SQL query:
SELECT * FROM patients WHERE last_seen < '$date'
What gets returned
So what is returned is very unusual (to me). All records where the last_seen "day" is less than todays "day". It seems to be overlooking the month and year. So in other words if I last_seen = "30/05/2014" and todays date is "29/05/2014" this record is still returned.
Does anyone have any ideas what I might be doing wrong here?
Thanks
You really, really shouldn't store dates in a varchar field - use date or datetime or timestamp data type.
That said, sometimes you don't have control over the database and you have to deal with somebody else's bad design decision. In this case, to compare dates, convert the varchar strings to dates and compare them that way. So, in your case, you can have something like this:
$date = date("d/m/Y");
and then
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < str_to_date('$date', '%d/%m/%Y')
or simpler
SELECT * FROM patients WHERE date(last_seen) < current_date
This way you are actually comparing dates and not strings containing dates. Naturally, this assumes that all dates are stored in the same format.
EDIT: I just tested the last option - and, apparently, date('30/05/2014') returns NULL on my system (mysql 5.5 on linux), hence I suggest the best way is
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < current_date
You need to store your date as DATE or DATETIME in your database.
Then you can use:
SELECT * FROM patients WHERE DATE(last_seen) < CURRENT_DATE
I have a column in my database table that holds dates in the format (mmm-yy) Ex: Aug-13. I'm trying to select all dates after Jun-12. The problem is the column is a string so when I say WHERE column > 'Jun-12' it gives me values that are alphabetically greater than Jun-12 such as May-12, which is not what i want. Any thoughts on how to go about this?
Store dates as dates, not strings. That's the recommended way of doing it and I recommend changing your code accordingly.
In this case, you could use STR_TO_DATE function to compare the dates on the fly:
SELECT * FROM table_name WHERE column > STR_TO_DATE('Jun-12','%m-%y');
Hope this helps!
In this situation you'll should be able to convert to a date and then filter as required, the below code should give you a guide,
SELECT *
FROM Table
WHERE CAST('01-' + column AS DATETIME) > '01-JUN-12'