MySQL Query with three tables - mysql

I have got the following mySQL tables:
movies:
id,
title,
year
directors:
id,
name
directors_in_movies:
director_id,
movie_id
I would like to search for all the movies in which a director has been involved.
But I don't really know if I can combine three tables. I am quite new with databanks and I would be glad about some help how to write the SQL statement.

The exists operator should do the trick:
SELECT *
FROM movies m
WHERE EXISTS (SELECT *
FROM directors_in_movies dim
JOIN directors d ON dim.director_id = d.id
WHERE dim.movie_id = m.id AND
d.name = 'Some Director')

Related

Join with SELECT MySQL

I need a way to join a table with the results of a SELECT query, and I'm facing some difficulty to do that ...
There are 3 tables:
food_shops
id, name, slug
categories_food_shops
id, id_category, id_food_shop
categories
id, name, slug
How can I do to show a result with: food_shop.id, food_shop.name, categorie.slug
based on the categories_food_shops table? that contains a registry of the shop and their category?
so far I was able to do this, but it gets
Unknown column 'fs.id' in 'where clause'
SELECT fs.id, fs.slug, fs.name
FROM food_shops fs
JOIN
(
SELECT *
FROM categories_food_shops cfs
WHERE cfs.id_food_shop = fs.id
) AS cfs ON categories.id = cfs.id_catedory
Any help would be very appreciated, I'm new to this join statements
Try this:
SELECT
food_shops.id, food_shops.name, categories.slug
FROM food_shops
INNER JOIN categories_food_shops
ON food_shops.id = categories_food_shops.id_food_shop
INNER JOIN categories
ON categories_food_shops.id_category = categories.id
You can then use a WHERE statement at the end to display food shops with a specific id, or with a similar name, etc.

Select if another select returns rows

Im trying to make a select to make cooking recipe select within the items you have.
i have a table named ingredientsOwn with the following structure:
idType (int) amount (int)
Another table named recipes with this structure:
idRecipe (int) name (varchar)
And another table named recipeIngredients
idRecipe (int) idType (int) amount (int)
I would like to show the recipes you can do with the elements you have, how could i perform this?
Im trying to implement it in only one query cause i really dont know how to go throw and array on node js.
Thanks
The way I would go around this is, try to compute for each recipe, the number of ingredients you need, and join that with the number of ingredients own, and if the two numbers match, you have a candidate recipe.
So, to get the number of ingredients a recipe needs you'll have to do something like (this is more like a sql server syntax, so please try to focus in the concepts, and not the syntax):
select idRecipe, count(*) as neededIngredientsCount
from recipeIngredients
group by idRecipe
To get the number of available ingredients for each receipe, you have to join your ingredientsOwn with recipeIngredients, to be able to tell how many matching ingredients you have for each recipe.
select ingredientsOwn.idRecipe, count(*) as matchingIngredientsCount
from ingredientsOwn inner join recipeIngredients
on ingredientsOwn.idType = recipeIngredients.idType
where ingredientsOwn.amount >= recipeIngredients.amount
group by ingredientsOwn.idRecipe
Now you join the previous 2 queries to get the idRecieps that you have enough ingredients for, and join them with the recipes table to get the recipe name.
select r.idRecipe, r.name from
((select idRecipe, count(*) as neededIngredientsCount
from recipeIngredients
group by idRecipe) as in
inner join
(select ingredientsOwn.idRecipe, count(*) as matchingIngredientsCount
from ingredientsOwn inner join recipeIngredients
on ingredientsOwn.idType = recipeIngredients.idType
where ingredientsOwn.amount >= recipeIngredients.amount
group by ingredientsOwn.idRecipe) as io
on in.idRecipe = io.idRecipe
and in.neededIngredientsCount = io.matchingIngredientsCount
inner join
(select * from recipes) as r
on r.idRecipe = in.idRecipe)
Hope this helps, and sorry for not being able to provide valid mysql syntax.
SELECT * FROM recipes INNER JOIN (
select idRecipe from recipeIngredients
WHERE recipeIngredients.idType IN (
SELECT ingredientsOwn.idType from ingredientsOwn
)
) as a ON a.idRecipe = recipes.idRecipe

