Join table on already joined table - mysql

I'm struggling to put together a select statement that joins 3 tables.
Here's the database:
CREATE TABLE Beerstyles
(
style_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
style_name VARCHAR(50) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Breweries
(
brew_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
booth_num VARCHAR(10) NOT NULL,
brew_name VARCHAR(50) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Beers
(
beer_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
beer_name VARCHAR(50) NOT NULL,
alc_vol DECIMAL(2,1) NOT NULL,
fk_style_id INT NOT NULL,
fk_brew_id INT NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Favorites
(
fav_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_id VARCHAR(55) NOT NULL,
fk_beer_id INT NOT NULL,
fav_comment VARCHAR(255)
)ENGINE=InnoDB;
ALTER TABLE Beers ADD CONSTRAINT FK_BeerStyle_Style FOREIGN KEY (fk_style_id) REFERENCES Beerstyles (style_id);
ALTER TABLE Beers ADD CONSTRAINT FK_BeerBrew_Brew FOREIGN KEY (fk_brew_id) REFERENCES Breweries (brew_id);
ALTER TABLE Favorites ADD CONSTRAINT FK_FavBeer_Beer FOREIGN KEY (fk_beer_id) REFERENCES Beers (beer_id);
And here's the first part:
SELECT * FROM Favorites JOIN Beers ON Favorites.fk_beer_id = Beers.beer_id
I need to mix in the brew_name, but haven't been able to get it right. When I try to join Breweries (ON Favorites.fk_brew_id = Breweries.brew_id) i get an error saying "Unknown column 'Favorites.fk_brew_id' in 'on clause'"
Hope you guys can help me out :)

There is no fk_brew_id in the Favorite table, but you have to add the JOIN condition to the Beers table not to the Favorite table like this:
SELECT
bw.brew_name,
b.beer_name,
f.fav_comment,
...
FROM Favorites AS f
INNER JOIN Beers AS b ON f.fk_beer_id = b.beer_id
INNER JOIN Breweries AS bw ON b.fk_brew_id = bw.brew_id;

SELECT *
FROM Favorites
JOIN Beers
ON Favorites.fk_beer_id = Beers.beer_id
JOIN Breweries
ON Beers.fk_brew_id = Breweries.brew_id
Of course is real life you shoudl never use select *. When you have a join you are repeating columns and causing your query to be slower.

Related

How to make this join between multiple tables in mysql work

I have three tables, patient_set, user and map_patient_set_user. I'm trying to do an inner join between patient_set and user and also patient_set and map_patient_set_user in a single query. The query should return all the patient_sets for which a given user is the creator or care_coordinator. But patient sets can also be returned if they have the required permission (specified in map_patient_set_user). The following query ends up returning data from only the map_patient_set_user and ignores the join between patient_set and user. What should be the correct way to implement this join.
//Query
SELECT *
FROM (
patient_set AS a
JOIN user AS b ON a.creator_id = b.user_id OR a.care_coordinator = b.user_id
JOIN map_patient_set_user AS m ON a.patient_set_id = m.patient_set_id
)
WHERE
b.user_id = UUID_TO_BIN_F(:id)
OR
m.permission = 'view_all'
ORDER BY created DESC;
//Tables
CREATE TABLE user (
user_id BINARY(16) PRIMARY KEY NOT NULL,
first_name VARCHAR(55) NOT NULL,
last_name VARCHAR(25) NOT NULL,
org_id INT UNSIGNED,
FOREIGN KEY (org_id) REFERENCES organization(org_id),
lru_patient_set_id BINARY(16)
);
CREATE TABLE patient_set (
patient_set_id BINARY(16) PRIMARY KEY NOT NULL,
creator_id BINARY(16) NOT NULL,
FOREIGN KEY (creator_id) REFERENCES user(user_id),
created DATETIME NOT NULL,
last_updated DATETIME,
care_coordinator BINARY(16),
FOREIGN KEY (care_coordinator) REFERENCES user(user_id),
num_patient_updates INT NOT NULL,
name VARCHAR(100)
);
CREATE TABLE map_patient_set_user (
patient_set_id BINARY(16),
user_id BINARY(16),
permission ENUM('view_no_personal', 'view_all', 'edit_all'),
FOREIGN KEY(patient_set_id) REFERENCES patient_set(patient_set_id),
FOREIGN KEY(user_id) REFERENCES user(user_id)
);

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)
);

Query gets data from two different users when I try to get data just from just one user

I want to get data just from only one specific user but I get data from both users. Why is that? I don't understand. How can I solve this?.
I have three tables:
/*User*/
CREATE TABLE `User` (
`IDUser` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`IDUser`)
);
/*Category*/
CREATE TABLE `Category` (
`IDCategory` CHAR(3) NOT NULL,
`FK_User` INT NOT NULL,
`CategoryName` VARCHAR(40) NOT NULL,
PRIMARY KEY (`IDCategory`, `FK_User`)
);
/*Product*/
CREATE TABLE `Product` (
`IDProduct` VARCHAR(18) NOT NULL,
`FK_User` INT NOT NULL,
`ProductName` VARCHAR(150) NOT NULL,
`FK_Category` CHAR(3) NOT NULL,
PRIMARY KEY (`IDProduct`, `FK_User`)
);
ALTER TABLE `Product` ADD FOREIGN KEY (`FK_User`) REFERENCES `User`(`IDUser`);
ALTER TABLE `Product` ADD FOREIGN KEY (`FK_Category`) REFERENCES `Category`(`IDCategory`);
ALTER TABLE `Category` ADD FOREIGN KEY (`FK_User`) REFERENCES `User`(`IDUser`);
insert into User(Name) values('User1');
insert into User(Name) values('User2');
insert into Category(IDCategory,FK_User,CategoryName) values('CT1',1,'Category1User1');
insert into Category(IDCategory,FK_User,CategoryName) values('CT1',2,'Category1User2');
If two different users insert both the same product with the same ID:
insert into Product values('001',1,'shoe','CT1');
insert into Product values('001',2,'shoe','CT1');
Why do I keep getting data from both users if I try a query like this one:
SELECT P.IDProduct,P.ProductName,P.FK_Category,C.CategoryName
FROM Product P inner join Category C on P.FK_Category=C.IDCategory
WHERE P.FK_User=1
this is the result I get:
You are getting two rows because both categories have the same IDCategory value which is the value you are JOINing on. You need to also JOIN on the FK_User values so that you don't also get User2's category values:
SELECT P.IDProduct,P.ProductName,P.FK_Category,C.CategoryName
FROM Product P
INNER JOIN Category C ON P.FK_Category=C.IDCategory AND P.FK_User = C.FK_User
WHERE P.FK_User=1
You need to add p.FK_User=C.Fk_User this condition in your join clause
SELECT P.IDProduct,P.ProductName,P.FK_Category,C.CategoryName
FROM Product P inner join Category C
on P.FK_Category=C.IDCategory and p.FK_User=C.Fk_User
WHERE P.FK_User=1
A PRIMARY KEY is a UNIQUE key. Shouldn't CategoryID be unique? That is, shouldn't Category have PRIMARY KEY(CategoryId)?
(Check other tables for a similar problem.)

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