What's wrong with this SELECT Statement? - mysql

I'm getting this error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' JOIN `shirts_link` ON `shirts_link`.`shirt_id`=`shirts`.`id`
JOIN `shirt_sizes' at line 1
I've been combing over it for nearly a half an hour now and can't figure it out. Can someone please point it out to me? Here's the code.
SELECT
`shirts`.`shirt_name`,
`shirts`.`men` AS `main_photo`,
GROUP_CONCAT ( `shirt_sizes`.`size_name` ) AS `sizes`
FROM
`shirts`
JOIN
`shirts_link` ON `shirts_link`.`shirt_id`=`shirts`.`id`
JOIN
`shirt_sizes` ON `shirt_sizes`.`id`=`shirts_link`.`size_id`
JOIN
`shirt_prices` ON `shirt_prices`.`id`=`shirts_link`.`price_id`
WHERE `men`!=''
GROUP BY
`shirt_prices`.`price_cat`
In case the problem goes deeper than this script, here's the other tables I'm trying to tie together.
Shirts Table
CREATE TABLE shirts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
shirt_name VARCHAR(20) NOT NULL,
men VARCHAR(10) NULL,
women VARCHAR(10) NULL,
boys VARCHAR(10) NULL,
girls VARCHAR(10) NULL,
babies VARCHAR(10) NULL,
)ENGINE=INNODB;
INSERT INTO shirts(shirt_name,men,women,boys,girls,babies) VALUES
('Crewneck Tee','me_crn','wo_crn','bo_crn','gi_crn','ba_crn'),
('V-Neck Tee','me_vnc','wo_vnc','','',''),
('Scoop Neck Tee','','wo_sco','','',''),
('Raglan Tee','me_rag','wo_rag','bo_rag','gi_rag',''),
('Ringer Tee','me_rin','wo_rin','bo_rin','gi_rin',''),
('Cap Sleeve Tee','','wo_cap','','gi_cap',''),
('Tank Top','me_tan','wo_tan','bo_tan','gi_tan',''),
('Spaghetti Strap','','wo_spa','','',''),
('Hoodie','me_hod','wo_hod','bo_hod','gi_hod','ba_hod'),
('Onsie','','','','','ba_ons');
Size Table
CREATE TABLE shirt_sizes (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
size_name VARCHAR(10) NOT NULL
)ENGINE=INNODB;
INSERT INTO shirt_sizes(size_name) VALUES
('new born'),
('6 months'),
('12 months'),
('18 months'),
('2T'),
('3T'),
('4T'),
('5T'),
('x-small'),
('small'),
('medium'),
('large'),
('1x-large'),
('2x-large'),
('3x-large'),
('4x-large'),
('5x-large');
Price Table
CREATE TABLE shirt_prices (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
price_cat VARCHAR(10) NOT NULL,
price NUMERIC(6,2) NOT NULL
)ENGINE=INNODB;
INSERT INTO shirt_prices(price_cat,price) VALUES
('crn_01','25.40'),('crn_02','26.30'),('crn_03','27.20'),('crn_04','28.10'),
('crn_05','29.11'),
('vnc_01','26.21'),('vnc_02','27.11'),('vnc_03','28.01'),('vnc_04','29.02'),
('vnc_05','30.80'),
('sco_01','28.10'),
('rag_01','29.22'),('rag_02','30.44'),('rag_03','31.70'),('rin_01','29.22'),
('rin_02','30.44'),('rin_03','31.70'),
('cap_01','29.04'),('cap_02','30.26'),
('tan_01','25.31'),('tan_02','26.21'),
('spa_01','26.30'),('spa_02','27.11'),
('hod_01','35.21'),('hod_02','36.11'),('hod_03','37.10'),('hod_04','38.11');
Shirt Link Table
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT UNSIGNED NOT NULL,
size_id INT UNSIGNED NOT NULL,
price_id INT UNSIGNED NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (`shirt_id`) REFERENCES `shirts`(`id`),
FOREIGN KEY (`size_id`) REFERENCES shirt_sizes(`id`),
FOREIGN KEY (`price_id`) REFERENCES shirt_prices(`id`)
)ENGINE=INNODB;
INSERT INTO shirts_link (adult, kids, babies, shirt_id, size_id, price_id) VALUES
('n','n','y','1','1','1'),('n','n','y','1','2','1'),('n','n','y','1','3','1'),('n','n','y','1','4','1'),
('n','n','y','1','5','1'),('n','n','y','1','6','1'),('n','n','y','1','7','1'),('n','n','y','1','8','1'),
('n','y','n','1','9','1'),('y','y','n','1','10','1'),('y','y','n','1','11','1'),('y','y','n','1','12','1'),
('y','y','n','1','13','1'),('y','n','n','1','14','2'),('y','n','n','1','15','3'),('y','n','n','1','16','4'),
('y','n','n','1','17','5'),
('y','n','n','2','10','6'),('y','n','n','2','11','6'),('y','n','n','2','12','6'),('y','n','n','2','13','6'),
('y','n','n','2','14','7'),('y','n','n','2','15','8'),('y','n','n','2','16','9'),('y','n','n','2','17','10'),
('y','n','n','3','10','11'),('y','n','n','3','11','11'),('y','n','n','3','12','11'),('y','n','n','3','13','11'),
('y','n','n','3','14','11'),
('y','y','n','4','10','12'),('y','y','n','4','11','12'),('y','y','n','4','12','12'),('y','y','n','4','13','12'),
('y','n','n','4','14','13'),('y','n','n','4','15','14'),
('y','y','n','5','10','15'),('y','y','n','5','11','15'),('y','y','n','5','12','15'),('y','y','n','5','13','15'),
('y','n','n','5','14','16'),('y','n','y','5','15','17'),
('y','y','n','6','10','18'),('y','y','n','6','11','18'),('y','y','n','6','12','18'),('y','y','n','6','13','18'),
('y','n','n','6','14','19'),
('y','y','n','7','10','20'),('y','y','n','7','11','20'),('y','y','n','7','12','20'),('y','y','n','7','13','20'),
('y','n','n','7','14','21'),
('y','n','n','8','10','22'),('y','n','n','8','11','22'),('y','n','n','8','12','22'),('y','n','n','8','13','22'),
('y','n','n','8','14','23'),
('n','n','y','9','5','24'),('n','n','y','9','6','24'),('n','n','y','9','7','24'),('n','n','y','9','8','24'),
('n','n','y','9','9','24'),('y','y','n','9','10','24'),('y','y','n','9','11','24'),('y','y','n','9','12','24'),
('y','y','n','9','13','24'),('y','y','n','9','14','25'),('y','n','n','9','15','26'),('y','n','n','9','16','27'),
('y','n','n','9','16','25'),
('n','n','y','10','1','1'),('n','n','y','10','2','1'),('n','n','y','10','3','1'),('n','n','y','10','4','1');

Where clause belongs after the entire from clause, meaning after all the joins.

You have this WHERE clause in the wrong place:
WHERE `men`!=''
Move it in front of your GROUP BY clause.
UPDATE: Here is a reformatted version of your query:
SELECT `shirts`.`shirt_name`
, `shirts`.`men` AS `main_photo`
, GROUP_CONCAT ( `shirt_sizes`.`size_name` ) AS `sizes`
FROM `shirts`
JOIN `shirts_link`
ON `shirts_link`.`shirt_id`=`shirts`.`id`
JOIN `shirt_sizes`
ON `shirt_sizes`.`id`=`shirts_link`.`size_id`
JOIN `shirt_prices`
ON `shirt_prices`.`id`=`shirts_link`.`price_id'
WHERE `men` != ''
GROUP BY `shirt_prices`.`price_cat`

