For my CIS class, I have SQL project, I'm still very new to SQL and trying to learn it. Any help would be greatly appreciated.
Query: Write a query to show the total value of all the orders that customers living in San Jose or Turlock have placed.
This is what I tried and giving me an error:
select SUM(price * quantity) as Revenue
FROM customer,salesorder
where customer.cno = salesorder.cno AND
zip = 95124 AND zip = 95380
AND zip = 95382
The error I'm getting is: #1054 - Unknown column 'price' in 'field list'
Table: Table
Database codes:
create table zipcode (
zip integer(5) primary key,
city varchar(30),
State varchar(20));
create table employee (
eno varchar(10) primary key,
ename varchar(30),
zip integer(5) references zipcode(zip),
hire_date date);
create table book (
bno integer(5) primary key,
bname varchar(30),
qoh integer(5) not null,
price dec(6,2) not null);
create table customer (
cno integer(5) primary key,
cname varchar(30),
street varchar(30),
zip integer(5) references zipcode(zip),
phone char(12));
create table salesOrder (
ono integer(5) primary key,
cno integer(5) references customer(cno),
eno varchar(10) references employees(Eno),
received date,
shipped date
);
create table orderLine (
ono integer(5) references salesOrder(ono),
bno integer(5) references book(bno),
quantity integer(10) not null,
primary key (ono, bno));
insert into zipcode values (98225, 'Bellingham', 'WA');
insert into zipcode values (95388, 'Winton', 'CA');
insert into zipcode values (44242, 'Stow', 'OH');
insert into zipcode values (61536, 'Hanna city', 'IL');
insert into zipcode values (01254, 'Richmond', 'MA');
insert into zipcode values (95124, 'San Jose', 'CA');
insert into zipcode values (95382, 'Turlock', 'CA');
insert into zipcode values (95380, 'Turlock', 'CA');
insert into zipcode values (98102, 'Seattle', 'WA');
insert into employee values ('P0239401', 'Jones Hoffer',98225, '2000-12-12');
insert into employee values ('P0239402', 'Jeffrey Prescott',95388, '2016-11-07');
insert into employee values ('P0239403', 'Fred NcFaddeb',95124, '2008-09-01');
insert into employee values ('P0239404', 'Karen Ives',98102, '2014-05-21');
insert into book values (10501, 'Forensic Accounting',200, 229.99);
insert into book values (10502, 'SAP Business One',159, 149.99);
insert into book values (10503, 'Fraud Cases',190, 179.99);
insert into book values (10504, 'CPA Review',65, 279.99);
insert into book values (10605, 'Quickbooks for Business',322, 59.99);
insert into book values (10704, 'Financial Accounting',129, 164.99);
insert into book values (10879, 'Managerial Accounting',155, 114.99);
insert into book values (10933, 'Cost Accounting',122, 219.99);
insert into book values (10948, 'Intermediate Accounting',123, 164.99);
insert into book values (10965, 'Accounting Information Systems',211, 259.99);
insert into book values (10988, 'XBRL in Nutshell',124, 109.99);
insert into customer values (23511, 'Michelle Kuan', '123 Main St.',98225, '360-636-5555');
insert into customer values (23512, 'George Myer', '237 Ash Ave.',95124, '312-678-5555');
insert into customer values (23513, 'Richard Gold', '111 Inwood St.',95124, '312-883-7337');
insert into customer values (23514, 'Robert Smith', '54 Gate Dr.',95388, '206-832-1221');
insert into customer values (23515, 'Christopher David', '777 Loto St.',98225, '360-458-9878');
insert into customer values (23516, 'Adam Beethoven', '234 Park Rd.',95380, '209-546-7299');
insert into customer values (23517, 'Ludwig Bach', '5790 Walnut St.',95382, '209-638-2712');
insert into customer values (23518, 'Kathleen Pedersen', '1233 Federal Ave E', 98102, '360-573-7239');
insert into salesOrder values (1020, 23511, 'P0239403', '2018-01-13', '2018-01-15');
insert into salesOrder values (1021, 23513, 'P0239401', '2018-01-13', '2018-01-16');
insert into salesOrder values (1022, 23513, 'P0239402', '2018-01-15', '2018-01-17');
insert into salesOrder values (1023, 23512, 'P0239403', '2018-01-16', '2018-01-18');
insert into salesOrder values (1024, 23511, 'P0239402', '2018-01-18', '2018-01-20');
insert into salesOrder values (1025, 23511, 'P0239403', '2018-01-29', '2017-01-31');
insert into salesOrder values (1026, 23512, 'P0239404', '2018-01-30', '2018-01-31');
insert into salesOrder values (1027, 23512, 'P0239402', '2018-01-30', '2018-01-31');
insert into salesOrder values (1028, 23512, 'P0239404', '2018-01-30', '2018-01-31');
insert into salesOrder (ONO, CNO, ENO, RECEIVED) values (1029, 23513, 'P0239402', '2018-01-31');
insert into salesOrder (ONO, CNO, ENO, RECEIVED) values (1030, 23511, 'P0239401', '2018-01-31');
insert into orderLine values (1020, 10501,7);
insert into orderLine values (1020, 10502,15);
insert into orderLine values (1020, 10504,3);
insert into orderLine values (1020, 10503,6);
insert into orderLine values (1021, 10605,4);
insert into orderLine values (1022, 10605,2);
insert into orderLine values (1022, 10704,4);
insert into orderLine values (1023, 10879,4);
insert into orderLine values (1023, 10988,19);
insert into orderLine values (1024, 10502,7);
insert into orderLine values (1024, 10988,2);
insert into orderLine values (1025, 10502,4);
insert into orderLine values (1025, 10988,3);
insert into orderLine values (1025, 10948,2);
insert into orderLine values (1026, 10965,15);
insert into orderLine values (1026, 10933,5);
insert into orderLine values (1027, 10933,21);
insert into orderLine values (1028, 10933,9);
insert into orderLine values (1028, 10965,11);
insert into orderLine values (1029, 10933,4);
insert into orderLine values (1029, 10965,10);
insert into orderLine values (1029, 10988,3);
insert into orderLine values (1030, 10965,6);
You can try this.
That no make sense on
AND zip = 95124
AND zip = 95380
AND zip = 95382
I guess you want to use IN
From your table schema join Orderline and Book you will get price and quantity
select SUM(b.price * o.quantity)
FROM customer c
INNER JOIN salesorder s ON c.cno = s.cno
INNER JOIN Orderline o ON s.ono = o.ono
INNER JOIN Book b ON b.bno = o.bno
where zip IN (95124,95380,95382);
sqlfiddle : http://sqlfiddle.com/#!9/07a9c0b/8
Once check the table where the column price exists in...I see it in the table book
and yet you did not use the table book in your query... so this is how you should do it..
select SUM(Book.price * Orderline.quantity)
FROM customer
INNER JOIN salesorder ON customer.cno = salesorder.cno
INNER JOIN Orderline ON salesorder.ono = Orderline.ono
INNER JOIN Book ON Book.bno = Orderline.bno
where customer.zip IN (95124,95380,95382);
The error I'm getting is: #1054 - Unknown column 'price' in 'field list'`
As per your query you have not added the table having price column.
If you can see your DDL statements price column is in book table and you have to join this table to get the required result.
I guess this should work
SELECT SUM(book.price*orderline.quantity) AS "order value" from customer
JOIN salesorder
ON salesorder.cno=customer.cno
JOIN orderline
ON salesorder.ono=orderline.ono
JOIN book
ON orderline.bno=book.bno
JOIN zipcode
ON customer.zip = zipcode.zip
WHERE city in ('Turlock', 'San Jose')
First thing that needs correction is an aggregate function like sum, avg etc need a group clause as well. For example
select sum(price * quantity) as revenue from customers c, salesorder s where
c.customer_no=s.customer_no group by c.customer_no
It will return the total revenue a single customer generated . Second thing is
for filtering based on several Zip Code you can use IN clause like
where zip_no in ('A','B','C')
Third please check you are writing correct column name and spelling of column as well of price
Related
Good afternoon.
I have two tables:
1 - table1 - (id, address, who_lives) who_lives field - text, comma-separated list of identifiers
2 - table2 - (id, name_of_resident)
composition of table 1
12| US Oregon | 12,13
14| US Washington| 9,11
composition of table 2
9 |Petrov
11|Sidorov
12|Ivanov
13|Popov
How to make MySQL request, preferably via join, that the result was
US Oregon |Ivanov,Popov
US Washington|Petrov,Sidorov
If possible, replace the comma with the br tag: Ivanov,Popov -> Ivanov >br< Popov
SELECT table1.address, GROUP_CONCAT(table2.name_of_resident SEPARATOR '<br>')
FROM table1
JOIN table2 ON FIND_IN_SET(table2.id, table1.who_lives)
GROUP BY table1.address
DEMO
That is not how you design tables. Don't do comma separated listes. Do either a (foreign) key from table 2 to table 1, or a table with table1.id and table2.id for each relation.
Then writing the query will be easier.
create table state (
state_id int primary key,
address varchar(255)
);
create table resident (
resident_id int primary key,
name varchar(255)
);
create table who_lives (
state_id int,
resident_id int,
primary key (state_id, resident_id)
);
insert into state values (12, 'US Oregon');
insert into state values (14, 'US Washington');
insert into resident values (9, 'Petrov');
insert into resident values (11, 'Sidorov');
insert into resident values (12, 'Ivanov');
insert into resident values (13, 'Popov');
insert into who_lives values (12, 12);
insert into who_lives values (12, 13);
insert into who_lives values (14, 9);
insert into who_lives values (14, 11);
select address, group_concat(name)
from state
join who_lives using (state_id)
join resident using (resident_id)
group by state_id;
http://sqlfiddle.com/#!9/e7041f/2
I am working on a homework assignment for a database class. Part of the assignment is creating SQL queries to accomplish various tasks using the following database:
create database homework3;
use homework3;
create table customer(
cus_code int,
cus_lname varchar(20),
cus_fname varchar(20),
cus_initial char,
cus_areacode int,
cus_phone int,
primary key(cus_code));
create table invoice(
inv_number int,
cus_code int,
inv_date date,
primary key(inv_number),
foreign key(cus_code) references customer(cus_code));
create table vendor(
vend_code int,
vend_name varchar(30),
vend_contact varchar(30),
vend_areacode int,
vend_phone int,
primary key(vend_code));
create table product(
prod_code int,
prod_desc varchar(50),
prod_price int,
prod_quant int,
vend_code int,
primary key(prod_code),
foreign key(vend_code) references vendor(vend_code));
create table line(
inv_number int,
prod_code int,
line_units int,
primary key(inv_number, prod_code),
foreign key(inv_number) references invoice(inv_number),
foreign key(prod_code) references product(prod_code));
/* cus_code, cus_lname, cus_fname, cus_initial, cus_areacode, cus_phone */
insert into customer values (10010, "Ramas", "Alfred", 'A', 615, 8442573);
insert into customer values (10011, "Dunne", "Leona", 'K', 713, 8941238);
insert into customer values (10012, "Smith", "Kathy", 'W', 615, 8942285);
insert into customer values (10013, "Olowski", "Paul", 'F', 615, 2221672);
insert into customer values (10014, "Orlando", "Myron", NULL, 615, 2971228);
/* inv_number, cus_code, inv_date */
insert into invoice values (1001, 10011, '2008-08-03');
insert into invoice values (1002, 10014, '2008-08-04');
insert into invoice values (1003, 10012, '2008-03-20');
insert into invoice values (1004, 10011, '2008-09-23');
/* vend_code, vend_name, vend_contact, vend_areacode, vend_phone */
insert into vendor values (232, "Bryson", "Smith", 615, 2233234);
insert into vendor values (235, "SuperLoo", "Anderson", 615, 2158995);
/* prod_code, prod_desc, prod_price, prod_quant, vend_code */
insert into product values (12321, "hammer", 189 ,20, 232);
insert into product values (65781, "chain", 12, 45, 235);
insert into product values (34256, "tape", 35, 60, 235);
insert into product values (12333, "hanger", 200 ,10, 232);
/* inv_number, prod_code, line_units */
insert into line values (1001, 12321, 1);
insert into line values (1001, 65781, 3);
insert into line values (1002, 34256, 6);
insert into line values (1003, 12321, 5);
insert into line values (1002, 12333, 6);
And the statement in question is:
I tried:
use homework3;
select product.prod_desc, customer.cus_fname, customer.cus_lname from product,
customer
natural join line where line.inv_number is not null;
which returned absolutely wrong results with every person having bought several of each product. Thanks in advance for the help.
You need to specify the joining conditions between line and the other tables. You need to include the invoice table to get the relationship to the customer.
SELECT cus_fname, cus_lname, prod_desc
FROM customer AS c
JOIN invoice AS i ON c.cus_code = i.cus_code
JOIN line AS l ON l.inv_number = i.inv_number
JOIN product AS p ON p.prod_code = l.prod_code
ORDER BY cus_code
Get out of the habit of using implicit joins (FROM customer, product), list each table you're joining specifically with an ANSI JOIN with the appropriate ON condition.
If you removed the natural join from your query, you would have a cross-product of products and customers: matching each product with each customer. Adding the join just matches irrelevant info to each pairing.
Instead, what you want is every row in line (which I presume is a purchase) joined with the corresponding customer & product so you can get the desired fields about each.
Suppose I have a tables
MEMBER
member_id(using sequence),
member_lastname,
member_firstname,
BOOKS
book_num(using sequence),
book_title,
book_author,
RENTAL
rent_num(pk fk),
member_id(fk),
rent_date,
DETAILRENTAL
rent_num(fk),
detail_fee,
detail_returndate,
detail_duedate,
how do I add these values both in detail rental and rental?
member_lastname:Simon
member_id: (base on the last_name inserted)
detail_fee :40
detail_returndate :12-03-2016
detail_duedate:12-01-2016
Try this, if the rent_num is auto increment :
BEGIN;
INSERT INTO RENTAL (member_id, rent_date)
SELECT member_id, NOW() FROM MEMBER WHERE member_lastname = 'Simon';
INSERT INTO DETAILRENTAL (rent_num, detail_fee, detail_returndate, detail_duedate)
VALUES(LAST_INSERT_ID(), 40, '12-03-2016', '12-01-2016');
COMMIT;
I am having a hard time implementing these queries. Can someone help.
Query 1: Find the names of the drivers who took at least 2 exams at the same branch.
Query 2: Find the names of the drivers whose exam scores get lower when he/she took more exams.
my code
create table branch(branch_id integer, branch_name varchar(20), branch_addr varchar(50), branch_city varchar(20), branch_phone integer);
create table driver(driver_ssn integer, driver_name varchar(20), driver_addr varchar(50), driver_city varchar(20), driver_birthdate date, driver_phone integer);
create table license(license_no integer, driver_ssn integer, license_type char, license_class integer, license_expiry date, issue_date date, branch_id integer);
create table exam(driver_ssn integer, branch_id integer, exam_date date, exam_type char, exam_score integer);
insert into branch values(10,'Main','1234 Main St.','Vancouver',5551234);
insert into branch values(20,'Richmond','23 No. 3 road','Richmond',5552331);
insert into branch values(30,'West Creek','251 creek rd.','Sechelt',5552511);
insert into branch values(40,'Blenheim','1342 W.22 Ave.','Burnaby',5551342);
insert into driver values(11111111, 'Bob Smith','111 E.11 st.', 'Vancouver', '1975-01-01',5551111);
insert into driver values(22222222, 'John Walters','222 E.22 st.', 'Burnaby', '1976-02-02',5552222);
insert into driver values(33333333, 'Troy Rops','333 W.33 ave.', 'Richmond', '1970-03-03',5553333);
insert into driver values(44444444, 'Kevin Mark','444 E.4 ave.', 'Vancouver', '1974-04-04',5554444);
insert into license values(1, 11111111,'D', 5, '1999-05-25','1997-05-25',20);
insert into license values(2, 22222222,'D', 5, '1998-08-29','1996-08-29',40);
insert into license values(3, 33333333,'L', 5, '1997-12-27','1997-06-27',20);
insert into license values(4, 44444444,'D', 5, '1999-08-30','1997-08-30',40);
insert into exam values(11111111,20, '1997-05-25', 'D',79);
insert into exam values(11111111,20, '1997-12-02', 'L',97);
insert into exam values(22222222,30, '1996-05-06', 'L',25);
insert into exam values(22222222,40, '1996-06-10', 'L',51);
insert into exam values(22222222,40, '1996-08-29', 'D',81);
insert into exam values(33333333,10, '1997-07-07', 'L',45);
insert into exam values(33333333,20, '1997-06-27', 'L',49);
insert into exam values(33333333,20, '1997-07-27', 'L',61);
insert into exam values(44444444,10, '1997-07-27', 'L',71);
insert into exam values(44444444,20, '1997-08-30', 'D',65);
My Attempt on Query 1:
select driver.driver_name
from exam, branch, driver
where driver.driver_ssn = exam.driver_ssn AND
(select exam.driver_ssn
from exam
group by exam.driver.ssn, exam.branch_id
having count(exam.branch_id) >= 2);
Well, the first query might look something like this...
SELECT something
FROM somehere d
JOIN somewhere_else e
ON e.some_other_thing = d.some_other_thing
GROUP
BY d.some_other_thing
, e.and_yet_another_thing
HAVING COUNT(*) >= 2;
Assuming question 2 means 'find the names of drivers who posess a subsequent exam score lower than a preceding one', then that might look like this...
SELECT DISTINCT d.driver_name
FROM ... d
JOIN ... preceding
ON preceding.... = d....
JOIN ... subsequent
ON subsequent.... = preceding....
AND subsequent.... > preceding....
AND subsequent.... < preceding....;
first query something like this
select d.driver_name from driver d inner join exam e on d.driver_ssn=e.driver_ssn inner join branch b on e.branch_id=b.branch_id
group by d.driver_name
having count(*)>=2
I have fetching record from table named Pizza_Table.
Pizza_table Schema
id | pizza name | pizza_topping_ids
1 | pizza1 |1,2,3
2 | pizza2 |2,3
3 | pizza3 |4,5,6
Actually my functionality is searching pizza based on pizza_topping_ids
I have work allot in this searching. but I can't find proper solution. .
I can't use IN clause for fetching record.
I have tried out :
1)
SELECT *
FROM `pizza_table`
WHERE `pizza_topping_id` REGEXP '[[:<:]]2[[:>:]]'
Problem : fetch all record having value 2
2)
SELECT *
FROM `pizza_table`
WHERE `pizza_topping_id` REGEXP '[[:<:]]1,2,3,4[[:>:]]'
but MySQL returned an empty result set,
If comma separated values are not match to any record then return result which is most having value in topping_id column.
EX: If i have run above query for find out pizza having 1,2,3,4 topping_id, but In database no pizza exist having such topping_id combination then result will be most covered id should be displayed like 'pizza1' having pizza_topping_id 1,2,3.
Sorry for bad English as well as format, but try to understand my problem.
Thanks in advance.
Please help me.
Just try this-
SELECT * FROM `pizza_table` WHERE FIND_IN_SET(2,pizza_topping_id)
OR REGEXP
SELECT * FROM `pizza_table` WHERE pizza_topping_id REGEXP '^2,|,2$|,2,' OR pizza_topping_id =2
Please see the demo : demo
RegEx is slow on very big data sets.
MySQL has a FIND_IN_SET(value, column).
This may not answer your question directly but just only a suggestion :D
First, the rows on table pizza_topping doesn't need to have values separated as commas. It's hard to search for values like this using the current design. The values should be stored in rows not in column.
Consider this following schema,
CREATE TABLE PIZZA
(
PizzaID INT PRIMARY KEY,
NAME VARCHAR(50) UNIQUE,
PRICE DECIMAL(10,2)
);
INSERT INTO PIZZA VALUES (1,'Sunny Side Up Pizza', 120);
INSERT INTO PIZZA VALUES (2,'BBQ Chicken Pizza', 200);
INSERT INTO PIZZA VALUES (3,'Muffuletta Pizza', 175);
INSERT INTO PIZZA VALUES (4,'Caramelized Onion Pizza', 135);
INSERT INTO PIZZA VALUES (5,'Broccoli Deep Dish Pizza', 150);
CREATE TABLE TOPPINGS
(
ToppingID INT PRIMARY KEY,
NAME VARCHAR(50) UNIQUE
);
INSERT INTO TOPPINGS VALUES (1,'Pepperoni');
INSERT INTO TOPPINGS VALUES (2,'Mushroom');
INSERT INTO TOPPINGS VALUES (3,'Sausage');
INSERT INTO TOPPINGS VALUES (4,'Cheese');
INSERT INTO TOPPINGS VALUES (5,'Garlic');
INSERT INTO TOPPINGS VALUES (6,'Ham');
INSERT INTO TOPPINGS VALUES (7,'Tomato Sauce');
And the the records for PIZZA_TOPPING should have multiple rows of ToppingID for each PizzaID.
CREATE TABLE PIZZA_TOPPINGS
(
PizzaID INT,
ToppingID INT,
CONSTRAINT tb_fk1 FOREIGN KEY (PizzaID)
REFERENCES Pizza(PizzaID),
CONSTRAINT tb_fk2 FOREIGN KEY (ToppingID)
REFERENCES TOPPINGS(ToppingID),
CONSTRAINT tb_UQ UNIQUE (PizzaID, ToppingID)
);
INSERT INTO PIZZA_TOPPINGS VALUES (1,1);
INSERT INTO PIZZA_TOPPINGS VALUES (1,2);
INSERT INTO PIZZA_TOPPINGS VALUES (1,3);
INSERT INTO PIZZA_TOPPINGS VALUES (2,4);
INSERT INTO PIZZA_TOPPINGS VALUES (2,5);
INSERT INTO PIZZA_TOPPINGS VALUES (2,6);
INSERT INTO PIZZA_TOPPINGS VALUES (2,7);
INSERT INTO PIZZA_TOPPINGS VALUES (3,1);
INSERT INTO PIZZA_TOPPINGS VALUES (3,3);
INSERT INTO PIZZA_TOPPINGS VALUES (3,5);
INSERT INTO PIZZA_TOPPINGS VALUES (4,2);
INSERT INTO PIZZA_TOPPINGS VALUES (5,1);
INSERT INTO PIZZA_TOPPINGS VALUES (6,7);
INSERT INTO PIZZA_TOPPINGS VALUES (6,1);
The technique of searching records like this is called Relational Division
For example, you want to search for pizza's that have ingredients of: Pepperoni, Mushroom, Sausage.
SELECT a.PIZZAID, a.NAME, a.PRICE
FROM Pizza a
INNER JOIN Pizza_Toppings b
ON a.PizzaID = b.PizzaID
INNER JOIN Toppings c
ON b.ToppingID = c.ToppingID
WHERE c.Name IN ('Pepperoni', 'Mushroom', 'Sausage')
GROUP BY a.PIZZAID, a.NAME, a.PRICE
HAVING COUNT(*) = 3
SQLFiddle Demo
or pizza's that constains of atleast: Pepperoni, Mushroom,
SELECT a.PIZZAID, a.NAME, a.PRICE
FROM Pizza a
INNER JOIN Pizza_Toppings b
ON a.PizzaID = b.PizzaID
INNER JOIN Toppings c
ON b.ToppingID = c.ToppingID
WHERE c.Name IN ('Pepperoni', 'Mushroom')
GROUP BY a.PIZZAID, a.NAME, a.PRICE
HAVING COUNT(*) = 2
SQLFiddle Demo
Theses are much better than using any other functions like FIND_IN_SET, REGEXP, etc..
Try find_in_set of mysql useful in this kind of structures
$id = 2;
$query = $this->db->query("
SELECT *
FROM `pizza_table`
WHERE FIND_IN_SET($id,pizza_topping_id)";
return $query->result();