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
Related
I have a table named as data_stats. Please check below respective schema:
CREATE TABLE `data_stats` (
`uuid` varchar(255) PRIMARY KEY NOT NULL COMMENT 'Primary Key',
`state` varchar(255) NOT NULL DEFAULT 'Active',
`created_at` BIGINT(20) NOT NULL COMMENT 'Timestamp of when the record was created',
`updated_at` BIGINT(20) NOT NULL COMMENT 'Last updated timestamp');
I am storing created_at and update_at timestamp in the epoch format.
Now I need to find all the records that are updated today (i.e update_at date == today's date)
I tried below query
SELECT * FROM `data_stats` where FROM_UNIXTIME(updated_at,"%Y-%m-%d") = CURRENT_DATE
But I am getting empty results. So how can fetch records that are updated today?
Could be a mismatch in the types on the equals. To make sure, check to see if coercing both sides to DATE helps. Replace FROM_UNIXTIME(updated_at,"%Y-%m-%d") = CURRENT_DATE with:
DATE(FROM_UNIXTIME(updated_at)) = DATE(CURRENT_DATE)
table with login data of the website users with two datetime columns. One is logged in time and other one is logged out time.
table has following columns
user_id (user id of the
datetime (start login time DATETIME )
datetime_end (end logout time DATETIME)
From this data I need to generate a report or calculate the following
Daily AVG Login Duration for Users (i.e 01 March 2015 its 21 mins , 03 November 2016 its 25 mins ..etc )
second report is
Current Day (Last 24 hours) based on Hour AVG Login Duration (i.e same as above but only per hour AVG)
is there a way i can achieve this via MySQL query ? (or SQL query)
table is create is
CREATE TABLE IF NOT EXISTS `users_login` (
`login_id` int(11) unsigned NOT NULL,
`user_id` int(11) unsigned NOT NULL DEFAULT '0',
`last_ip` varchar(45) NOT NULL DEFAULT '0.0.0.0',
`server_ip` varchar(225) DEFAULT '0.0.0.0',
`country` varchar(2) NOT NULL DEFAULT '00',
`continent` tinytext,
`datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`datetime_end` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Something like this for the first one
SELECT DATE(datetime),AVG(TIMESTAMPDIFF(MINUTE,datetime,datetime_end)) as DailyAvg
FROM users_login
GROUP BY DATE(datetime)
And for hourly :
SELECT DATE(datetime),extract(hour from datetime) as HourCol,
AVG(TIMESTAMPDIFF(MINUTE,datetime,datetime_end)) as HourlyAvg
FROM users_login
WHERE DATE(datetime) = date(now())
GROUP BY DATE(datetime),extract(hour from datetime)
Of course this queries have a few exceptions.. You didn't explain you entire logic so I assumed you want to group by the hour of the login and the date of the login..
I Wanted to initialize default date field in my table with CURRENT_DATE(); well BENCH gives me errors that is not possible i heard about triggers but it seems to be a little over complicatet for that problem so is there any way to make this in such way
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data date default CURRENT_DATE(),
check (Data >= now()),
....
);
You can initialize a TIMESTAMP column with this:
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP
or a DATETIME column (MySQL 5.6+):
Data DATETIME DEFAULT CURRENT_TIMESTAMP
but if you want to initialize a DATE column using MySQL 5.5 you need an INSERT TRIGGER:
CREATE TRIGGER setdate_before_insert BEFORE INSERT ON test
FOR EACH ROW
SET NEW.Data = CURDATE();
and maybe you need also an UPDATE trigger? Please see fiddle here.
Another way to go about this, if you do not mind changing the date type, would be to use a TIMESTAMP and initialize it with TIMESTAMP DEFAULT CURRENT_TIMESTAMP. Your definition would then become:
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP, <<<==== change this
check (Data >= now()),
....
);
If you have MySQL version 5.6.5 and above, you can use CURRENT_TIMESTAMP instead of CURRENT_DATE
See http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
So you can't do it with dates but you may be able to change your field to be a TIMESTAMP or DATETIME and Bobs your mothers brother!
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
This table I created in a SQLite database:
CREATE TABLE [tickets] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[coupon_id] INTEGER NULL,
[size] FLOAT NULL,
[phone] VARCHAR(10) NULL,
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
[product] TEXT NULL
);
Now INSERT operation is:
INSERT INTO "tickets" VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO "tickets" VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO "tickets" VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
Now the same table I created in MySQL:
CREATE TABLE tickets (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
coupon_id INTEGER NULL,
size FLOAT NULL,
phone VARCHAR(10) NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
product TEXT NULL
);
INSERT operation for MySQL is:
INSERT INTO tickets VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO tickets VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO tickets VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
When i am inserting those values I got an error :-there can be only one TIMESTAMP column with current_timestamp in default of on update clause
…but I am not able to insert all those values into MySQL. Help me?
In SQLite you have two columns
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
while on MySQL you have only one
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
and you're trying to insert two values on it...
You should try
INSERT INTO tickets VALUES(..., '2010-11-30 17:46:39', ...)
At first glace, your varchar column is size 10, but you are inserting greater than length 10 data into it. Make sure your varchar column is wide enough for your data.
Your MySQL Schema appears to be incorrect for what you're trying to insert.
Excerpt from this post: Should I use field 'datetime' or 'timestamp'?
...Timestamps in MySQL generally used to track changes to records, and are updated every time the record is changed. If you want to store a specific value you should use a datetime field.
Change your MySQL schema to something closer to:
...
phone VARCHAR(12) NULL,
date DATE DEFAULT CURRENT_DATE NULL,
time TIME DEFAULT CURRENT_TIME NULL,
...