Related

Didnt display results in database

I have a little problem with one database. I have already entered data in the individual tables in the database. The problem is that with this code, it displays the column names, but didnt return rows. I can't find the error. I think the problem is in JOIN itself. Any ideas for solving the problem?
SELECT cars.brand,
cars.model,
cars.yearofproduction,
cars.engine_type,
parts.part_name,
parts.price AS MONEY,
parts.quantity
FROM CATALOG
JOIN parts
ON parts.part_name = parts.id
JOIN cars
ON CATALOG.car_id = cars.id
WHERE quantity >= '0'
HAVING MONEY < (
SELECT AVG(price)
FROM cars
);
And here the tables. I've already insert values in the tables.
CREATE TABLE CATALOG.parts
(
id INT AUTO_INCREMENT PRIMARY KEY,
part_name VARCHAR(255) NOT NULL,
price DECIMAL NOT NULL,
DESCRIPTION VARCHAR(255) DEFAULT NULL,
quantity TINYINT DEFAULT 0
);
CREATE TABLE CATALOG.cars
(
id INT AUTO_INCREMENT PRIMARY KEY,
brand VARCHAR(255) NOT NULL,
model VARCHAR(255) NOT NULL,
yearofproduction YEAR NOT NULL,
engine_type SET('Diesel', 'A95', 'Gas', 'Metan')
);
CREATE TABLE CATALOG.catalog
(
part_id INT NOT NULL,
CONSTRAINT FOREIGN KEY(part_id) REFERENCES parts(id)
ON DELETE RESTRICT ON UPDATE CASCADE,
car_id INT NOT NULL,
CONSTRAINT FOREIGN KEY(car_id) REFERENCES cars(id)
ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY(part_id, car_id)
);

