Find difference between 2 dates in MySQL - mysql

I have 2 different date columns: detail_returndate and detail_duedate. I am trying to find the difference between the two and return it as days_overdue.
Here is the basic SELECT. I have tried using diffday and subtracting the 2, but I am not having much luck.
select rent_num, vid_num, detail_returndate, detail_duedate
from detailrental
If you need the full sql code for the table let me know but i do not think that you will need it
To be clear I need the days overdue that is found out by taking the difference between detail_returndate and detail_duedate. This should yield the amount overdue, in days
/* Database Systems, 9th Ed., Coronel/MOrris/Rob */
/* Type of SQL : MySQL */
/*Create table price*/
CREATE TABLE price
(price_code INTEGER PRIMARY KEY AUTO_INCREMENT,
price_description VARCHAR(20) NOT NULL,
price_rentfee DECIMAL(5,2),
price_dailylatefee DECIMAL(5,2));
/*Insert data into price*/
INSERT INTO price VALUES(1,'Standard',2,1);
INSERT INTO price VALUES(2,'New Release',3.5,3);
INSERT INTO price VALUES(3,'Discount',1.5,1);
INSERT INTO price VALUES(4,'Weekly Special',1,.5);
/*Create table movie*/
CREATE TABLE movie
(movie_num INTEGER PRIMARY KEY AUTO_INCREMENT,
movie_title VARCHAR(75) NOT NULL,
movie_year INTEGER,
movie_cost DECIMAL(5,2),
movie_genre VARCHAR(50),
price_code INTEGER,
FOREIGN KEY(price_code) REFERENCES price(price_code));
/*Insert data into movie*/
INSERT INTO movie VALUES(1234,'The Cesar Family Christmas',2007,39.95,'FAMILY',2);
INSERT INTO movie VALUES(1235,'Smokey Mountain Wildlife',2004,59.95,'ACTION',1);
INSERT INTO movie VALUES(1236,'Richard Goodhope',2008,59.95,'DRAMA',2);
INSERT INTO movie VALUES(1237,'Beatnik Fever',2007,29.95,'COMEDY',2);
INSERT INTO movie VALUES(1238,'Constant Companion',2008,89.95,'DRAMA',NULL);
INSERT INTO movie VALUES(1239,'Where Hope Dies',1998,25.49,'DRAMA',3);
INSERT INTO movie VALUES(1245,'Time to Burn',2005,45.49,'ACTION',1);
INSERT INTO movie VALUES(1246,'What He Doesn''t Know',2006,58.29,'COMEDY',1);
/*Create table video*/
CREATE TABLE video
(vid_num INTEGER PRIMARY KEY AUTO_INCREMENT,
vid_indate DATE,
movie_num INTEGER,
FOREIGN KEY(movie_num) REFERENCES movie(movie_num));
/*Insert data into video*/
INSERT INTO video VALUES(54321,'2008-06-18',1234);
INSERT INTO video VALUES(54324,'2008-06-18',1234);
INSERT INTO video VALUES(54325,'2008-06-18',1234);
INSERT INTO video VALUES(34341,'2007-01-22',1235);
INSERT INTO video VALUES(34342,'2007-01-22',1235);
INSERT INTO video VALUES(34366,'2009-03-02',1236);
INSERT INTO video VALUES(34367,'2009-03-02',1236);
INSERT INTO video VALUES(34368,'2009-03-02',1236);
INSERT INTO video VALUES(34369,'2009-03-02',1236);
INSERT INTO video VALUES(44392,'2008-10-21',1237);
INSERT INTO video VALUES(44397,'2008-10-21',1237);
INSERT INTO video VALUES(59237,'2009-02-14',1237);
INSERT INTO video VALUES(61388,'2007-01-25',1239);
INSERT INTO video VALUES(61353,'2006-01-28',1245);
INSERT INTO video VALUES(61354,'2006-01-28',1245);
INSERT INTO video VALUES(61367,'2008-07-30',1246);
INSERT INTO video VALUES(61369,'2008-07-30',1246);
/*Create table membership*/
CREATE TABLE membership
(mem_num INTEGER PRIMARY KEY AUTO_INCREMENT,
mem_fname VARCHAR(30) NOT NULL,
mem_lname VARCHAR(30) NOT NULL,
mem_street VARCHAR(120),
mem_city VARCHAR(50),
mem_state VARCHAR(2),
mem_zip VARCHAR(5),
mem_balance DECIMAL(10,2));
/*Insert data into membership*/
INSERT INTO membership VALUES(102,'Tami','Dawson','2632 Takli Circle','Norene','TN','37136',11);
INSERT INTO membership VALUES(103,'Curt','Knight','4025 Cornell Court','Flatgap','KY','41219',6);
INSERT INTO membership VALUES(104,'Jamal','Melendez','788 East 145th Avenue','Quebeck','TN','38579',0);
INSERT INTO membership VALUES(105,'Iva','Mcclain','6045 Musket Ball Circle','Summit','KY','42783',15);
INSERT INTO membership VALUES(106,'Miranda','Parks','4469 Maxwell Place','Germantown','TN','38183',0);
INSERT INTO membership VALUES(107,'Rosario','Elliott','7578 Danner Avenue','Columbia','TN','38402',5);
INSERT INTO membership VALUES(108,'Mattie','Guy','4390 Evergreen Street','Lily','KY','40740',0);
INSERT INTO membership VALUES(109,'Clint','Ochoa','1711 Elm Street','Greenville','TN','37745',10);
INSERT INTO membership VALUES(110,'Lewis','Rosales','4524 Southwind Circle','Counce','TN','38326',0);
INSERT INTO membership VALUES(111,'Stacy','Mann','2789 East Cook Avenue','Murfreesboro','TN','37132',8);
INSERT INTO membership VALUES(112,'Luis','Trujillo','7267 Melvin Avenue','Heiskell','TN','37754',3);
INSERT INTO membership VALUES(113,'Minnie','Gonzales','6430 Vasili Drive','Williston','TN','38076',0);
/*Create table rental*/
CREATE TABLE rental
(rent_num INTEGER PRIMARY KEY AUTO_INCREMENT,
rent_date DATE,
mem_num INTEGER,
FOREIGN KEY(mem_num) REFERENCES membership(mem_num));
/*Insert data into rental*/
INSERT INTO rental VALUES(1001,'2009-03-01',103);
INSERT INTO rental VALUES(1002,'2009-03-01',105);
INSERT INTO rental VALUES(1003,'2009-03-02',102);
INSERT INTO rental VALUES(1004,'2009-03-02',110);
INSERT INTO rental VALUES(1005,'2009-03-02',111);
INSERT INTO rental VALUES(1006,'2009-03-02',107);
INSERT INTO rental VALUES(1007,'2009-03-02',104);
INSERT INTO rental VALUES(1008,'2009-03-03',105);
INSERT INTO rental VALUES(1009,'2009-03-03',111);
/*Create table detailrental*/
CREATE TABLE detailrental
(rent_num INTEGER,
vid_num INTEGER,
detail_fee DECIMAL(5,2),
detail_duedate DATE,
detail_returndate DATE,
detail_dailylatefee DECIMAL(5,2),
PRIMARY KEY(rent_num, vid_num),
FOREIGN KEY(rent_num) REFERENCES rental(rent_num),
FOREIGN KEY(vid_num) REFERENCES video(vid_num));
/*Insert data into dailyrental*/
INSERT INTO detailrental VALUES(1001,34342,2,'2009-03-04','2009-03-02',1);
INSERT INTO detailrental VALUES(1001,61353,2,'2009-03-04','2009-03-03',1);
INSERT INTO detailrental VALUES(1002,59237,3.5,'2009-03-04','2009-03-04',3);
INSERT INTO detailrental VALUES(1003,54325,3.5,'2009-03-04','2009-03-09',3);
INSERT INTO detailrental VALUES(1003,61369,2,'2009-03-06','2009-03-09',1);
INSERT INTO detailrental VALUES(1003,61388,0,'2009-03-06','2009-03-09',1);
INSERT INTO detailrental VALUES(1004,44392,3.5,'2009-03-05','2009-03-07',3);
INSERT INTO detailrental VALUES(1004,34367,3.5,'2009-03-05','2009-03-07',3);
INSERT INTO detailrental VALUES(1004,34341,2,'2009-03-07','2009-03-07',1);
INSERT INTO detailrental VALUES(1005,34342,2,'2009-03-07','2009-03-05',1);
INSERT INTO detailrental VALUES(1005,44397,3.5,'2009-03-05','2009-03-05',3);
INSERT INTO detailrental VALUES(1006,34366,3.5,'2009-03-05','2009-03-04',3);
INSERT INTO detailrental VALUES(1006,61367,2,'2009-03-07',NULL,1);
INSERT INTO detailrental VALUES(1007,34368,3.5,'2009-03-05',NULL,3);
INSERT INTO detailrental VALUES(1008,34369,3.5,'2009-03-05','2009-03-05',3);
INSERT INTO detailrental VALUES(1009,54324,3.5,'2009-03-05',NULL,3);
INSERT INTO detailrental VALUES(1001,34366,3.5,'2009-03-04','2009-03-02',3);

