I have 3 tables, 'film', 'actor' and 'cast'. I want to get a list of actors that are in a certain film, but I am brand new to SQL and cannot figure out how to join the tables.
I am comfortable with the SELECT and WHERE, just not the FROM.
Film has the following fields: id, title and year.
Actor has the fields id and name.
Cast has the fields filmid and actorid
You need to join the tables like this
select a.name
from actor a
join cast c on a.id = c.actorid
join film f on f.id = c.filmid
where f.title = 'your movie title'
Related
Suppose that we have four tables in the following structure (Table name - fields):
person - id, name
doctor - id_person, specialty
pacient - id_person, disease
appointment - doctor_id, pacient_id, date
How can I construct a query to return the doctor's name and specialty, the pacient's name and disease and the appointment date?
Here's where I've got so far:
SELECT person.name, doctor.specialty, pacient.disease, appointment.date
FROM appointment
INNER JOIN person
ON appointment.pacient_id=person.id
INNER JOIN doctor
ON appointment.doctor_id=doctor.id_person
INNER JOIN pacient
ON appointment.pacient_id=pacient.id_person
But this is not returning the right fields. I think the problem resides in returning the same field (person.name) for two different ids (doctor and pacient) in the same row.
You need to do two separate joins to the person table, and use aliases to identify the individual tables like so:
select
dp.name as DoctorName
, doctor.specialty
, pp.name as PacientName
, pacient.disease
, appointment.date
from appointment
inner join doctor
on appointment.doctor_id = doctor.id_person
inner join person dp
on appointment.doctor_id = dp.id
inner join pacient
on appointment.pacient_id = pacient.id_person
inner join person pp
on appointment.pacient_id = pp.id
I have the following four tables:
Subscription:
book_id, duration, subscriber
Book:
book_id, book_name
Genre:
genre_id, genre_name
book_to_genre:
genre_id, book_id
For a certain user (subscriber) I would like to get all rows from subscription. The book name should be fetched from table Book. I know this is done with an inner join:
SELECT Book.book_name, Subscription.duration from Subscription INNER JOIN Book on Subscription.book_id = Book.book_id where Subscription.subscriber = "somevalue";
What if I would like to fetch genre_name from table Genre, where Genre.genre_id = book_to_genre.genre_id for that book?
Here is a modified version of your initial query that will return genre_name:
- I added the field genre_name in the SELECT part
- I added table aliases to make it easier to read
- I added 2 INNER JOIN: one between tables book and book_to_genre, the other between tables book_to_genre and Genre
SELECT b.book_name, s.duration, g.genre_name
FROM Subscription s
INNER JOIN Book b on s.book_id = b.book_id
INNER JOIN book_to_genre bg ON b.book_id = bg.book_id
INNER JOIN Genre g ON bg.genre_id = g.genre_id
where s.subscriber = "somevalue";
Documentation:
- SELECT
- JOIN
I have a three table setup in my database: members, clases, classregistration.
The class registration table has m_id and c_id columns that store the member id and class id. For example if the same member registers with more classes it will be rows of same m_id with different c_id.
I am trying to view all members that are with a particular class and I have this query but it doesn't return anything.
Any suggestions what is wrong in my query?
Thanks
SELECT members.member_first_name, classregistration.c_id
FROM classregistration INNER JOIN clases
ON classregistration.c_id = clases.class_id
WHERE classregistration.c_id = 1
You can give this a try:
SELECT m.member_first_name, cr.c_id
FROM classregistration cr
INNER JOIN clases c ON cr.c_id = c.class_id
INNER JOIN members m ON m.member_id = cr.m_id
WHERE cr.c_id = 1
I am working with MySql 5.1 and am building my first many-to-many database. I understand the concepts, I have 3 tables:
Albums, with a unique ID
Genres, with a unique ID
album_genres with columns for each of the ID's from Albums and Genres.
The issue I am having is that, of course, 1 album can have multiple genres. But when I do a search, I'm really in the dark about how to structure it so during searches, I get all the genres for each individual album. Please note, this is not the loading of 1 album, but doing a search that will net 1 or more albums.
Sorry I don't really have anything to show what I've tried because I'm not even sure where to begin.
I'm sure it's easy enough. But all the tutorials I could find only address the basics of M2M but not how to get multiple matching entries.
After looking at many great suggestions, I have built this query:
SELECT
album.album_title,
Concat(genre.genre_id, ',') as GenreName,
count(album.album_id) as GenreCount
FROM $this->album_tbl album
JOIN wp_musicmgr_albgnr albgnr ON albgnr.albgnr_album_fk = album.album_id
JOIN $this->genre_tbl genre ON genre.genre_id = albgnr.albgnr_genre_fk
GROUP BY album.album_id
Which is producing this:
[0] => stdClass Object
(
[album_title] => album q
[GenreName] => 1,
[GenreCount] => 3
)
However, as you can see. Despite having a count of 3 hits on genres, it is listing the first one.
If I got the question you need output like
AlbumName | Genre1,Genre2.........
FOr this you need to use GroupBy
SELECT A.album_name as AlbumName, GROUP_CONCAT(G.genre_name) as GenreName, count(A.ID) as GenreCount
FROM Album A
JOIN album_genres AG
ON (A.ID = AG.album_ID)
JOIN Genre G
ON (G.ID = AG.genre_ID)
Group by A.ID
Join on the two tables.
SELECT cols FROM Albums
JOIN album_genres USING (albumID)
JOIN Genres USING (genreID)
WHERE albumName LIKE :search
Select * from Albums a, Genres g, album_genres ag
where a.unique_id = ag.a_unique_id and g.unique_id = ag.g_unique_id and a.name like '%album-name%'
album-name is the value of album in table album
Also in place of star(*) in Select *, please do put columns name's from Gener table.
To get all Genres of an Album the query would be
SELECT A.album_name, G.genre_name FROM Album A
JOIN album_genres R
ON (A.ID = R.album_ID)
JOIN Genre G
ON (G.ID = R.genre_ID)
WHERE A.ID = '5'
Where 5 would be the album unique id. The basic concept is that you should join the 3 tables, and use JOIN - ON to match the ids of each columns in the relationship table to the entity table one
EDIT: If you want a list of every album with every genre like this one:
Album - Genre
Al01 - Rock
Al02 - Rock
Al01 - Rock
Al01 - Metal
Al03 - Metal
just remove the WHERE A.ID = '5'.
Updated (New Answer):
Note: Tested.
select a.name, group_concat(g.name separator ', ')
from albums a
inner join album_genre ag on a.id = ag.albumid
inner join genres g on ag.genreid = g.id
group by a.id;
Note: GROUP_CONCAT has a maximum length limit. Default limit is 1024, but can changed by modifying group_concat_max_len. Read more: http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
Old answer:
Search for Albums:
SELECT a.album_name, a.AlbumId FROM `Albums` a
INNER JOIN `album_genres` ag on a.AlbumId = ag.AlbumId
INNER JOIN `Genres` g on ag.GenreId = g.GenreId
WHERE
a.album_name = 'Michael Learns To Dance' AND
g.genre_name = 'ROCK'
GROUP BY a.AlbumId;
Foreach row... perform another search query:
SELECT g.genre_name FROM `Genres` g
INNER JOIN `album_genres` ag on g.GenreId = ag.GenreId
WHERE ag.AlbumId = 1;
....[continue for next row]....
....
WHERE ag.AlbumId = 2;
WHERE ag.AlbumId = 3;
WHERE ag.AlbumId = 4;
......
WHERE ag.AlbumId = 1000;
I have various tables like
Student
primary id , students name, course
Papers
paper id, papername, course, semester, type
StudentOptions
primary id, studentid (foreign key - reference student id) and paperid (foreign key - references paper id)
StudentsTerm
studentid (foreign key- references student id) and student semester
Now the kind of result i want is,
I want to choose a course then the term, which will give me the number of papers/subject it has with their types (Mandatory/Optional) and with that i want to have the count of number of students studying those papers from all these tables.
I don't wanna create any view or stuff, Just a normal select query will do.
The query i am running is :
SELECT p_name,
p_id,
type,
Count(sps.studentid) AS counts
FROM students,
str,
papers
LEFT JOIN sps
ON sps.paperid = papers.p_id
WHERE sps.studentid = students.studentid
AND students.studentid = str.studentid
AND sps.studentid = str.studentid
AND str.semesterid = p_semid
AND str.sessionid = 12
AND students.course = c_id
AND c_id = 6
AND p_semid = 1
GROUP BY p_id
As better practice, if you are going to be using explicit JOIN syntax, then don't use implicit syntax. In the query you posted above, you select from papers, but you don't use it anywhere. Also, your column names are slightly ambiguous. If it's easier, alias each table using a single letter, or explicitly prefix the column names. If you're using an aggregate with GROUP BY, you cannot select columns which are not in the group.
Let's assume this is your ER diagram:
Let's first join all the tables:
SELECT a.id, a.name
FROM student a
JOIN str b ON b.student_id = a.id
JOIN sps c ON c.student_id = a.id
JOIN papers d ON d.id = c.paper_id
Now you wish to find the number of students studying papers from a specific course and type:
SELECT a.id, a.name
FROM student a
JOIN str b ON b.student_id = a.id
JOIN sps c ON c.student_id = a.id
JOIN papers d ON d.id = c.paper_id
WHERE b.semester = 12
AND d.course = 6
Because your attributes are ambigiuous, it is hard to tell what tables they are coming from. If you can set up the structure and sample data on SQL Fiddle, we could help you better.