Correct SQL Multiply across tables

I am at the end of my rope. I am learning SQL for a class. I have tried to get something akin to the to work, but to no avail. Can someone take a look at it.
Keep in mind that I'm new to this. I am trying to get the code to make subtotal equal the sum of column qty multiplied by the sum of column donutPrice in the donut table. I can't find much except for joins and if I do that, I can't use the join as a value.
The ultimate goal is to make it kinda automated.
CREATE TABLE donut
(
donutID int(50) not null auto_increment primary key,
donutName varchar(50) not null,
donutDesc varchar(200),
donutPrice dec(8,2)
);
CREATE TABLE customer
(
customerID int(50) not null auto_increment primary key,
fname char(50) not null,
lname char(50) not null,
address varchar(50) not null,
apartment varchar(10),
city char(50) not null,
state char(2) not null,
zip dec(5) not null,
homeph varchar(10),
mobileph varchar(10),
otherph varchar(10)
);
CREATE TABLE invoice
(
orderID int(50) not null auto_increment primary key,
notes varchar(50) not null,
orderdate date not null,
customerID int(50) not null default 1,
foreign key (customerID) references customer(customerID)
);
CREATE TABLE invoice_line_item
(
donutID int(50) not null,
orderID int(50) not null,
qty dec not null,
subtotal dec(10,2),
subtotal= sum('qty'*'donutPrice') FROM (invoice_line_item, donut),
primary key (donutID, orderID),
foreign key(donutID) references donut(donutID),
foreign key(orderID) references invoice(orderID)
);
ALTER TABLE donut AUTO_INCREMENT=1;
ALTER TABLE customer AUTO_INCREMENT=1001;
ALTER TABLE invoice AUTO_INCREMENT=500;
I guess you want a result looking like this:
OrderID subtotal
1 12.50
2 15.00
27.50
You get that with a query like this:
SELECT invoice.orderID, SUM(invoice_line_item.qty * donut.donutPrice) subtotal
FROM invoice
JOIN invoice_line_item ON invoice.orderID = invoice_line_item.orderID
JOIN donut ON invoice_line_item.donutID = donut.donutID
GROUP BY invoice.orderID WITH ROLLUP
Did you cover entity-relationship data in your class? Your entities are invoice, invoice_line_item, and donut (and your other tables). The relationships between them appear in the ON clauses of the JOIN operations.
Start with a query, and get it working. Then you can create a view ... which is nothing more or less than an encapsulated query.

Selecting data from other table using a foreign key?

So I'm trying to get the clan tag from ranks table for a specific member_id. But I don't understand how to do that. I used INNER JOIN commands but nothing works.
CREATE TABLE ranks(
rank VARCHAR(8),
tag VARCHAR(18) NOT NULL,
PRIMARY KEY(rank)
);
CREATE TABLE member(
member_id INT UNSIGNED AUTO_INCREMENT,
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(15),
ign VARCHAR(20) NOT NULL,
rank VARCHAR(8) NOT NULL,
joined_date DATE NOT NULL,
dob DATE,
sex ENUM('M','F') NOT NULL,
country VARCHAR(3) NOT NULL,
PRIMARY KEY(member_id),
FOREIGN KEY(rank) REFERENCES ranks(rank)
);
INSERT INTO ranks VALUES
('Founder','|NoMercy|King'),
('Admin','^1|NoMercy|^7'),
('TmpAdmin','^5|NoMercy|^7'),
('Pro','^1|NoMercy PRO|^7'),
('Member','^4|NoMercy|^7'),
('Banned','BANNED');
INSERT INTO member VALUES
(NULL,'Reznov','NULL','REZ','Member','2017/12/22','1954/02/28','M','RUS'),
(NULL,'Amanda','NULL','S3XXY|G!RL|','Pro','2018/01/05','1992/01/25','F','USA'),
(NULL,'Elmasri','Navathe','INDIAN_Noob _XOX','TmpAdmin','2018/04/02','1960/08/25','M','IND'),
(NULL,'Tony','Silva','Cool KiD','Member','2018/04/26','1988/02/22','M','BR'),
(NULL,'Hashan','NULL','Big Papa','Member','2018/05/08','1996/06/12','M','NZ'),
(NULL,'Emma','Watson','EmmaXXX','Banned','2018/06/10','1985/05/22','F','UK');
I need to output the correct tag for specific member. It should only display the tag.
Please help!
below query should be worked
SELECT tag FROM ranks INNER JOIN member ON ranks.rank=member.rank
where member_id=1;
http://www.sqlfiddle.com/#!9/e90392/1
tag
^4|NoMercy|^7

