Confused on SQL assignment - mysql

We are doing a database query in class. And it's using relational keys. I don't know how to get the query to run. Here is what is says.
For each movie, list its number and title, along with the number and name of the actors who appeared in it.
This is what I have, but it doesn't work
SELECT `Movie`,`Movie_ID`,`ActorNum` FROM `Movies`
Union
Select Actor.Fname, Actor.Lname FROM Actor
;

Im not sure what all the column names are but if ActroName would be the actors name would this be what you are looking for?
SELECT Movies.Movie,
Movies.Movie_ID,
Movies.ActroNum,
Actor.ActroName
FROM Movies
JOIN Actor ON
Actor.ActroNum = Movies.ActroNum

Related

SQL query with distinct values

I have the two following schemes:
Movies[title, year, director, country, rating, genre, gross, producer]
and
Actors[title, year, characterName, actor]
Now I have the following exercise
Find character names that appeared in two movies produced in different countries.
My idea was the following which doesn't really work:
SELECT characterName
FROM Actors a
JOIN Movies m
ON a.title=m.title
AND a.year=m.year
WHERE COUNT(m.title)=2
AND COUNT(DISTINCT(m.country)=2
GROUP BY m.title;
My idea was to obviously select the characterName and join both tables on title and year because they are unique values in combination. Then my plan was to get the movies that are unique (by grouping them) and find the ones with a count of 2 since we want two movies. I hope that I am right till now.
Now I have my problems, because I don't really know how to evaluate if the movies played in two different locations.
I want to somehow make sure that they play in different countries.
You are on the right track. Here is a fixed version of your original query, that should get you the results that you expect:
select a.characterName
from actors a
inner join movies m
on m.title = a.title
and m.year = a.year
group by a.characterName
having count(distinct m.coutry) >= 2
Notes on your design:
it seems like you are using (title, year) as the primary key for the movies table. This does not look like a good design (what if two movies with the same title are produced the same year?). You would be better off with an identity column (in MySQL, an autoincremented primary key), that you would refer as a foreign key in the actors table
better yet, you would probably need to create a separate table to store the masterdata of the actors, and set up a junction table, say appearances, that represents which actors interpreted which character in which movie

Averaging a one-to-one field will summing a one to many in MySQL

I put together this example to help
http://sqlfiddle.com/#!9/51db24
The idea is I have 3 tables. One is the "root" table, in this case person which has a score attached to it. They then have some category I need to group by person_cat.cat and a one to many field called CS.
I would like to query for the average of the score, the sum of the one to many field person_co.co and group by the category.
SELECT
person_cat.cat,
person.id,
SUM(person_co.co),
AVG(person.cs)
FROM
person
LEFT JOIN person_co USING (id)
LEFT JOIN person_cat USING (id)
GROUP BY cat;
The issue I'm currently having is the average gets thrown off due to the join for the one to many. I can accomplish this with multiple queries, which is ok if that is the answer. However it would be nice to get this as one query.

How to show a data using JOIN in SQL (PHP)

Select maintable.name FROM maintable
JOIN genres genre1 USING (tmdb_id)
JOIN genres genre2 USING (tmdb_id)
WHERE genre1.genres_name = 'Action'
AND genre2.genres_name = 'Drama'
group by maintable.name
Here genres is table name. genres_name is column name. genres1 and genres2 are just nor a table name, nor a column name, they are just random name in the code.
This is my code, now How do i display all genres_name?
The genres is like:
tmdb_id genres_name
1 Action
1 Crime
1 Drama
2 Horror
2 Comedy
2 Drama
The main table isl ike
tmdb_id movie_title
1 The Dark Knight
2 Logan
3 Wonder Woman
Let me know, if you need more information. (Please do not ask to show, what i tried. Trust me, it will make the question more confusing)
I want to echo the genres like:
The Dark Knight - Drama, Action, Crime
Of course, you need to use group_concat:
Select maintable.movie_title, group_concat(genres.genres_name) AS genres_name
FROM maintable
JOIN genres USING (tmdb_id)
GROUP BY maintable.tmdb_id
HAVING find_in_set('Action', genres_name) AND find_in_set('Drama', genres_name)
See demo here.
Note: How does find_in_set works, please see official doc.
I would try something like this. But this is the best I can do guessing at it in my head... ( sorry for any mistakes )
$Sql = "SELECT
m.name,
GROUP_CONCAT( g.genres_name ) as genres_list
FROM
maintable AS m
JOIN
genres AS g USING (tmdb_id)
WHERE
g.genres_name IN('Drama', 'Action')
GROUP BY m.tmdb_id";
MySQL GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
Also note GROUP_CONCAT has a setting for the length, I don't recall what that is or how to change it, but it bit me in the butt one time. Basically it will truncate the list after a certain size, so be cautious of that.
See here: MySQL and GROUP_CONCAT() maximum length
AS I said I haven't tested this, but it seems you have a many to one relationship. Records in the maintable can have many related records in the genres table. Therefor, you should be able to group them on that relationship. Normally this would return 1 record for each pair ( same record in main table different in genre ) Without the group. The Group Concat allows you to compress that into a comma separated list.

MySQL Select statement - value name from other table

I have two tables. One has a list of baseball players associated with numbers representing their position on the field, and the other table has a list of numbers associated with the names of the field positions.
Using a select statement, how do I select the position number from the players table, and have it show the position name (converting the position number to the name)?
I'm new to MySQL, so please keep it simple and informative. Thank you!
Just join the two tables on the number column.
See MYSQL JOINS HERE: https://dev.mysql.com/doc/refman/5.0/en/join.html
Here is an example, since I don't know your actual schema.
SELECT pt.player_name as PLAYERNAME, fp.position_name as POSITION
FROM playersTable AS pt
INNER JOIN fieldPositions AS fp ON fp.position_number = pt.position_number
You can try like this
SELECT pf.position_name as position, p.position_number as playerName
FROM field_positions as pf, player as p
WHERE fp.position_number = p.position_number

Querying of same columns with different values

Im just new to MySql Programming and Im creating a project to enchance my skills.
I have a table called tblRecipe which has the columns RecipeId, RecipeName and Instructions. the other table is called tblIngredients which has the columns IngredientId,RecipeId, IngredientName.
Now for example,RecipeOne needs IngredientOne and IngredientTwo to make, RecipeTwo needs IngredientOne,IngredientTwo and IngredientThree to make.
My programs asks the IngredientNames for the input so when i put in IngredientOne and IngredientTwo it should give me back the result RecipeOne.
Here is my code
Select Recipename,Instructions
from tblREcipe where RecipeId in
(select recipeID
from tblIngredients
where Ingredientname in('IngredientOne','IngredientTWo')
);
I know that the IN operator is like saying match any of the following.
So with that code it gives me both recipes.
Im looking for an equivalent of AND.
I tried "Ingredientname =ALL(in('IngredientOne','IngredientTwo'))"
but it does not return any rows.
Im open to any changes such as restructuring the tables if that fixes the problem since this is just a practice project.Hope to hear from you soon guys and thank you in advance.
You also need to count the number of instance of the records that match to the number of ingredients. Like this one below:
Select a.Recipename,
a.Instructions
FROM tblREcipe a INNER JOIN
(
SELECT recipeID,
COUNT(IngredientId)
FROM tblIngredients
WHERE Ingredientname IN ('IngredientOne', 'IngredientTWo')
GROUP BY recipeID
HAVING COUNT(IngredientId) = 2 -- the number of ingredients
-- should somehow dynamic
) b ON a.recipeID = b.recipeID