MySQL Selecting from one table while needing data from other

I have such tables:
How to, with just SQL select all the comments, for tracks with the genre of 'xxx'?
Well you could do that in two way first:
SELECT commmentId, content, date
FROM comments
WHERE trackId IN (SELECT trackId
FROM tracks
WHERE genreId = xxx);
Or:
SELECT c.commentId, c.content, c.date, t.genreId
FROM comments c
INNER JOIN tracks t
ON c.trackId = t.trackId
WHERE t.genreId = xxx;
And I didn't understand very well if you want to select comments by genreId or by genre name (if you want to do that by name than you should extend this a little bit) but this is the logic you should fallow...

How to fetch records using NOT syntax?

I have the next tables: faculty, faculty_subjects and subjects. Every faculty has a list of subjects
How can I SELECT subjects that are not needed for this faculty ?
Note: if user adds some new subject, then this subject doesn't already have a record in faculty_subjects table, but it should be displayed in SELECT.
So to sum up - from the list of ALL subjects that exists I want to know the ones that doesn't needed for this faculty. How this can be achieved ?
I'm using MySQL database.
PS. if there is a more better title - please edit.
You can LEFT JOIN to the Faculty_Subject, with the particulary Faculty ID you are interested in used in the join predicate, then filter out the rows with a match using the WHERE predicate, so you are left with the subjects you want:
SELECT s.id, s.Name
FROM Subject AS s
LEFT JOIN Faculty_Subject AS fs
ON fs.Subject_idSubject = s.ID
AND fs.Faculty_idFaculty = 1
WHERE fs.id IS NULL;
Note, I would usually advise to use NOT EXISTS syntax:
SELECT s.id, s.Name
FROM Subject AS s
WHERE NOT EXISTS
( SELECT 1
FROM Faculty_Subject AS fs
WHERE fs.Subject_idSubject = s.ID
AND fs.Faculty_idFaculty = 1
);
Which I beleive is more logical to the reader, but in MySQL LEFT JOIN/IS NULL performs better than NOT EXISTS
SELECT name FROM Subject WHERE id NOT IN
(SELECT FS.Subject_idSubject
FROM Faculty_Subjects AS FS
INNER JOIN Faculty AS F ON F.id=FS.Faculty_idFaculty
WHERE F.name='Physics' AND FS.Subject_idSubject IS NOT NULL)

MySQL select from multiple tables using pivot

I have a many-to-many relationship between two tables, and I am using a pivot table to manage it.
The structure is as follows:
Table 1: cars (car_id, ...)
Table 2: brands (brand_id, ...)
Pivot table: cars_brands (car_id, brand_id, units, discount)
Using only one query, I am trying to select every field of the cars of a certain brand PLUS the number of units and discount (both fields of the pivot table...)
My try:
SELECT c.*, cb.units, cb.discount
FROM (cars c, cars_brands cb)
INNER JOIN cb ON c.car_id = cb.car_id
WHERE cb.brand_id = 1
ORDER BY c.car_id asc
I am getting a
#1066 - Not unique table/alias: 'cb'
MySQL error.
Any help is much appreciated.
Thanks in advance.
I think what you want is:
SELECT c.*, cb.units. cb.discount
FROM cars AS c
JOIN car_brands as cb ON cb.car_id = c.car_id
JOIN brands AS b ON b.brand_id = cb.brand_id
WHERE b.brand_name = 'Audi'
ORDER BY c.car_id ASC
you can try this also:-
select c.*,cb.units,cb.discount from car c,brands b,cars_brands cb where c.car_id=cb.car_id and b.brand_id=cb.brand_id and cb.brand_id=8888 order by cb.car_id asc;