Try to join more than one table

Tables And what i need
I'm trying to Create a view with 5 tables for a school project. The statement so far goes like this:
CREATE view lager AS
Select produkt.produktNumber,
(SELECT buyphone.lagerNummer FROM bluecity.buyphone) AS 'Lagernummer',
produkt.produktBrand,
produkt.produktModel,
sizeMemory.memoryInformation,
(SELECT buyphone.colorValue FROM bluecity.buyphone) AS 'Farve',
(SELECT buyphone.conditionValue FROM bluecity.buyphone) AS 'Stand'
FROM bluecity.produkt
JOIN bluecity.sizememory ON bluecity.produkt.memorySize = bluecity.sizememory.memorySize
JOIN bluecity.color ON buyphone.colorValue = bluecity.color.colorInformation
JOIN bluecity.conditions ON buyphone.conditionValue = bluecity.conditions.conditionInformation
But i can't seem to get the joins right. The main table bluecity.produkt needs to join some of it's values with other table. The first join with Memory size works, but that is it. The main table is supposed to hold an Integer Value which draws meaning from the joined table if that makes sense.
Help is much appriciated, if you can explain why and how it would be even better so i can try to understand.
Added create stmt
CREATE DATABASE bluecity;
CREATE TABLE bluecity.Member
(memberNumber INTEGER(5) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(25) NOT NULL,
address VARCHAR(40) NOT NULL,
zipNumber INTEGER(4) NOT NULL,
phoneNumber INTEGER(8) NOT NULL,
email VARCHAR(35) NOT NULL,
statusValue INTEGER(1) NOT NULL,
FOREIGN KEY (zipNumber) REFERENCES bluecity.zipCode(zipNumber),
FOREIGN KEY (statusValue) REFERENCES bluecity.ID(statusValue),
PRIMARY KEY (memberNumber));
CREATE TABLE bluecity.ID
(statusValue INTEGER(1) NOT NULL,
information VARCHAR(19) NOT NULL,
PRIMARY KEY (statusValue))
CREATE TABLE bluecity.zipCode
(zipNumber INTEGER(4) NOT NULL,
city VARCHAR(32) NOT NULL,
PRIMARY KEY (zipNumber));
CREATE TABLE bluecity.Produkt
(produktNumber INTEGER(5) NOT NULL,
produktType VARCHAR(30) NOT NULL,
produktBrand VARCHAR(30) NOT NULL,
produktModel VARCHAR(30) NOT NULL,
memorySize INTEGER(2) NOT NULL,
FOREIGN KEY (memorySize) REFERENCES bluecity.sizeMemory(memorySize),
PRIMARY KEY (produktNumber));
CREATE TABLE bluecity.Conditions
(conditionValue INTEGER(1) NOT NULL,
conditionInformation VARCHAR(13) NOT NULL,
PRIMARY KEY (conditionValue));
CREATE TABLE bluecity.sizeMemory
(memorySize INTEGER(1) NOT NULL,
memoryInformation VARCHAR(5) NOT NULL,
PRIMARY KEY (memorySize));
CREATE TABLE bluecity.Color
(colorValue INTEGER(2) NOT NULL,
colorInformation VARCHAR(20) NOT NULL,
PRIMARY KEY (colorValue));
CREATE TABLE bluecity.Prices
(conditionValue INTEGER(1) NOT NULL,
produktNumber INTEGER(5) NOT NULL,
price INTEGER(6) NOT NULL,
FOREIGN KEY (conditionValue) REFERENCES bluecity.Conditions(conditionValue),
FOREIGN KEY (produktNumber) REFERENCES bluecity.Produkt(produktNumber),
PRIMARY KEY (conditionValue, produktNumber));
CREATE TABLE bluecity.buyPhone
(IMEI Integer(15) NOT NULL,
lagerNummer INTEGER(7) NOT NULL,
produktNumber INTEGER(5) NOT NULL,
colorValue INTEGER(2) NOT NULL,
conditionValue INTEGER(1) NOT NULL,
FOREIGN KEY (produktNumber) REFERENCES bluecity.Produkt(produktNumber),
FOREIGN KEY (colorValue) REFERENCES bluecity.Color(colorValue),
FOREIGN KEY (conditionValue) REFERENCES bluecity.Conditions(conditionValue), PRIMARY KEY (IMEI));
You should also make a join on the bluecity.buyphone instead of having it as subqueries in your select, as it is related to your produkt table.
Something like this:
Select produkt.produktNumber,
buyphone.lagerNummer AS 'Lagernummer',
produkt.produktBrand,
produkt.produktModel,
sizeMemory.memoryInformation,
buyphone.colorValue AS 'Farve',
buyphone.conditionValue AS 'Stand'
FROM produkt
JOIN buyphone ON buyphone.produktNumber = produkt.produktNumber
JOIN sizememory ON produkt.memorySize = sizememory.memorySize
JOIN color ON buyphone.colorValue = color.colorInformation
JOIN conditions ON buyphone.conditionValue = conditions.conditionInformation
Select DISTINCT produkt.produktNumber AS 'Produkt #',
buyphone.lagerNummer AS 'Lager #',
produkt.produktBrand AS 'Mærke',
produkt.produktModel AS 'Model',
sizeMemory.memoryInformation 'Hukommelse',
color.colorInformation AS 'Farve',
conditions.conditionInformation AS 'Stand',
prices.price AS 'Pris'
FROM bluecity.buyphone
JOIN bluecity.produkt ON bluecity.buyphone.produktNumber = bluecity.produkt.produktNumber
JOIN bluecity.sizememory ON bluecity.produkt.memorySize = bluecity.sizememory.memorySize
JOIN bluecity.color ON bluecity.buyphone.colorValue = bluecity.color.colorValue
JOIN bluecity.conditions ON bluecity.buyphone.conditionValue = bluecity.conditions.conditionValue
JOIN bluecity.prices ON bluecity.produkt.produktNumber = bluecity.prices.produktNumber
JOIN bluecity.prices p1 ON bluecity.buyphone.conditionValue = bluecity.prices.conditionValue
I got it working like this, thx guys.
Edit: there still was some flaws which i think is fixed now.

