MAX() + ADD_DATE - per update su Mysql - mysql

I'm trying to perform a field update on mysql with a MAX() value getting other columns of the same table .
For instance, I've this table:
id starting_date activity_1 activity_2 activity_3
1 0000-00-00 10 5 12
I'm trying this query (it doesn't work):
$today="2022-07-20"; //It's a dynamic var generate via date()
UPDATE table_name SET starting_date = DATE_ADD('2022-07-20',(INTERVAL (SELECT GREATEST(activity_1,activity_2,activity_3) FROM table_name WHERE id ='1') MONTH) WHERE id ='1'
My desire is to add 12 months (or the greatest value) to 2022-07-20...
I'm trying several queries with no positive result
Any idea around?
Thanks

Use multiple-table UPDATE syntax.
UPDATE table_name
JOIN ( SELECT id,
GREATEST(activity_1,activity_2,activity_3) interval_for_update
FROM table_name ) value_for_update USING (id)
SET starting_date = '2022-07-20' + INTERVAL interval_for_update MONTH
-- WHERE id = 1
PS. Never enclose numeric literal values with the quote chars - this converts them to strings and causes excess implicit data convertions.

Related

String handling in MySQL - calculate days since date

I have a table that looks like this:
table
---------------------------------------
id last_update
---------------------------------------
1 2020-02-22T00:32:04.254975Z
2 2020-02-22T02:09:27.057131Z
3 2020-02-22T01:38:48.739303Z
4 2020-02-21T06:19:17.832257Z
5 2020-02-14T03:10:02.551126Z
6 2020-02-21T23:17:01.907037Z
id is INT and last_update is VARCHAR (because the format is not supported as a DATE TIME type in MySQL).
What I want to do is to create a new column called "days_since_last_update" that calculates the number of days between today's date and the "last_updated" date.
How can this be done in a MySQL query?
Thanks, x
You should be storing your timestamps in a proper datetime column. That being said, we can try working around the text dates using STR_TO_DATE. I would actually suggest not adding a new column, since your ask is just for derived data. Instead, generate this column when you query:
SELECT
id,
last_update,
DATEDIFF(STR_TO_DATE(last_update, '%Y-%m-%dT%H:%i:%s'),
(SELECT STR_TO_DATE(t2.last_update, '%Y-%m-%dT%H:%i:%s')
FROM yourTable t2
WHERE t2.id < t1.id ORDER BY t2.id DESC)) AS days_since_last_update
FROM yourTable t1
ORDER BY t1.id;
This would place the first days since last update value as NULL, as there is no earlier update recorded for the table. Though, if you want a default value, that can easily be included.
If you instead want the difference in days between the last_update column and today's date, then use:
SELECT
id,
last_update,
DATEDIFF(CURDATE(),
STR_TO_DATE(last_update, '%Y-%m-%dT%H:%i:%s')) AS days_since_last_update
FROM yourTable
ORDER BY id;

Order by in a update query

UPDATE table1
SET date1 = DATE_FORMAT(DATE_ADD(STR_TO_DATE(date1,'%m/%d/%Y'),INTERVAL 1 DAY),'%m/%d/%Y'),
date2 = date2 + 1*24*60*60
ORDER BY STR_TO_DATE(date1,'%m/%d/%Y') DESC;
I am trying to update all dates in table1 1 day a head (using MySql), I am using the query above.
in table1, ad_id + date1 represent a uniqe key and thats why I am using the Order By line.
this query worked on my local data base but when I run it on anther data base I get this error:
Duplicate entry '40001305194-07/02/2018' for key 'ad_id_2'
this doesn't make sense since in the data base their is no date 07/02/2018 for this ad_id.
and for date 07/01/2018 there is only 1 line for this ad_id.
what am I doing wrong, thanks in advance :)

Update multiple values, in different columns, to null based on what the value is intitially in Mysql

There are different date columns in a table and some rows in the table have 1st of January values, "YYYY-01-01".
I can do a query and find all those rows with any date field that has 1st Jan YYYY.
select * from table
where dob like '%01-01'
OR start_date like '%01-01'
OR ...
But how do I then set those values to NULL?
I've tried:
update table
set dob = NULL
where dob='%01-01';
update table
set start_date= NULL
where start_date='%01-01';
...
That doesn't work...
I would use this:
UPDATE table SET dob = null WHERE dob LIKE '%01-01'
EDIT: corrected
Use update:
update table
set start_date = NULL
where day(start_date) = 1 and month(start_date) = 1;
update table
set dob = NULL
where day(dob) = 1 and day(dob) = 1;
You might as well do a separate update for each column. Although the logic can be encapsulated into a single query, that is just a more complicated query.
Don't use like on dates. like is for strings. MySQL has plenty of appropriate date functions.
Do note: Some people really are born on January 1st.

SQL order by and SQL date

I have 2 doubts related to SQL query's
id name update
1 some 2013-05-03
2 som 2013-05-08
3 smee 2013-06-05
How can i list items on a particular month (I want all records,year and date will not be specified I just want to check the month)
How can I arrange name in alphabetic order and arrange it as groups of names such as (limiting number of records =10)
Array A = names starting with A
Array B = names starting with B
The easiest way, to fetch MONTH from a DATE or DATETIME type of fields is to use the MySQL's date-time function MONTH().
For your query, it shall be:
SELECT *
FROM tblName
WHERE MONTH( `update` ) = <month Number such as 5>
The second would need a more complex query. I'd rather use php to do the grouping better(as I've more control over that language).
You can simply use datatype of the field as DATE or you can store any date as unix timestamp and then convert it whenever you want to show it.
Example: 1363979714 (ISO 8601:2013-03-22T19:15:14Z)
If you want list items on a particular date, you can write your query like this:
Month:
Select * from tableName where update like '%-5-%'
day:
Select * from tableName where update like '%-16'
year:
Select * from tableName where update like '2013-%'

Need to know if between and like can be used together or something better in sql

Here is my query:
SELECT count(*)
FROM table_testcase_execution
WHERE campaign_session_id = any(SELECT campaign_session_id
FROM table_campaign_session
WHERE campaign_session_name = 'sitename')
AND timestamp BETWEEN "1288929643485" AND "1289010305536"
This works just fine, the problem is that I have to use a LIKE on these because they contain 3 extra digits (so this is a timestamp plus 3 digits).
Therefore I am using a strtotime in php and trying to match these in the database, but they contain the extra 3 digits. Is there a way I can redo this sql or add a LIKE clause for each of these timestamps?
"1288929643485" AND "1289010305536"
Something like
SELECT count(*)
FROM table_testcase_execution
WHERE campaign_session_id = any(SELECT campaign_session_id
FROM table_campaign_session
WHERE campaign_session_name = 'sitename')
AND timestamp between LIKE "1288929643%" AND "1289010305%"
Assuming that the 3 extra digits can range from 000 to 999 then you could do something like this:
select count(*) from table_testcase_execution
where campaign_session_id = any(
SELECT campaign_session_id
FROM table_campaign_session
WHERE campaign_session_name = 'sitename')
AND timestamp >= "1288929643000" AND timestamp <= "1289010305999"
This along with an index in timestamp column should give you good performance.
Since the timestamps in your query are strings, can you not just truncate the unwanted three digits with SUBSTRING()?
select count(*)
from table_testcase_execution
where campaign_session_id = any(SELECT campaign_session_id FROM table_campaign_session WHERE campaign_session_name = 'sitename')
AND timestamp between SUBSTRING(#timestamp1, -3) AND SUBSTRING(#timestamp2, -3)
(assuming #timestamp1 and #timestamp2 are the respective timestamp values supplied to the query...)