How to join the same table multiple times with 3 other tables? - mysql

I have 4 tables 'match', 'location', 'team' and 'round'. Which have the following structure:
match: location_id, team_one, team_two, round_id
location: location_id, location_name, referee_name
team: team_id, team_name
round: round_id, round_name
What I'm trying to do is get an overview of every match on all locations in a certain round, so the output would look like this:
team_one_id, team_one_name, team_two_id, team_two_name, round_id,
location_name, referee
What I tried is:
SELECT * FROM `match`
INNER JOIN `team` AS `team_one` ON `match`.`team_one` = `team_one`.`team_id`
INNER JOIN `team` AS `team_two` ON `match`.`team_two` = `team_two`.`team_id`
INNER JOIN `location` ON `match`.`location_id` = `location`.`location_id`
This kind of works but it only gives one team: the team called last (so in this case team two). I don't get why it doesn't work because I used aliasing.
How could I join these tables in the way described above?

Short answer: it should work, but sometimes MySQL has a problem with columns with the same name for reasons unknown to me (I've seen this behavior before though). The easiest remedy would be to use the complete alias.column in the query:
SELECT
team_one.team_id AS team_one_id,
team_one.team_name AS team_one_name,
team_two.team_id AS team_two_id,
team_two.team_name AS team_two_name,
round_id,
location_name,
referee_name AS referee
FROM
`match`
INNER JOIN
`team` AS `team_one` ON `match`.`team_one` = `team_one`.`team_id`
INNER JOIN
`team` AS `team_two` ON `match`.`team_two` = `team_two`.`team_id`
INNER JOIN
`location` ON `match`.`location_id` = `location`.`location_id`;

Related

Select all rows with multiple possible values

I am trying to get all fields from the product table (*) that have the following set of sub_property:
subprop_name=X
subprop_value=Y
I have the following tables :
https://imgur.com/a/y4LGqMI (couldn't upload the picture because the format was not accepted)
So, for an example, if I have two products which has in their sub_property table a entry like this:
subprop_name=X
subprop_value=Y
I would like to return it. As described by the schema, a product can have multiple sub_property entries!
So far, this is what I have:
SELECT prod_id,prod_name from product WHERE product.prod_id IN
(
SELECT property.product_prod_id FROM property WHERE property.prop_id IN
(
SELECT property_prop_id from sub_property WHERE
(
sub_property.subprop_name="Type de scanner" AND sub_property.subprop_value="par transparence"
)
OR
(
sub_property.subprop_name="Pages/minute maximum" AND subprop_value="8.5 pages"
)
)
)
But obviously, it doesn't work because of the 'OR'.
It returns me all items that have one of the set of sub_property instead of all the products that have all the sets of sub_property.
DATABASE HERE
Using JOIN's and an IN for the tupples could be a simple solution for this.
SELECT p.prod_id, p.prod_name
FROM product p
JOIN property AS prop
ON prop.product_prod_id = p.prod_id
JOIN sub_property AS subprop
ON subprop.property_prop_id = prop.prop_id
WHERE (subprop.subprop_name, subprop.subprop_value) IN (
('Type de scanner', 'par transparence'),
('Pages/minute maximum', '8.5 pages')
)
GROUP BY p.prod_id, p.prod_name
So... I am not sure if it's the correct way to validate two answers, but I got 2 working answers.
This is the first one from #Steff Mächtel using LEFT JOIN (https://stackoverflow.com/a/53915792/5454875)
And the second one is from #Shidersz (see comments below the original question), using INNER JOIN)
"I used and approach using INNER JOIN, is something like this what you need? db-fiddle.com/f/t6RrnhDPQuEamjf2bTxFeX/5"
EDIT
#Shidersz solution doesn't work because it selects all products who as at least one of the condition, wich I don't want. I want the product to have all the conditions.
UPDATE 2:
I would suggest to use LEFT JOIN for each property and each sub_property and then check the value inside WHERE condition:
SELECT product.prod_id, product.prod_name
FROM product
LEFT JOIN property AS property_a ON property_a.product_prod_id = product.prod_id AND
property_a.prop_name = "PROPERTY_GROUP_3"
LEFT JOIN sub_property AS sub_property_a ON sub_property_a.property_prop_id = property_a.prop_id AND
sub_property_a.subprop_name="property_4"
LEFT JOIN property AS property_b ON property_b.product_prod_id = product.prod_id AND
property_b.prop_name = "PROPERTY_GROUP_4"
LEFT JOIN sub_property AS sub_property_b ON sub_property_b.property_prop_id = property_b.prop_id AND
sub_property_b.subprop_name="property_3"
WHERE sub_property_a.subprop_value="value_of_property_4" AND
sub_property_b.subprop_value="value_of_property_3"
GROUP BY product.prod_id
Example: https://www.db-fiddle.com/f/wk344Gt6hm98xEhM4jei92/6
Example with 2 new "KEYS" (Index) for better performance:
ALTER TABLE `property`
... ADD KEY `prop_name` (`prop_name`);
ALTER TABLE `sub_property`
... ADD KEY `subprop_name` (`subprop_name`);
https://www.db-fiddle.com/f/wk344Gt6hm98xEhM4jei92/7
Example with INNER JOIN instead of LEFT JOIN
I see no difference with EXPLAIN on test data, maybe Mysql optimizer handles this internal equal
SELECT product.prod_id, product.prod_name
FROM product
INNER JOIN property AS property_a ON property_a.product_prod_id = product.prod_id AND
property_a.prop_name = "PROPERTY_GROUP_3"
INNER JOIN sub_property AS sub_property_a ON sub_property_a.property_prop_id = property_a.prop_id AND
sub_property_a.subprop_name="property_4" AND
sub_property_a.subprop_value="value_of_property_4"
INNER JOIN property AS property_b ON property_b.product_prod_id = product.prod_id AND
property_b.prop_name = "PROPERTY_GROUP_4"
INNER JOIN sub_property AS sub_property_b ON sub_property_b.property_prop_id = property_b.prop_id AND
sub_property_b.subprop_name="property_3" AND
sub_property_b.subprop_value="value_of_property_3"
GROUP BY product.prod_id
https://www.db-fiddle.com/f/wk344Gt6hm98xEhM4jei92/8

SQL column name ambiguous

I am having trouble with a new IMDB like system I'm building. My specific issue is that when I run:
CREATE VIEW `directors` AS
SELECT
`stars`.`id` AS `movie_id`,
`stars`.`title`,`stars`.`rating`,
`stars`.`storyline`,
`stars`.`star`,
`people_list`.`name` AS `director`
FROM `stars`
INNER JOIN `stars`
ON `movie_directors`.`movie` = `stars`.`id`
INNER JOIN `people_list`
ON `movie_directors`.`director` = `people_list`.`id`
WHERE `movie_directors`.`enabled` = 1;
I get the following error:
#1052 - Column 'stars.id' in field list is ambiguous
All of the questions I've found on here seem to relate to when you don't prefix the column name with a table name or, in this case, a view name since I'm writing a view to build off another view
You are selecting from stars and then INNER JOINing on stars:
SELECT ... FROM stars INNER JOIN stars
I think that you probably want to join with movie_directors based on your query.
You are using a self join (starts table is used two time) in this case you need an alias for refer the proper table instance
CREATE VIEW `directors` AS
SELECT
`stars`.`id` AS `movie_id`
, `stars`.`title`
,`stars`.`rating`
, `stars`.`storyline`
, `stars`.`star`
, `people_list`.`name` AS `director`
FROM `stars`
INNER JOIN `stars` as s2 ON `movie_directors`.`movie` = s2.`id`
INNER JOIN `people_list` ON `movie_directors`.`director` = `people_list`.`id`
WHERE `movie_directors`.`enabled` = 1;
I think it's because you are not aliasing correctly.
It should be something like
select ...
from stars
inner join stars as anothername
It looks to be ambiguous because you have two references to the stars table. Your FROM clause and your first INNER JOIN.
It looks like you are intending to join on movie_directors instead of INNER JOIN stars clause. E.g.
CREATE VIEW `directors` AS SELECT
`stars`.`id` AS `movie_id`,
`stars`.`title`,`stars`.`rating`, `stars`.`storyline`, `stars`.`star`, `people_list`.`name` AS `director`
FROM `stars`
INNER JOIN `movie_directors` ON `movie_directors`.`movie` = `stars`.`id`
INNER JOIN `people_list` ON `movie_directors`.`director` = `people_list`.`id` WHERE `movie_directors`.`enabled` = 1;
Hope this helps!

Full outer join in mysql with movie database

Hi I have the following tables and columns.
movie: ID, title
person: ID, name
involved: personID, movieID
I need to answer the question:
"Which movies have either John Travolta or Uma Thurman, but not both starred in?"
I couldn't figure out how to do this without creating new tables, so I made 2 new tables. And tried to do the full outer join on, where you dont get intersecting results. I found out that you can't do full outer joins in mysql but had to do a left join, unioned with a right join. I tried this but don't get the results I wanted at all. I have been stuck for a while now. Can anyone point me in the right direction?
This is what I have so far.
DROP TABLE IF EXISTS Umatable;
DROP TABLE IF EXISTS Johntable;
CREATE TABLE Umatable(title VARCHAR(500));
CREATE TABLE Johntable(title VARCHAR(500));
INSERT INTO Umatable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "Uma Thurman"
AND p.id = i.personid
AND m.id = i.movieiD;
INSERT INTO Johntable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "John Travolta"
AND p.id = i.personid
AND m.id = i.movieiD;
SELECT *
FROM Umatable
LEFT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
UNION
SELECT *
FROM Umatable
RIGHT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
I would do this using aggregation and having:
select i.movieId
from involved i join
person p
on p.id = i.personId
group by i.movieId
having sum(p.name in ('John Travolta', 'Uma Thurman')) = 1;
A count(*) inside a correlated subquery will work:
select *
from movie m
where 1 = (select count(*)
from involved i
join person p
on p.ID = i.personID
and p.name IN ('John Travolta', 'Uma Thurman')
where i.movieID = m.ID)
SQLFiddle Demo

Inner Join from multiple tables

I have 3 tables, as follows:
Patron
======
patron_num
Booking_For_Schedule
====================
tname
date
time
booking_num
Booking_By_Patron
=================
booking_num
patron_num
I would like to retrieve a result with columns patron_num, date, time, and tname, like so:
patron_num date time tname
1 2013-11-03 20:00 TestName
...etc
The purpose of this homework question is to teach us INNER JOINS, but I am having some difficulty. Could some kind SO user push me in the right direction?
Here's my SQL:
SELECT `patron_num`,`date`,`time`,`tname`
FROM `booking_for_schedule` `F`
INNER JOIN `booking_by_patron` `B` on `F`.`booking_num` = `B`.`booking_num`
INNER JOIN `patron` `P` on `B`.`patron_num`=`P`.`patron_num`
which returns the error: #1052 - Column 'patron_num' in field list is ambiguous
SELECT `P`.`patron_num`,`date`,`time`,`tname`
FROM `booking_for_schedule` `F`
INNER JOIN `booking_by_patron` `B` on `F`.`booking_num` = `B`.`booking_num`
INNER JOIN `patron` `P` on `B`.`patron_num`=`P`.`patron_num`
Will help with the ambiguity.
Since patron_num is in multiple tables you need to specify which one you want to use. You can do this by adding the table alias before it like you are in your joins. Example:
SELECT `B`.`patron_num`,`date`,`time`,`tname`
FROM `booking_for_schedule` `F`
INNER JOIN `booking_by_patron` `B` on `F`.`booking_num` = `B`.`booking_num`
INNER JOIN `patron` `P` on `B`.`patron_num`=`P`.`patron_num`

Help with a fairly simple MySQL Query

I am trying to pull up some data.
Here is the setup:
A [school] can have multiple [semester]. Only one [semester] can be active per [school].
The simplified fields per table:
[school] has 'id','title'
[school_semester] has 'id','school_id' (fk), 'semester_id' (fk), 'active', 'start_date', 'end_date'
[semester] has 'id', 'title'
The "school_semester" table holds information for it's specific school/semester. (startdate,enddate,etc).
Anyway, I am simply trying to pull up all schools, and in this query I want to also see that school's current active semester.
Here is my query so far:
SELECT *, `school`.`name` as school_name
FROM (`school`)
LEFT JOIN `school_semester` ON `school`.`id` = `school_semester`.`school_id`
LEFT JOIN `semester` ON `semester`.`id` = `school_semester`.`semester_id`
ORDER BY `school_semester`.`active`
The problem:
This works if a school has semester that is active, but if it does not, it will show one which may be marked as inactive. If I add the statement WHERE school_semester.active = 1, it excludes schools that do not have an active semester..
Any pointers?
move the condition on the joining :
SELECT *, `school`.`name` as school_name
FROM (`school`)
LEFT JOIN `school_semester` ON
`school`.`id` = `school_semester`.`school_id` AND
`school_semester`.`active`
LEFT JOIN `semester` ON `semester`.`id` = `school_semester`.`semester_id`
This way, only the active semesters will be joined to the result.
Add active=1 as part of your join condition.
SELECT *, `school`.`name` as school_name
FROM (`school`)
LEFT JOIN `school_semester` ON `school`.`id` = `school_semester`.`school_id`
AND `school_semester`.`active` = 1
LEFT JOIN `semester` ON `semester`.`id` = `school_semester`.`semester_id`
ORDER BY `school_semester`.`active`