updating date in mysql (phpMyAdmin) - mysql
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.
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?
How to count repeated project_no in mysql
imagine i have 3 table employee detail, project details and Works on(where it's set that which employee will work on which project). The question is To show the average salary of all the employees work on those project which are having more than three employees. create table Employee ( empid varchar(5) primary key, ename varchar(20), address varchar(50), phone bigint(10), job varchar(10), salary varchar(10) ); insert into Employee values ('e101','Ankita','Delhi',9987654321,'Manager','12000'); insert into Employee values ('e102','Rahul','Banglore',9219849816,'Developer','80000'); insert into Employee values ('e103','Aditya','Noida',9517536842,'Chef','16000'); insert into Employee values ('e104','Rachna','Mumbai',7539561682,'Teacher','45000'); insert into Employee values ('e105','Karan','Delhi',8529517463,'Driver','18000'); create table Project ( project_no varchar(5) primary key, pname varchar(20), no_of_hours int(3) ); Insert into Project values('p101', 'Data Handling', 5); Insert into Project values('p102', 'Game Develop', 3); Insert into Project values('p103', 'New Recipe', 6); Insert into Project values('p104', 'Question Paper', 9); Insert into Project values('p105', 'Ride to Shimla', 12); create table Works_on ( empid varchar(5), project_no varchar(5), start_date date, end_date date, manager_name varchar(20), primary key(empid, project_no), foreign key(empid) references Employee(empid), foreign key(project_no) references Project(project_no) ); Insert into Works_on value('e101', 'p101', '2021-02-24', '2021-03-25', 'Anshika'); Insert into Works_on value('e102', 'p101', '2021-03-4', '2021-04-2', 'Anshika'); Insert into Works_on value('e103', 'p102', '2021-02-4', '2021-03-5', 'Roshan'); Insert into Works_on value('e104', 'p101', '2021-05-20', '2021-05-29', 'Anshika'); Insert into Works_on value('e105', 'p103', '2021-02-4', '2021-02-5', 'Dharmesh'); Insert into Works_on value('e103', 'p101', '2021-02-4', '2021-02-5', 'Anshika'); Insert into Works_on value('e105', 'p104', '2021-02-4', '2021-02-5', 'Ritika'); Insert into Works_on value('e105', 'p105', '2021-02-4', '2021-02-5', 'Sid');
select w.project_no project_number, count(w.project_no) number_of_employees, avg(e.salary) average_salary from Employee e, Project p, Works_on w where w.project_no= p.project_no and w.empid=e.empid group by w.project_no having(count(w.project_no))>3; DBFiddle
My SQL Auto_increment not working for a fixed column
I am trying to create three tables using MySQL Workbench, where two columns need to be auto incremented with a fixed starting value. I have checked some online resources and figured it out what statements to use. create database test; use test; /*table Project */ create table Project( Pnumber INT NOT NULL AUTO_INCREMENT, Pname varchar(30) NOT NULL, Plocation enum ('QLD', 'VIC', 'NSW', 'SA') NOT NULL, primary key(Pnumber) ); ALTER TABLE Project AUTO_INCREMENT = 7777770; /*table Department*/ Create table Department( Dcode varchar(5) NOT NULL, Dname varchar(30), Dmg_ssn varchar(30), primary key(Dcode) ); /* Table Employee*/ create table Employee( Ssn INT NOT NULL AUTO_INCREMENT, Ename varchar(30) NOT NULL, Bdate DATE, Address varchar(30), Dcode varchar(5) NOT NULL, Driver_License varchar(30), primary key(Ssn), foreign key(Dcode) references Department(Dcode) ); ALTER TABLE Employee AUTO_INCREMENT = 1000000; /* Insert into Project*/ Insert into Project values ('7777770','Star', 'QLD'); Insert into Project values ('7777771','Innova', 'NSW'); Insert into Project values ('7777772','Andra', "QLD"); /* Insert into Department */ insert into Department values ('ABC12', 'Finances', 'RA12'); insert into Department values ('WXY10', 'Human Resources', 'RA12'); insert into Department values ('PBC32', 'S2', 'RB13'); /*Insert into Employee */ insert into Employee values ('0000001','Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456'); insert into Employee values ('0000002','Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947'); insert into Employee values ('0000003','Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655'); However, after I created the tables, only the column Pnumber in table Project follows the required format, while the column Ssn in Employee does not. This is a SELECT over the Employee table: Ssn,Ename,Bdate,Address,Dcode,Driver_License 1,"Vladimir Rostov",2008-07-04,"19 Wilson St",ABC12,1023456 2,"Rory Reid",2002-02-10,"10 Mary St",WXY10,2365947 3,"Andy Murray",2001-05-11,"1280 Albert St",WXY10,5891655 Any idea what I am doing wrong??
Your sql script has an employee table, not a student, I assume these two names refer to the same table. I will use the employee name. So, you set the start value auto increment of the employee table to 1000000: ALTER TABLE Employee AUTO_INCREMENT = 1000000; But then you explicitly insert 1, 2, and 3 into this column with your insert statements because '0000001' translates into 1. If you explicitly insert a value into auto increment and it is higher than the maximum value in the given field, then mysql will insert that value as is into the auto increment field. If you are using auto increment, then you should let it work and do not specify an explicit value: /*Insert into Employee */ insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456'); insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947'); insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
MySQL stored procedure to insert or update table
I have two tables: CREATE TABLE EMPLOYEE ( EMPLOYEE_ID bigint NOT NULL AUTO_INCREMENT, EMPLOYEE_NO VARCHAR(30), EMPLOYEE_TYPE VARCHAR(20), PRIMARY KEY(EMPLOYEE_ID) ); CREATE TABLE EMP_DETAIL( EMPLOYEE_ID BIGINT NOT NULL , INS_ID VARCHAR(20) , SALARY int, PRIMARY KEY(EMPLOYEE_ID ,INS_ID), FOREIGN KEY(EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID) ); I want to insert a value in the emp table and the emp_detail table. If the employee already exists, then only insert a record in the emp_details table. This is the stored procedure: CREATE PROCEDURE INSERTEMP( IN EMPLOYEE_NO VARCHAR(30), IN EMPLOYEE_TYPE VARCHAR(20), IN INS_ID VARCHAR(20), IN SALARY INT ) BEGIN DECLARE EMPLOYEE_ID BIGINT; SET #EMPLOYEE_ID= (SELECT IFNULL((SELECT EMPLOYEE_ID FROM EMPLOYEE WHERE EMPLOYEE_NO=EMPLOYEE_NO AND EMPLOYEE_TYPE=EMPLOYEE_TYPE) ,'0')); IF #EMPLOYEE_ID=0 THEN INSERT INTO EMPLOYEE (EMPLOYEE_NO ,EMPLOYEE_TYPE ) VALUES (EMPLOYEE_NO,EMPLOYEE_TYPE); SET #EMPLOYEE_ID=(SELECT LAST_INSERT_ID()); END IF; INSERT INTO EMP_DETAIL(EMPLOYEE_ID,INS_ID,SALARY) VALUES (#EMPLOYEE_ID,INS_ID,SALARY); END If I call the procedure: CALL INSERTEMP(100, 'PERM','AAA',100); first execute it created id as 1 in emp table if execute it in second time also insert with new id and third time it fails. Can someone help?
Find difference between 2 dates in 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.