I have an error in MySQL: Error Code: 1054. Unknown column 's.Product_id' in 'on clause'
It appears at:
SELECT c.customer_ID, p.Product_id, p.Product_price, s.Items_quantity, (p.Product_price * s.Items_quantity) AS total FROM Products p LEFT JOIN Shopping_list s ON p.Product_id = s.Product_id LEFT JOIN Customer_sign_in c ON c.customer_ID = s.customer_ID LIMIT 0, 50000
Does anyone has one idea why is it happening?
You should use LEFT JOIN
select c.customer_ID, p.Product_id, p.Product_price, s.Items_quantity,
(p.Product_price * s.Items_quantity) as total from Products p LEFT JOIN Shopping_list s ON p.Product_id = s.Product_id LEFT JOIN Customer_sign_in c ON c.customer_ID = s.customer_ID;
Are you sure you have table Shopping_list and a column named Product_id in it?
If you don't have it, create the column and try again.
drop database if exists shopmeout;
create database shopmeout;
use shopmeout;
drop table if exists Customer_sign_in;
CREATE TABLE Customer_sign_in (
customer_ID INT (50) not null,
Name_customer varchar(255) not null,
Credit_card BIGINT(100) not null,
Password_cust TEXT(150) not null,
Email_cust TEXT(50) not null,
primary key (customer_ID)
);
insert into Customer_sign_in values( 19867889, 'Stefan Darescu', 9018783269173917, 'personalcomputer84', 'stefandarescu#gmail.com');
insert into Customer_sign_in values( 11958178, 'Mogens Hemming', 5090180120973782,'masadebiliard4', 'mogenshemming#yahoo.com');
insert into Customer_sign_in values( 52562523, 'Viggo Kristian', 9015908218093698,'asawece', 'viggokristian#gmail.com');
insert into Customer_sign_in values( 42515323, 'Asbjørn Hjalmar', 0198490128761903,'esrjgads', 'asbjornhjalmar#hotmail.com');
insert into Customer_sign_in values( 69723642, 'Rasmus Bjarne', 9180583647839793,'Sgsyhdfs', 'rasmusbjarne#outlook.com');
CREATE TABLE Products (
Product_id INT(150) not null,
Product_name VARCHAR(100) not null,
Product_price DECIMAL(6,2) not null,
Deduction_price INT(50) not null,
Deadline DATE not null,
PRIMARY KEY (Product_id)
);
insert into Products values( 509687, 'Plain Flower', 59.46, 10, '2013-05-30');
insert into Products values( 485901, 'Cow Milk', 17.85, 12, '2013-04-18');
insert into Products values( 908571, 'Kærgården', 131.75, 8, '2013-04-07');
insert into Products values( 432612, 'Chicken Eggs', 27.57, 9, '2013-03-31');
insert into Products values( 853235, 'Pepsi Cherry', 25.11, 12, '2013-03-25');
insert into Products values( 357342, 'Spaghetti', 10.8, 5, '2013-03-20');
insert into Products values( 123563, 'Spaghetti sos', 22.4, 10, '2013-03-17');
delimiter //
CREATE TRIGGER first_trigger BEFORE UPDATE ON Products
FOR EACH ROW
BEGIN
IF NEW.Product_price < 20 THEN
SET NEW.Product_price = 15;
ELSEIF NEW.Product_price > 140 THEN
SET NEW.Product_price = 80;
END IF;
END;//
delimiter ;
UPDATE Products set Product_price = 25 where Product_id = 509687;
UPDATE Products set Product_price = 22 where Product_id = 908571;
CREATE TABLE Recipes (
Recipes_id INT (10) not null,
Recipe_name CHAR (150) not null,
Recipe_descrip TEXT(255) not null,
primary key (Recipes_id)
);
insert into Recipes values( 513678, 'Pancakes', 'Crack the eggs into a blender, add the flour, milk and a pinch of sea salt, and blitz until smooth.');
insert into Recipes values( 867523, 'Spaghetti Bolognese', 'Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat.');
CREATE TABLE Shopping_list (
Shopping_list_id INT (50) not null,
customer_ID INT (50) not null,
Items_quantity INT (50) not null,
primary key (Shopping_list_id),
foreign key (customer_ID)
REFERENCES
Customer_sign_in (customer_ID)
);
insert into Shopping_list values( 14081869, 19867889, 15);
insert into Shopping_list values( 15163266, 11958178, 30);
insert into Shopping_list values( 34232325, 52562523, 3);
insert into Shopping_list values( 82412845, 42515323, 7);
insert into Shopping_list values( 36272236, 69723642, 5);
CREATE TABLE Payment (
Payment_id INT(150) NOT NULL,
customer_ID INT(150) NOT NULL,
Date_pay DATETIME NOT NULL,
Shopping_list_id INT(50) NOT NULL,
PRIMARY KEY (Payment_id),
FOREIGN KEY (customer_ID)
REFERENCES Customer_sign_in (customer_ID),
FOREIGN KEY (Shopping_list_id)
REFERENCES Shopping_list (Shopping_list_id)
);
insert into Payment values( 84163801, 19867889, '2013-05-25 07:12:00', 14081869);
insert into Payment values( 89171750, 11958178, '2013-04-08 21:43:12', 15163266);
insert into Payment values( 19829247, 52562523, '2013-04-02 16:41:09', 34232325);
insert into Payment values( 35819204, 42515323, '2013-03-25 23:54:32', 82412845);
insert into Payment values( 25085739, 69723642, '2013-03-20 12:38:56', 36272236);
CREATE TABLE Has (
Product_id INT(150) NOT NULL,
Recipes_id INT(150) NOT NULL,
FOREIGN KEY (Product_id)
REFERENCES Products (Product_id),
FOREIGN KEY (Recipes_id)
REFERENCES Recipes (Recipes_id)
);
insert into Has values( 509687, 513678);
insert into Has values( 485901, 513678);
insert into Has values( 908571, 513678);
insert into Has values( 357342, 867523);
insert into Has values( 123563, 867523);
CREATE TABLE Goes (
Product_id INT(150) NOT NULL,
Shopping_list_id INT(150) NOT NULL,
FOREIGN KEY (Product_id)
REFERENCES Products (Product_id),
FOREIGN KEY (Shopping_list_id)
REFERENCES Shopping_list (Shopping_list_id)
);
insert into Goes values( 485901, 14081869);
insert into Goes values( 123563, 14081869);
insert into Goes values( 908571, 14081869);
insert into Goes values( 485901, 82412845);
insert into Goes values( 123563, 34232325);
insert into Goes values( 357342, 34232325);
select * from Shopping_list;
select * from Payment;
select * from Customer_sign_in;
select * from Recipes;
select * from Products;
select * from Has;
select * from Goes;
SELECT c.customer_ID, p.Product_id, p.Product_price, s.Items_quantity,
(p.Product_price * s.Items_quantity) AS total FROM Products p LEFT JOIN Shopping_list s ON p.Product_id = s.Product_id LEFT JOIN Customer_sign_in c ON c.customer_ID = s.customer_ID;
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;