Update Varchar entries in MySQL table - mysql

CREATE TABLE `Schedule` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
`deptime` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3221 DEFAULT CHARSET=latin1;
The field deptime contains entries in the following format: 2012-09-04 09:17.
Now I need to change the day of all entries in this field, i.e. 2013-07-01 09:17. The time, i.e. 09:17 should not be changed. How can I do this in a quick way using some UPDATE query?

Or try this...
UPDATE schedule SET deptime = CONCAT('2013-07-01 ',TIME(deptime));

Try this-
update table Schedule
set deptime = STR_TO_DATE(deptime,'%Y-%m-7 %h: %i');
OR
update table Schedule
set deptime = STR_TO_DATE(deptime,'%Y-%m-$PHPVar %h: %i');
1) Give the hardcode value in day field or provide PHP variable if you have.
2) use %H for 24 hrs format and %h for 12 hrs format in str_to_date function

Related

When i Create table with Month he only given 0000-00-00

I create table for hour
CREATE TABLE hour (
Name1 varchar(25) not null,
Datee Datetime not null DEFAULT (CURRENT_TIMESTAMP()),
Monthh date not null DEFAULT (MONTH(CURRENT_DATE()))
);
Mysql only give me 0000-00-00 not Name where i use button in php. What is wrong with this and how correct? In my opinion my phpadmin dont have a MONTH function
CREATE TABLE hour ( Name1 varchar(25) not null,
Datee DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
Monthh TINYINT NOT NULL DEFAULT (MONTH(CURRENT_TIMESTAMP))
);
fiddle
PS. HOUR is Reserved word - I'd recommend to rename a table.
Store full timestamp and it's month part in separate columns in same table is absolutely redundant. You can story only datetime and get it's month in select query:
CREATE TABLE Godzina (
Nazwa varchar(25) not null,
Randka Datetime not null DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO Godzina (Nazwa) VALUES ('TestName');
SELECT
Nazwa,
Randka,
MONTH(Randka) AS Miesiac
FROM Godzina;
Test it on SQLize.online

How can I use a trigger to update several tables taking specific data from rows in a table being updated?

I have searched quite a bit and can't seem to find the answer to this. I am fairly new to MySQL and this is something I had previously written as a php script to update the individual tables, but would rather it update automatically on the database when the table changes.
When I update a table via a google script I then require about 6 different tables to be updated from different rows in the main table, by date, and each table requires data from a different column.
I know this is not the correct way to do it but I need the trigger to effectively do the following;
CREATE TRIGGER dash_1
ON global_summary AFTER UPDATE
UPDATE widget_dash_2 (date, metric)
WHERE inc_id=100 SELECT date, volume
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 1))
UPDATE widget_dash_2 (date, metric)
WHERE inc_id=101 SELECT date, volume
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 8))
This would be for one of the columns "volume" from table "global_summary", I then need to add up to another 5 updates such as;
UPDATE widget_dash_3 (date, metric)
WHERE inc_id=100 SELECT date, score
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 1))
UPDATE widget_dash_3 (date, metric)
WHERE inc_id=101 SELECT date, score
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 8))
Where "score" is another column in the table "global_summary".
Any guidance would be much appreciated. I have been looking for some guidance on this for days now and it has just made me more confused.
I'm assuming I need to do something with arrays and initially getting all data from the main table, but how can I then carve that up?
#sticky bit,
Unfortunately I have to put this data into these small tables, believe me I would much rather just pull it from global_summary directly.
CREATE TABLE `global_summary` (
`id` int(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`volume` int(10) DEFAULT NULL,
`volume_problem` int(10) DEFAULT NULL,
`removed` int(10) DEFAULT NULL,
`score` int(10) DEFAULT NULL,
`error` int(10) DEFAULT NULL,
`total` int(10) DEFAULT NULL,
`previous` int(10) DEFAULT NULL,
`entryID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
All the widget_dash tables;
CREATE TABLE `widget_dash_2` (
`inc_id` int(11) NOT NULL,
`date` date DEFAULT NULL,
`metric` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Thanks for your feedback so far, and yes I probably was confusing UPDATE with INSERT.

DATE_ADD() with TIME column type to Expire a record

I have two tables, KEY_TYPE and USER_KEY:
CREATE TABLE `KEY_TYPE` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`internal_name` varchar(16) NOT NULL,
`expiration` time NOT NULL DEFAULT '00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_application_internal_name` (`internal_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `USER_KEY` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`uuid` char(36) NOT NULL,
`key_type_id` int(11) NOT NULL,
`CREATE_DATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I want to DELETE a USER_KEY once it's CREATE_DATE plus the KEY_TYPE.expiration is greater than NOW(). I'm going to set up an event to accomplish this (assuming this is best practice?). However, I can't figure out how to add the Time data type expiration column to the CREATE_DATE column properly. I think I'm close, but the DATE_ADD is adding more than 24 hours for example to the CREATE_DATE. So far my query is as follows:
SELECT USER_KEY.create_date, KEY_TYPE.expiration,
DATE_ADD(USER_KEY.create_date, INTERVAL KEY_TYPE.expiration SECOND) CREATE_PLUS_EXPIRATION
FROM USER_KEY
JOIN KEY_TYPE ON USER_KEY.key_type_id = KEY_TYPE.id;
I've been playing around on DB Fiddle with the following example.
Thanks, #P.Salmon for pointing me in the right direction. I ended up changing the INTERVAL SECOND to INTERVAL HOUR_SECOND which seemed to do the trick.
This is the event I ended up with:
SET GLOBAL event_scheduler = ON;
CREATE EVENT EVENT_EXPIRE_USER_KEYS
ON SCHEDULE EVERY 1 HOUR STARTS CURRENT_TIMESTAMP
DO
DELETE USER_KEY
FROM USER_KEY
JOIN KEY_TYPE ON USER_KEY.key_type_id = KEY_TYPE.id
WHERE DATE_ADD(USER_KEY.create_date, INTERVAL KEY_TYPE.expiration HOUR_SECOND) < NOW();
But this also works as well :
SET GLOBAL event_scheduler = ON;
CREATE EVENT EVENT_EXPIRE_USER_KEYS
ON SCHEDULE EVERY 1 HOUR STARTS CURRENT_TIMESTAMP
DO
DELETE USER_KEY
FROM USER_KEY
JOIN KEY_TYPE ON USER_KEY.key_type_id = KEY_TYPE.id
WHERE DATE_ADD(USER_KEY.create_date, INTERVAL TIME_TO_SEC(KEY_TYPE.expiration) SECOND) < NOW();

mysql calculate duration from two dates

Hi I have a log database table in mysql which captures a start date and time and an end date and time.
The start and now the stop time is inserted to the record.
I have a third field which is duration which I would like displayed as hh:mm:ss
The schema looks like this at present
CREATE TABLE IF NOT EXISTS `log` (
`uid` int(20) NOT NULL AUTO_INCREMENT,
`room` int(11) NOT NULL,
`start` datetime NOT NULL,
`stop` datetime NOT NULL,
`duration` datetime DEFAULT NULL,
`participants` int(3) NOT NULL,
`recorded` varchar(3) NOT NULL,
`rec_file` varchar(35) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Log File' AUTO_INCREMENT=106 ;
I'm trying to create a trigger which calculates the duration and writes it to the duration field as data is inserted
Currently the trigger I am trying to debug looks like this
BEGIN
SET NEW.duration = (TIMEDIFF (NEW.start,NEW.stop)) ;
END
The result is the duration field remains set to 0000-00-00 00:00:00.000000
Any suggestions on how to make this work are greatfully recived
Try to add the following :
Change duration type to TIME instead of DATETIME. According to documentation, the result returned by TIMEDIFF() is limited to the range allowed for TIME values.
This is the reason you are receiving all zeros currently.
I assume that stop time will always be later than start time, so I would write the trigger in the following way:
CREATE TRIGGER your_schema.insert_duration BEFORE INSERT ON your_schema.log
FOR EACH ROW SET NEW.duration = TIMEDIFF(NEW.stop, NEW.start);
The result will be in HH:MM:SS format for duration field.

mysql count groupings

CREATE TABLE `articles_entities` (
`id` CHAR(36) NOT NULL,
....
`created` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `created` (`created`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8;
I am trying to create a query that gives me the count of records by day.. e.g
Day 1: 23
Day 2: 343
etc...
Please note the output is not the exact format I want, just a display of what data I want.
While writing the question I realised just how easy this is....
SELECT COUNT(*), DATE_FORMAT(created, '%Y%m%d') AS testgroup FROM articles_entities GROUP BY testgroup;