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
Related
So I have a database with two tables, profile, and friends. The primary key in profile is an auto incremented int, and it is a foreign key in friends. The fields in the profile table are: id, name, age, bio, motto, email_address.
The fields in friends is: initiator_id, receiver_id, date_added.
So ultimately I am trying to make a query where I set the initiator_id and get a list of receiver_id's, and use those id numbers to get them from the profile table.
I've tried left join's, inner joins, and joins in general. Open to suggestions, and interpretations on what these types of joins are actually doing.
select friends.receiver_id, profile.name
from profile
inner join friends on friends.initiator_id=1;
I need the fields to return the receiver_id number as well as the corresponding name for that id number.
The specification is a bit unclear. Sample data and expected output would go a longs ways towards illustrating the requirements.
But my guess (and without a specification, its just a guess) is that we are after the resultset returned from this query:
SELECT f.receiver_id
, p.name
FROM friends f
JOIN profile p
ON p.id = f.receiver_id
WHERE f.initiator_id = 1
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.
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.
I have 2 tables:
matches TABLE
FIELDS: record, date, competition
outrights TABLE
FIELDS: record, competition
What I would like, is to select rows grouped by the different types of competition. Below are the statements that work fine when I treat each table seperately.
Firstly, from 'matches' and only if the date hasn't already past:
SELECT competition, date FROM matches WHERE date >= '$currentTime' GROUP BY competition
Followed by rows from 'outrights':
SELECT competition FROM outrights GROUP BY competition
This is all pretty straight forward, except the same competition values will often (but not always) appear in both tables. I have looked at many different methods (including LEFT and RIGHT JOINS), but haven't found a simple solution. Basically I want the different competition types that appear in both tables, without duplication. Is this possible?
Is this what you are looking for. A little confused by the question but it appears that you want a DISTINCT listing of the competition column from both tables
SELECT DISTINCT competition
FROM
(
SELECT competition FROM matches
UNION
SELECT competition from outrights
) AS t
If you need the distinct competitions that appear only in both tables and not just one or both you could use
SELECT DISTINCT competition
FROM
(
SELECT competition FROM matches INNER JOIN
outrights ON matches.competition = outrights.competition
) AS t
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