SELECT rent_num,
vid_num,
detail_returndate,
detail_duedate,
DATEDIFF( detail_duedate, detail_returndate) as days_overdue
FROM detailrental

It should work as shown above. Perhaps add the obligatory `.
SELECT `rent_num`,
`vid_num`,
`detail_returndate`,
`detail_duedate`,
DATEDIFF(`detail_returndate`,`detail_duedate`) as days_overdue
FROM detailrental
Also, DATEDIFF subtracts the second value from the first, so as shown it should give a positive number for days_overdue.

Related

Update Table from Trigger

I have the following tables in a database:
CREATE TABLE `CRUISE-RES` (
`cruiseid` INT,
`end-day` DATE,
`start-day` DATE
PRIMARY KEY (`cruiseid`));
CREATE TABLE `ROOM` (
`cruise-id` INT,
`price` FLOAT,
FOREIGN KEY (`cruise-id`));
CREATE TABLE `PROFIT` (
`cruiseid` INT,
`total` FLOAT);
With the following sample table inserts:
-- cruise table inserts
insert into `CRUISE-ID` (`cruiseid`,`start-day`,`end-day`)
values (1, '2022/01/01', '2022/01/05'), (1, '2022/01/05', '2022/01/10'), (2, '2022/01/05', '2022/01/10')
-- room table inserts
insert into ROOM (price,`cruise-id`)
values (5,1), (10,1), (25,2)
I also have the following function that shows the profit of each cruiseid based on the number of days in the CRUISE-RES * price per day.
SELECT c.`cruiseid`, sum(rm.`price`*(DATEDIFF(c.`end-date`, c.`start-date`))) AS 'total_profit'
FROM ROOM rm
JOIN `CRUISE-RES` c
ON rm.`cruise-id` = c.cruiseid
GROUP BY rm.`cruise-id`,'cruiseid'
How can I use this information on a trigger that updates the PROFIT table after each insert into CRUISE-RES table?

MySQL Foreign Key value is NULL

I am trying to create a table called "Borrower" or "Lånetagare" and want to insert the value from table Lån with the ID from there. The result I get is NULL. How come?
create table Lån
(
Lån_ID INT auto_increment primary key,
Lånedatum DATETIME,
Inlämningsdatum DATETIME,
Omlån DATETIME
);
INSERT INTO Lån (Lånedatum, Inlämningsdatum, Omlån)
VALUES ('2017-09-12', '2017-09-15', '2017-09-15');
CREATE TABLE Lånetagare
(
Lånetagare_ID INT(10) auto_increment primary key,
Lösenord varchar(50),
Förnamn varchar(50),
Efternamn varchar(50),
Adress varchar (50),
Ort varchar(50),
Postnummer int(5),
Email varchar(50),
Telefonnummer int(20),
Lånekort int(50),
Lån_ID int(50),
FOREIGN KEY (Lån_ID) REFERENCES Lån(Lån_ID)
);
INSERT INTO Lånetagare (Lösenord, Förnamn, Efternamn, Adress, Ort, Postnummer, Email, Telefonnummer, Lånekort)
VALUES ('hej135', 'Victor', 'Chi', 'Blekingegatan 28', 'Stockholm', 11856, 'Tim#hotmail.com', 0704582235, 56);
SELECT * FROM Lånetagare;
If you want to associate a row in Lånetagare with the previously-inserted row in Lån, you must set a value for the foreign key.
Here's a common way to do it:
INSERT INTO Lån (Lånedatum, Inlämningsdatum, Omlån)
VALUES ('2017-09-12', '2017-09-15', '2017-09-15');
INSERT INTO Lånetagare (...other columns..., Lån_ID)
VALUES (...other values..., LAST_INSERT_ID());
The LAST_INSERT_ID() function returns the most recent auto-increment id created by an INSERT statement during your session. Read more about this function here: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id
Try
INSERT INTO Lånetagare (Lösenord, Förnamn, Efternamn, Adress, Ort, Postnummer, Email, Telefonnummer, Lånekort, Lån_ID)
VALUES ('hej135', 'Victor', 'Chi', 'Blekingegatan 28', 'Stockholm', 11856, 'Tim#hotmail.com', 0704582235, 56, (SELECT Lån_ID FROM Lån WHERE <put your select criteria here>));

updating date in mysql (phpMyAdmin)

i have created a table called vendor and imported the document with these commands:
DROP TABLE IF EXISTS vendor;
CREATE TABLE VENDOR (
V_CODE int,
V_NAME varchar(15),
V_CONTACT varchar(50),
V_AREACODE varchar(3),
V_PHONE varchar(8),
V_STATE varchar(2),
V_ORDER varchar(1)
);
INSERT INTO VENDOR VALUES('21225','Bryson, Inc.','Smithson','615','223-3234','TN','Y');
INSERT INTO VENDOR VALUES('21226','SuperLoo, Inc.','Flushing','904','215-8995','FL','N');
INSERT INTO VENDOR VALUES('21231','D&E Supply','Singh','615','228-3245','TN','Y');
INSERT INTO VENDOR VALUES('21344','Gomez Bros.','Ortega','615','889-2546','KY','N');
INSERT INTO VENDOR VALUES('22567','Dome Supply','Smith','901','678-1419','GA','N');
INSERT INTO VENDOR VALUES('23119','Randsets Ltd.','Anderson','901','678-3998','GA','Y');
INSERT INTO VENDOR VALUES('24004','Brackman Bros.','Browning','615','228-1410','TN','N');
INSERT INTO VENDOR VALUES('24288','ORDVA, Inc.','Hakford','615','898-1234','TN','Y');
INSERT INTO VENDOR VALUES('25443','B&K, Inc.','Smith','904','227-0093',null ,'N');
INSERT INTO VENDOR VALUES('25501','Damal Supplies','Smythe','615','890-3529','TN','N');
INSERT INTO VENDOR VALUES('25595','Rubicon Systems','Orton','904','456-0092','FL','Y');
DROP TABLE IF EXISTS product;
CREATE TABLE PRODUCT (
P_CODE varchar(10),
P_DESCRIPT varchar(35),
P_INDATE datetime,
P_QOH int,
P_MIN int,
P_PRICE float(8),
P_DISCOUNT float(8),
V_CODE int
);
INSERT INTO PRODUCT VALUES('11QER/31','Power painter, 15 psi., 3-nozzle','11/3/2007','8','5','109.98999786377','0','25595');
INSERT INTO PRODUCT VALUES('13-Q2/P2','7.25-in. pwr. saw blade','12/13/2007','32','15','14.9899997711182','0.05','21344');
INSERT INTO PRODUCT VALUES('14-Q1/L3','9.00-in. pwr. saw blade','11/13/2007','18','12','17.4899997711182','0','21344');
INSERT INTO PRODUCT VALUES('1546-QQ2','Hrd. cloth, 1/4-in., 2x50','1/15/2008','15','8','39.9500007629395','0','23119');
INSERT INTO PRODUCT VALUES('1558-QW1','Hrd. cloth, 1/2-in., 3x50','1/15/2008','23','5','43.9900016784668','0','23119');
INSERT INTO PRODUCT VALUES('2232/QTY','B&D jigsaw, 12-in. blade','12/30/2007','8','5','109.919998168945','0.05','24288');
INSERT INTO PRODUCT VALUES('2232/QWE','B&D jigsaw, 8-in. blade','12/24/2007','6','5','99.870002746582','0.05','24288');
INSERT INTO PRODUCT VALUES('2238/QPD','B&D cordless drill, 1/2-in.','1/20/2008','12','5','38.9500007629395','0.05','25595');
INSERT INTO PRODUCT VALUES('23109-HB','Claw hammer','1/20/2008','23','10','9.94999980926514','0.1','21225');
INSERT INTO PRODUCT VALUES('23114-AA','Sledge hammer, 12 lb.','1/2/2008','8','5','14.3999996185303','0.05','');
INSERT INTO PRODUCT VALUES('54778-2T','Rat-tail file, 1/8-in. fine','12/15/2007','43','20','4.98999977111816','0','21344');
INSERT INTO PRODUCT VALUES('89-WRE-Q','Hicut chain saw, 16 in.','2/7/2008','11','5','256.989990234375','0.05','24288');
INSERT INTO PRODUCT VALUES('PVC23DRT','PVC pipe, 3.5-in., 8-ft','2/20/2008','188','75','5.86999988555908','0','');
INSERT INTO PRODUCT VALUES('SM-18277','1.25-in. metal screw, 25','3/1/2008','172','75','6.98999977111816','0','21225');
INSERT INTO PRODUCT VALUES('SW-23116','2.5-in. wd. screw, 50','2/24/2008','237','100','8.44999980926514','0','21231');
INSERT INTO PRODUCT VALUES('WR3/TT3','Steel matting, 4''x8''x1/6", .5" mesh','1/17/2008','18','5','119.949996948242','0.1','25595');
DROP TABLE IF EXISTS CUSTOMER;
CREATE TABLE CUSTOMER (
CUS_CODE int,
CUS_LNAME varchar(15),
CUS_FNAME varchar(15),
CUS_INITIAL varchar(1),
CUS_AREACODE varchar(3),
CUS_PHONE varchar(8),
CUS_BALANCE float(8)
);
INSERT INTO CUSTOMER VALUES('10010','Ramas','Alfred','A','615','844-2573','0');
INSERT INTO CUSTOMER VALUES('10011','Dunne','Leona','K','713','894-1238','0');
INSERT INTO CUSTOMER VALUES('10012','Smith','Kathy','W','615','894-2285','345.859985351562');
INSERT INTO CUSTOMER VALUES('10013','Olowski','Paul','F','615','894-2180','536.75');
INSERT INTO CUSTOMER VALUES('10014','Orlando','Myron','','615','222-1672','0');
INSERT INTO CUSTOMER VALUES('10015','O''Brian','Amy','B','713','442-3381','0');
INSERT INTO CUSTOMER VALUES('10016','Brown','James','G','615','297-1228','221.190002441406');
INSERT INTO CUSTOMER VALUES('10017','Williams','George','','615','290-2556','768.929992675781');
INSERT INTO CUSTOMER VALUES('10018','Farriss','Anne','G','713','382-7185','216.550003051758');
INSERT INTO CUSTOMER VALUES('10019','Smith','Olette','K','615','297-3809','0');
DROP TABLE IF EXISTS invoice;
CREATE TABLE INVOICE (
INV_NUMBER int,
CUS_CODE int,
INV_DATE datetime,
INV_SUBTOTAL float(8),
INV_TAX float(8),
INV_TOTAL float(8)
);
INSERT INTO INVOICE VALUES('1001','10014','1/16/2008','24.8999996185303','1.99000000953674','26.8899993896484');
INSERT INTO INVOICE VALUES('1002','10011','1/16/2008','9.97999954223633','0.800000011920929','10.7799997329712');
INSERT INTO INVOICE VALUES('1003','10012','1/16/2008','153.850006103516','12.3100004196167','166.160003662109');
INSERT INTO INVOICE VALUES('1004','10011','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1005','10018','1/17/2008','70.4400024414062','5.6399998664856','76.0800018310547');
INSERT INTO INVOICE VALUES('1006','10014','1/17/2008','397.829986572266','31.8299999237061','429.660003662109');
INSERT INTO INVOICE VALUES('1007','10015','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1008','10011','1/17/2008','399.149993896484','31.9300003051758','431.079986572266');
DROP TABLE IF EXISTS line;
CREATE TABLE LINE (
INV_NUMBER int,
LINE_NUMBER int,
P_CODE varchar(10),
LINE_UNITS float(8),
LINE_PRICE float(8),
LINE_TOTAL float(8)
);
INSERT INTO LINE VALUES('1001','1','13-Q2/P2','1','14.9899997711182','14.9899997711182');
INSERT INTO LINE VALUES('1001','2','23109-HB','1','9.94999980926514','9.94999980926514');
INSERT INTO LINE VALUES('1002','1','54778-2T','2','4.98999977111816','9.97999954223633');
INSERT INTO LINE VALUES('1003','1','2238/QPD','1','38.9500007629395','38.9500007629395');
INSERT INTO LINE VALUES('1003','2','1546-QQ2','1','39.9500007629395','39.9500007629395');
INSERT INTO LINE VALUES('1003','3','13-Q2/P2','5','14.9899997711182','74.9499969482422');
INSERT INTO LINE VALUES('1004','1','54778-2T','3','4.98999977111816','14.9700002670288');
INSERT INTO LINE VALUES('1004','2','23109-HB','2','9.94999980926514','19.8999996185303');
INSERT INTO LINE VALUES('1005','1','PVC23DRT','12','5.86999988555908','70.4400024414062');
INSERT INTO LINE VALUES('1006','1','SM-18277','3','6.98999977111816','20.9699993133545');
INSERT INTO LINE VALUES('1006','2','2232/QTY','1','109.919998168945','109.919998168945');
INSERT INTO LINE VALUES('1006','3','23109-HB','1','9.94999980926514','9.94999980926514');
INSERT INTO LINE VALUES('1006','4','89-WRE-Q','1','256.989990234375','256.989990234375');
INSERT INTO LINE VALUES('1007','1','13-Q2/P2','2','14.9899997711182','29.9799995422363');
INSERT INTO LINE VALUES('1007','2','54778-2T','1','4.98999977111816','4.98999977111816');
INSERT INTO LINE VALUES('1008','1','PVC23DRT','5','5.86999988555908','29.3500003814697');
INSERT INTO LINE VALUES('1008','2','WR3/TT3','3','119.949996948242','359.850006103516');
INSERT INTO LINE VALUES('1008','3','23109-HB','1','9.94999980926514','9.94999980926514');
DROP TABLE IF EXISTS EMP;
CREATE TABLE EMP (
EMP_NUM int,
EMP_TITLE varchar(4),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_DOB datetime,
EMP_HIRE_DATE datetime,
EMP_AREACODE varchar(3),
EMP_PHONE varchar(8),
EMP_MGR int
);
INSERT INTO EMP VALUES('100','Mr.','Kolmycz','George','D','6/15/1942','3/15/1985','615','324-5456','');
INSERT INTO EMP VALUES('101','Ms.','Lewis','Rhonda','G','3/19/1965','4/25/1986','615','324-4472','100');
INSERT INTO EMP VALUES('102','Mr.','VanDam','Rhett','','11/14/1958','12/20/1990','901','675-8993','100');
INSERT INTO EMP VALUES('103','Ms.','Jones','Anne','M','10/16/1974','8/28/1994','615','898-3456','100');
INSERT INTO EMP VALUES('104','Mr.','Lange','John','P','11/8/1971','10/20/1994','901','504-4430','105');
INSERT INTO EMP VALUES('105','Mr.','Williams','Robert','D','3/14/1975','11/8/1998','615','890-3220','');
INSERT INTO EMP VALUES('106','Mrs.','Smith','Jeanine','K','2/12/1968','1/5/1989','615','324-7883','105');
INSERT INTO EMP VALUES('107','Mr.','Diante','Jorge','D','8/21/1974','7/2/1994','615','890-4567','105');
INSERT INTO EMP VALUES('108','Mr.','Wiesenbach','Paul','R','2/14/1966','11/18/1992','615','897-4358','');
INSERT INTO EMP VALUES('109','Mr.','Smith','George','K','6/18/1961','4/14/1989','901','504-3339','108');
INSERT INTO EMP VALUES('110','Mrs.','Genkazi','Leighla','W','5/19/1970','12/1/1990','901','569-0093','108');
INSERT INTO EMP VALUES('111','Mr.','Washington','Rupert','E','1/3/1966','6/21/1993','615','890-4925','105');
INSERT INTO EMP VALUES('112','Mr.','Johnson','Edward','E','5/14/1961','12/1/1983','615','898-4387','100');
INSERT INTO EMP VALUES('113','Ms.','Smythe','Melanie','P','9/15/1970','5/11/1999','615','324-9006','105');
INSERT INTO EMP VALUES('114','Ms.','Brandon','Marie','G','11/2/1956','11/15/1979','901','882-0845','108');
INSERT INTO EMP VALUES('115','Mrs.','Saranda','Hermine','R','7/25/1972','4/23/1993','615','324-5505','105');
INSERT INTO EMP VALUES('116','Mr.','Smith','George','A','11/8/1965','12/10/1988','615','890-2984','108');
but when I entered commands:
SELECT INV_DATE FROM invoice;
the data only shows 0000-00-00 00:00:00
how do i update the whole column in INV_DATE to what I wish (example 1/16/2008)?
INSERT INTO INVOICE VALUES('1001','10014','1/16/2008','24.8999996185303','1.99000000953674','26.8899993896484');
INSERT INTO INVOICE VALUES('1002','10011','1/16/2008','9.97999954223633','0.800000011920929','10.7799997329712');
INSERT INTO INVOICE VALUES('1003','10012','1/16/2008','153.850006103516','12.3100004196167','166.160003662109');
INSERT INTO INVOICE VALUES('1004','10011','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1005','10018','1/17/2008','70.4400024414062','5.6399998664856','76.0800018310547');
INSERT INTO INVOICE VALUES('1006','10014','1/17/2008','397.829986572266','31.8299999237061','429.660003662109');
INSERT INTO INVOICE VALUES('1007','10015','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1008','10011','1/17/2008','399.149993896484','31.9300003051758','431.079986572266');
Type of INV_DATE in invoice table is 'datetime'. The default format of datetime is 'YYYY-MM-DD HH:MM:SS'.
So, if you insert values in different format, it will misinterpret it and show unexpected output.
Update the type of the INV_DATE column to any other favorable one or insert date and time formatted in this format : YYYY-MM-DD HH:MM:SS[.fraction]
Hope this helps.

MySQL update field based on conditional sum from other table

I am trying to populate an attendance system with values from a legacy system.
I am currently tracking all_time attendance (which is simple), attendance in this year, and attendance since last grading (examination). To avoid potential contamination, I am storing legacy data, and want to import it into my totals a bit at a time.
This is my basic data model:
-- information about the attendee
CREATE TABLE core_member (
id INT UNSIGNED AUTO_INCREMENT,
name VARCHAR (250),
dob DATE,
last_grade_date date default '2014-12-14',
grade INT UNSIGNED NOT NULL DEFAULT 50,
PRIMARY KEY (id)
);
-- attendee data
INSERT INTO core_member values(1, 'Donald Duck', '1950-03-01', '2015-06-15', 10);
INSERT INTO core_member values(2, 'Goofy', '1950-04-17', '2014-06-15', 42);
-- attendance sums (1:1 with antendee)
CREATE TABLE core_attendance(
medlemsid INT UNSIGNED,
imported INT DEFAULT 0,
all_time INT DEFAULT 0,
since_last_grad INT DEFAULT 0,
UNIQUE KEY medlemsid (medlemsid),
FOREIGN KEY (medlemsid) REFERENCES core_member (id)
);
-- attendance sum data
INSERT INTO core_attendance values(1,100,150,0);
INSERT INTO core_attendance values(2,80,103,3);
-- historic attendance
CREATE TABLE core_historic_attendance (
medlemsid int(10) unsigned NOT NULL,
year int(11) NOT NULL,
month enum('none','januar','februar','marts','april','maj','juni','juli','august','september','oktober','november','december') NOT NULL DEFAULT 'none',
attendance int(11) DEFAULT '0',
UNIQUE KEY unq (medlemsid,year,month),
KEY medlemsid_idx (medlemsid),
CONSTRAINT medlemsid FOREIGN KEY (medlemsid) REFERENCES core_member (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
-- historic attendance data
INSERT INTO core_historic_attendance values(1,2011,'none',54);
INSERT INTO core_historic_attendance values(1,2012,'none',43);
INSERT INTO core_historic_attendance values(1,2013,'none',61);
INSERT INTO core_historic_attendance values(1,2014,'none',49);
INSERT INTO core_historic_attendance values(1,2015,'januar',19);
INSERT INTO core_historic_attendance values(1,2015,'februar',14);
INSERT INTO core_historic_attendance values(1,2015,'marts',17);
INSERT INTO core_historic_attendance values(2,2011,'none',54);
INSERT INTO core_historic_attendance values(2,2012,'none',43);
INSERT INTO core_historic_attendance values(2,2013,'none',61);
INSERT INTO core_historic_attendance values(2,2014,'none',49);
INSERT INTO core_historic_attendance values(2,2015,'januar',19);
INSERT INTO core_historic_attendance values(2,2015,'februar',4);
INSERT INTO core_historic_attendance values(2,2015,'marts',7);
I need to add the sum of all the values from core_historic attendance where the year is greater than the year of the last grading to the since_last_grad column in the core_member table, for the member in question (initial load).
I gather from this post ( mysql update column with value from another table ), that I have to do something like
UPDATE core_members INNER JOIN core_historic_attendance ON id = core_historic_attendance.medlemsid having`year` > year(last_grad_date)
SET since_last_grad = since_last_grad + SUM(attendance);
But I'm a bit stuck, since i want only the sum of the core_historic_attendance records where the year is greater than year(last_grad_date)
EDIT:
I have corrected the SQL as suggested by Sasha, nut I get an error code 1111 "Invalid use of group function"
I have created an SQL Fiddle as well
Does this work for you:
CREATE TEMPORARY TABLE t1 (key(medlemsid)) SELECT core_historic_attendance.medlemsid,SUM(attendance) as total_attendance
FROM core_member INNER JOIN core_historic_attendance ON
id = core_historic_attendance.medlemsid AND `year` > year(last_grade_date)
GROUP BY medlemsid;
UPDATE core_attendance INNER JOIN t1 USING(medlemsid)
SET since_last_grad = since_last_grad + t1.total_attendance;
?

MySQL doesn't seem to work correct

I have this college database where i have to Retrieve names of instructors teaching Computer Science courses, the sections (course number, section number, year, semester) they are teaching, and the total number of students in the sections
So far this is what have managed but the output is not correct.
SELECT i.instructor_id,o.year,o.semester,o.section_number, o.number as CourseNumber,o.Total_num_student
FROM Instructor i JOIN
(SELECT t.instructor_id,t.year,t.semester,t.section_number,o1.number,o1.Total_num_student
FROM teaches t JOIN
(SELECT COUNT(s.student_id) as Total_num_student,e.section_number,e.number
FROM Student s JOIN
enrolls e
ON s.student_id=e.student_id
GROUP BY section_number,e.number) as o1
ON o1.section_number=t.section_number) as o
on i.instructor_id=o.instructor_id
WHERE department='Computer Science';
Can you see what is wrong with the output.
create table Course (
number int,
title varchar(255),
credits int,
syllabus varchar(255),
PRIMARY KEY (number)
);
INSERT INTO Course VALUES (620,'Algorithm',3,'XYZ');
INSERT INTO Course VALUES (621,'Algorithm',3,'XYZ');
INSERT INTO Course VALUES (232,'Java',2,'ABC');
INSERT INTO Course VALUES (420,'Cpp',2,'PQRS');
INSERT INTO Course VALUES (720,'Big Data',3,'NVGY');
INSERT INTO Course VALUES (120,'Intelligent System',4,'KJHU');
INSERT INTO Course VALUES (220,'Operating System',3,'GED');
INSERT INTO Course VALUES (480,'Graphics',4,'RSFN');
INSERT INTO Course VALUES (520,'Distributed Networks',3,'NHU');
INSERT INTO Course VALUES (820,'Data Mining',3,'TYU');
INSERT INTO Course VALUES (700,'Cryptography',1,'MNO');
create table Student (
student_id int,
name varchar(255),
department varchar(255),
PRIMARY KEY (student_id)
);
INSERT INTO Student VALUES (2345,'Mike','Computer Science');
INSERT INTO Student VALUES (346,'Rob','Computer Science');
INSERT INTO Student VALUES (789,'Nick','Game Design');
INSERT INTO Student VALUES (675,'Sara','Computer Science');
INSERT INTO Student VALUES (123,'Raj','Chemical');
INSERT INTO Student VALUES (5331,'Jack','Biotech');
INSERT INTO Student VALUES (1023,'Michelle','Chemical');
INSERT INTO Student VALUES (9800,'Jie','Game Design');
INSERT INTO Student VALUES (7834,'Dan','Petroleum');
INSERT INTO Student VALUES (4567,'Patrick','Computer Science');
create table Instructor (
instructor_id int,
name varchar(255),
department varchar(255),
title varchar(255),
PRIMARY KEY (instructor_id)
);
INSERT INTO Instructor VALUES (12,'John','Computer Science','Java');
INSERT INTO Instructor VALUES (23,'Bischof','Mechanical','Drawing');
INSERT INTO Instructor VALUES (56,'Kwon','Biotech','Biology');
INSERT INTO Instructor VALUES (78,'Deever','Security','Cryptography');
INSERT INTO Instructor VALUES (45,'Jin','Computer Science','Cpp');
INSERT INTO Instructor VALUES (67,'Bailey','Petroleum','Metalurgy');
INSERT INTO Instructor VALUES (90,'Richard','Industrial','Manufacture');
INSERT INTO Instructor VALUES (11,'Joe','Chemical','Chemistry');
INSERT INTO Instructor VALUES (24,'Roger','Game Design','Cpp');
INSERT INTO Instructor VALUES (55,'Zack','Computer Science','Design');
create table CourseOffering (
number int,
year int,
semester int,
section_number int,
classroom int,
PRIMARY KEY (number,year,semester,section_number),
FOREIGN KEY (number) REFERENCES Course (number)
);
INSERT INTO CourseOffering VALUES (620,2012,2,102,3540);
INSERT INTO CourseOffering VALUES (621,2013,3,103,3545);
INSERT INTO CourseOffering VALUES (232,2011,1,103,3455);
INSERT INTO CourseOffering VALUES (420,2013,3,101,2650);
INSERT INTO CourseOffering VALUES (720,2013,3,100,3455);
INSERT INTO CourseOffering VALUES (120,2014,1,102,3000);
INSERT INTO CourseOffering VALUES (220,2008,2,104,1450);
INSERT INTO CourseOffering VALUES (480,2009,4,106,1400);
INSERT INTO CourseOffering VALUES (520,2010,3,102,3500);
INSERT INTO CourseOffering VALUES (820,2008,3,100,2700);
INSERT INTO CourseOffering VALUES (700,2012,1,103,2540);
create table CourseOffering_Timing (
number int,
year int,
semester int,
section_number int,
timing int,
PRIMARY KEY (number,year,semester,section_number,timing),
FOREIGN KEY (number) REFERENCES Course (number)
);
INSERT INTO CourseOffering_Timing VALUES (620,2012,2,102,9);
INSERT INTO CourseOffering_Timing VALUES (621,2013,3,103,9);
INSERT INTO CourseOffering_Timing VALUES (232,2011,1,103,3);
INSERT INTO CourseOffering_Timing VALUES (420,2013,3,101,2);
INSERT INTO CourseOffering_Timing VALUES (720,2013,3,100,5);
INSERT INTO CourseOffering_Timing VALUES (120,2014,1,102,4);
INSERT INTO CourseOffering_Timing VALUES (220,2008,2,104,10);
INSERT INTO CourseOffering_Timing VALUES (480,2009,4,106,12);
INSERT INTO CourseOffering_Timing VALUES (520,2010,3,102,4);
INSERT INTO CourseOffering_Timing VALUES (820,2008,3,100,2);
INSERT INTO CourseOffering_Timing VALUES (700,2012,1,103,3);
create table PreRequisite (
number int,
prerequisite_number int,
PRIMARY KEY (number,prerequisite_number),
FOREIGN KEY (number) REFERENCES Course(number)
);
INSERT INTO PreRequisite VALUES (620,480);
INSERT INTO PreRequisite VALUES (621,481);
INSERT INTO PreRequisite VALUES (232,120);
INSERT INTO PreRequisite VALUES (420,320);
INSERT INTO PreRequisite VALUES (720,580);
INSERT INTO PreRequisite VALUES (120,110);
INSERT INTO PreRequisite VALUES (220,130);
INSERT INTO PreRequisite VALUES (480,380);
INSERT INTO PreRequisite VALUES (520,360);
INSERT INTO PreRequisite VALUES (820,700);
INSERT INTO PreRequisite VALUES (700,610);
create table enrolls (
student_id int,
number int,
year int,
semester int,
section_number int,
grade varchar(10),
PRIMARY KEY (student_id,number,year,semester,section_number),
FOREIGN KEY (number,year,semester,section_number) REFERENCES CourseOffering(number,year,semester,section_number),
FOREIGN KEY (student_id) REFERENCES Student (student_id)
);
INSERT INTO enrolls VALUES (2345,620,2012,2,102,'A');
INSERT INTO enrolls VALUES (346,232,2011,1,103,'B');
INSERT INTO enrolls VALUES (789,420,2013,3,101,'A');
INSERT INTO enrolls VALUES (675,621,2013,3,103,'C');
INSERT INTO enrolls VALUES (123,120,2014,1,102,'B');
INSERT INTO enrolls VALUES (5331,220,2008,2,104,'A');
INSERT INTO enrolls VALUES (1023,480,2009,4,106,'B');
INSERT INTO enrolls VALUES (9800,520,2010,3,102,'A');
INSERT INTO enrolls VALUES (7834,820,2008,3,100,'C');
INSERT INTO enrolls VALUES (4567,700,2012,1,103,'F');
create table teaches (
instructor_id int,
number int,
year int,
semester int,
section_number int,
PRIMARY KEY (instructor_id, number, year, semester, section_number),
FOREIGN KEY (instructor_id) REFERENCES Instructor(instructor_id),
FOREIGN KEY ( number, year, semester, section_number) REFERENCES CourseOffering( number, year, semester, section_number)
);
INSERT INTO teaches VALUES (12,620,2012,2,102);
INSERT INTO teaches VALUES (12,621,2013,3,103);
INSERT INTO teaches VALUES (23,232,2011,1,103);
INSERT INTO teaches VALUES (56,420,2013,3,101);
INSERT INTO teaches VALUES (78,720,2013,3,100);
INSERT INTO teaches VALUES (45,120,2014,1,102);
INSERT INTO teaches VALUES (67,220,2008,2,104);
INSERT INTO teaches VALUES (90,480,2009,4,106);
INSERT INTO teaches VALUES (11,520,2010,3,102);
INSERT INTO teaches VALUES (24,820,2008,3,100);
INSERT INTO teaches VALUES (55,700,2012,1,103);
I'm not completely sure what you are trying to achieve but this is if you like to retain the section info:
SELECT a.instructor_id, group_concat(DISTINCT a.year SEPARATOR ', ') YEAR, group_concat(DISTINCT a.semester SEPARATOR ', ') SEMESTER, group_concat(DISTINCT a.section_number SEPARATOR ', ') SECTION_NUMBER, group_concat(DISTINCT a.coursenumber SEPARATOR ', ') COURSENUMBER, count(a.student_id) as Total_num_student FROM (
SELECT e.student_id, i.instructor_id, t.year,t.semester,t.section_number, t.number as CourseNumber
FROM instructor i
JOIN teaches t
ON i.instructor_id = t.instructor_id
JOIN enrolls e
ON e.section_number=t.section_number
WHERE department='Computer Science') a
GROUP BY a.instructor_id
;
SELECT a.instructor_id, a.year, a.semester, a.section_number, a.coursenumber, count(a.student_id) as Total_num_student FROM (
SELECT e.student_id, i.instructor_id, t.year,t.semester,t.section_number, t.number as CourseNumber
FROM instructor i
JOIN teaches t
ON i.instructor_id = t.instructor_id
JOIN enrolls e
ON e.section_number=t.section_number
WHERE department='Computer Science') a
GROUP BY a.instructor_id, a.section_number
;
SQLFiddle demo
See if this will work for you
SELECT Instructor.name AS instructor_name, teaches.section_number AS section_number,
teaches.year AS year, teaches.semester AS semester, COUNT(enrolls.student_id) FROM Instructor
INNER JOIN teaches ON teaches.instructor_id = Instructor.instructor_id
INNER JOIN enrolls ON teaches.section_number = enrolls.section_number AND teaches.semester = enrolls.semester AND teaches.year = enrolls.year
WHERE department='Computer Science'
GROUP BY Instructor.name, teaches.section_number, teaches.year, teaches.semester;