MYSQL Select One Row With Less

I'm making a database for a unit and I need a query that selects the vet with less appointments assigned so I can assign the next appointment to him or her. I don't know how to start, but I'm pretty sure I'll have to use variables here. Those are my tables:
CREATE TABLE IF NOT EXISTS staff (
stafId MEDIUMINT UNSIGNED AUTO_INCREMENT,
stafAdd VARCHAR(150) NOT NULL,
stafConNum VARCHAR(15) NOT NULL,
stafEma VARCHAR(40) NOT NULL,
stafFirNam VARCHAR(20) NOT NULL,
stafLasNam VARCHAR(30) NOT NULL,
stafPos ENUM('nurse', 'vet') NOT NULL,
PRIMARY KEY (stafId)
) engine = InnoDB;
CREATE TABLE IF NOT EXISTS vet (
vetId MEDIUMINT UNSIGNED AUTO_INCREMENT,
FOREIGN KEY (vetId) REFERENCES staff(stafId),
PRIMARY KEY (vetId)
) engine = InnoDB;
CREATE TABLE IF NOT EXISTS appointment (
appoId MEDIUMINT UNSIGNED AUTO_INCREMENT,
appoDat DATETIME NOT NULL,
appoPetId MEDIUMINT UNSIGNED,
FOREIGN KEY (appoPetId) REFERENCES pet(petId),
appoVetId MEDIUMINT UNSIGNED,
FOREIGN KEY (appoVetId) REFERENCES vet(vetId),
PRIMARY KEY (appoId)
) engine = InnoDB;
You should start by looking up the mysql MIN() function. Follow that up with learning about JOINs and you'll be breezing through this.
You could get the vet's with the number of appointment like this:
SELECT
*
FROM
(
SELECT
vet.vetId,
COUNT(*) AS nbrOfAppointment
FROM
vet
JOIN appointment
ON vet.vetId = appointment.appoVetId
) AS tbl
ORDER BY tbl.nbrOfAppointment ASC
This Request gives you the number of appointement per vet ordered by number of appointement.
Select vet.id, count(*) as nb_appointement
from vet
inner join appointement app on vet.vetId = app.appoVetId
group by vet.id
order by nb_appointement asc