How to convert UTC to EST or EDT in my sql - mysql

I have created a table, which is given below,
CREATE TABLE `user` (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
department VARCHAR(100) NOT NULL,
submission_date DATE,
PRIMARY KEY ( id )
);
I have inserted data from user details table to user table. user details table contains utc date format in column submission_date.
Insertion query is given below,
INSERT INTO user
(name,department,submission_date)
SELECT name,department,submission_date FROM user_details;
How to convert utc to est/edt in my sql

Use CONVERT_TZ to achieve this:
Note that -05:00 is for EST. You can modify this as per your need.
INSERT INTO user
(name,department,submission_date)
SELECT name,department,convert_tz(submission_date , '+00:00', '-05:00')
FROM user_details;
db<>fiddle

Related

How to design Cassandra Scheme for User Actions Log?

I have a table like this in MYSQL to log user actions :
CREATE TABLE `actions` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`module` VARCHAR(32) NOT NULL,
`controller` VARCHAR(64) NOT NULL,
`action` VARCHAR(64) NOT NULL,
`date` Timestamp NOT NULL,
`userid` BIGINT(20) NOT NULL,
`ip` VARCHAR(32) NOT NULL,
`duration` DOUBLE NOT NULL,
PRIMARY KEY (`id`),
)
COLLATE='utf8mb4_general_ci'
ENGINE=MyISAM
AUTO_INCREMENT=1
I have a MYSQL Query Like this to find out count of specific actions per day :
SELECT COUNT(*) FROM actions WHERE actions.action = "join" AND
YEAR(date)=2017 AND MONTH(date)=06 GROUP BY YEAR(date), MONTH(date),
DAY(date)
this takes 50 - 60 second to me to have a list of days with count of "join" action with only 5 million rows and index in date and action.
So, I want to log actions using Cassandra, so How can I design Cassandra scheme and How to query to get such request less than 1 second.
CREATE TABLE actions (
id timeuuid,
module varchar,
controller varchar,
action varchar,
date_time timestamp,
userid bigint,
ip varchar,
duration double,
year int,
month int,
dt date,
PRIMARY KEY ((action,year,month),dt,id)
);
Explanation:
With abobe table Defination
SELECT COUNT(*) FROM actions WHERE actions.action = "join" AND yaer=2017 AND month=06 GROUP BY action,year,month,dt
will hit single partition.
In dt column only date will be there... may be you can change it to only day number with int as datatype and since id is timeuuid.. it will be unique.
Note: GROUP BY is supported by cassandra 3.10 and above

how to check to modify the definition

CREATE TABLE Hotel
(roomNum INTEGER NOT NULL,
arrD DATE NOT NULL,
depD DATE NOT NULL,
guestName CHAR(30) NOT NULL,
PRIMARY KEY (roomNum, arrDate));
how can I modify the definition to keep a check so that no room is booked before the departure date of previous allocation.
You can do an INSERT with a NOT EXISTS clause to check the validity of the date:
INSERT INTO Hotel (roomNum, arrD, depD, guestName)
SELECT 1, '2017-02-08', '2017-02-11', 'Tim Biegeleisen'
FROM dual
WHERE NOT EXISTS (SELECT * FROM Hotel WHERE roomNum = 1 AND depD > '2017-02-08')
I don't think that modifying the table definition will make it possible to enforce your logic.

Auto add days to datetime as each days passes in mysql

I want to make a column in mysql database that when user login first time in system, it stores that datetime in mysql table. And since that day in other column days will add according to his register date. Like 1, 2, 3,....and so on. So, is there any way I can achieve the results? Please guide me soon.
You can do this with just one column (to hold the registration / first login date) and the DATEDIFF function:
CREATE TABLE users (
ID int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
registered_at datetime NOT NULL,
PRIMARY KEY (ID)
);
INSERT INTO users SET
name = 'myname',
registered_at = NOW();
SELECT registered_at, DATEDIFF(NOW(), registered_at) AS days_since
FROM users
WHERE name = 'myname';

Set a datetime column with a SQL trigger

I using a MySQL server (5.5.27 - Community Server). I have a table with this definition:
CREATE TABLE IF NOT EXISTS tbl_messages (
`msg_id` VARCHAR(50) NOT NULL ,
`msg_text` VARCHAR(50) NULL ,
PRIMARY KEY (`msg_id`);
I write a trigger that, when I do an insert, the server sets the msg_id column with the current time including microseconds with this format "yyyymmddhhnnssuuuuuu". "u" is for microseconds.
I created a trigger:
create trigger tbl_messages_trigger
before insert on tbl_messages
for each row
BEGIN
SET NEW.msg_id = DATE_FORMAT(NOW(),'%Y%m%d%H%i%s%f');
END;$$
But the msg_id column only gets values like this: 20130302144818*000000*, with microseconds in zero. ¿Is it possible capture the microseconds?
TIA,
From the code provided I guess that you are trying to use microsecond to minimize probability of getting same msg_id for different rows.
Also, msg_id is the primary key, which should not present any object-specific data but only be unique. There is a good link about: http://en.wikipedia.org/wiki/Surrogate_key
The best way to deal with primary keys in MySql is AUTO_INCREMENT column attribute. If you need insert time for messages, you may provide column for it:
CREATE TABLE tbl_messages
(
`msg_id` bigint(20) NOT NULL AUTO_INCREMENT,
`msg_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`msg_text` VARCHAR(50) NULL,
PRIMARY KEY (`msg_id`)
);

Create a table and insert data in one statement MySQL

I'm trying to create a table and insert data into it in one statement but I can't figure out just how to do it.
The statement has to create a table that has 3 columns: stock symbol, price, and quote date.
The symbol data is supposed to be SELECT'ed from another table, my_stocks. The price and quote date need to be inserted manually.
I tried the following statement, but I keep getting a syntax error:
CREATE TABLE stock_prices (
symbol VARCHAR(20) NOT NULL,
quote_date DATE NOT NULL,
price DECIMAL(5,3) NOT NULL,
) SELECT symbol FROM my_stocks;
I'm running MySQL Community Server 5.5.28, btw.
Edit: I got it, the appropriate statement is:
CREATE TABLE stock_prices (
quote_date DATE NOT NULL,
price DECIMAL(5,5) NOT NULL
) SELECT `symbol`, SYSDATE() AS quote_date, 0 AS price FROM my_stocks;
CREATE TABLE stock_prices (
symbol VARCHAR(20) NOT NULL,
quote_date DATE NOT NULL,
price DECIMAL(5,5) NOT NULL,
) SELECT symbol, now(), 0 FROM my_stocks;