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!
Related
The query is meant to return the percentage based on value from one table being divided by the value of another table. However, there is something wrong and I am missing it.
similar problems noted on the board looked related to JOIN, but did not seem to be the problem, when I tried and explicit join -- basically mysql was like -- now you are an idiot-- I must have did that wrong or that is not the problem.
SELECT (pathogenPop / locationpop) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as pathogenPop, apinfectcount.APInfectCountLocation
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as locationpop, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on apinfectcount.APInfectCountLocation = apcountrypop.apcountrypopCountry
and apinfectcount.APInfectCountWeek = 23);
Table Schema: apcountrypop
idapcountrypop INT(11)
apcountrypopCountry VarChar(45)
apcountrypopPopulation FLOAT
Table Schema: apinfectcount
idAPInfectCount INT(11)
APInfectCountLocation VarChar(45)
APInfectCountOutBreak VarChar(45)
APInfectCountPathogen VarChar(45)
APInfectCountInfected FLOAT
APInfectCountDead FLOAT
APInfectCountWeek VarChar(45)
If it worked --
it would assign apinfectcount.APInfectCountInfected to pathogenPop
and apcountrypop.apcountrypopPopulation to locationpop
for the values where the locations are the same(apinfectcount.APInfectCountLocation = apcountrypop.apcountrypopCountry)
then it would return the value of the apinfectcount table value is divided by the apcountrypop table to give the percentage.
so in this specific example I only have sample data so I am just wanted to return one value so I added the where clause to just test the logic and syntax.
I appreciate the help.
You have assugned the tables alias pathogenPop and locationpop so
you need pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry
and pathogenPop.APInfectCountWeek = 23 in ON clause
SELECT (pathogenPop / locationpop) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as pathogenPop, apinfectcount.APInfectCountLocation
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as locationpop, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry
and pathogenPop.APInfectCountWeek = 23) T;
and also a table alias for the outer FROM(..) T
I don't have the database to test against so I'm not 100% certain this will run, but would the following query not be a bit simpler?
SELECT (apinfectcount.APInfectCountInfected / apcountrypop.apcountrypopPopulation) as PercentInfected, apinfectcount.APInfectCountLocation
FROM apinfectcount
INNER JOIN apcountrypop ON apcountrypop.apcountrypopCountry = apinfectcount.APInfectCountLocation
WHERE apinfectcount.APInfectCountWeek = 23
GROUP BY apinfectcount.APInfectCountLocation
And I assume there is only one location record per location in each table?
There is an issue within a query. As scope of apinfectcount.APInfectCountLocation column and apcountrypop.apcountrypopCountry column is limited to subquery only you cannot use it outside the subquery (within where clause).
You can check out these docs on subquery https://learn.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-2017
Refer code below.
SELECT (countInfected / countrypopulation) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as countinfected, apinfectcount.APInfectCountLocation, APInfectCountWeek as
countweek
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as countrypopulation, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.countinfected = locationpop.countrypopulation
and pathogenPop.countweek= 23);
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
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`;
I have
TABLE 1: r_profile_token
Columns:
r_sequence int(45) AI PK
r_profileid varchar(45)
r_token varchar(300)
r_deviceType int(1)
r_date date
r_time time
and
TABLE 2: r_token_arn
Columns:
r_token varchar(300) PK
r_arn varchar(300)
I need a result of the form -
r_profileid
r_arn
r_deviceType
where I can specify the r_profileid.
So far my SQL statement is:
SELECT
b.r_arn,
a.r_deviceType
FROM
coffee2.r_profile_token a INNER JOIN
coffee2.r_token_arn b
ON a.r_token=b.r_token;
Which returns r_arn and r_deviceType but for all r_profileid?
How do I modify the statement so that it returns me r_arn and r_deviceType only corresponding to a specific r_profileid?
Use a WHERE clause.
SELECT B.R_ARN, A.R_DEVICETYPE
FROM COFFEE2.R_PROFILE_TOKEN A
INNER JOIN
COFFEE2.R_TOKEN_ARN B
ON A.R_TOKEN=B.R_TOKEN
WHERE R_PROFILEID = 'SOME_VALUE';
If you want for a single profileid, then use
WHERE R_PROFILEID = 'SOME_VALUE';
If you want for a range of profileIds , then use
WHERE R_PROFILE_ID IN ('VALUE1','VALUE2','VALUE3');
You can try this Query against your requirements.
SELECT
b.r_arn,
a.r_deviceType ,
a.r_profileid
FROM
r_profile_token a
INNER JOIN
r_token_arn b
ON
a.r_token=b.r_token
where r_profileid='profile name';
You need to put a where condition in your MYSql query.
select b.r_arn, a.r_deviceType from coffee2.r_profile_token a
INNER JOIN coffee2.r_token_arn b on a.r_token=b.r_token
where r_profileid = "Specific value";
select b.r_arn, a.r_deviceType, a.r_profileid from r_profile_token a
INNER JOIN r_token_arn b on
a.r_token=b.r_token;
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`