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
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 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'
My database structure is the following:
Table RESERVATION
ID
responsible_person
number_of_persons
Table SIZE
ID
size
Many-to-many Table RESERVATIONS_HAS_SIZE
RESERVATION_ID
SIZE_ID
A person can register for example 3 persons. You have to choose the sizes of these 3 peoples (S, M or L).
My query looks like this:
SELECT * FROM RESERVATION
INNER JOIN RESERVATIONS_HAS_SIZE
ON RESERVATION.ID = RESERVATIONS_HAS_SIZE.RESERVATION_ID
INNER JOIN SIZES
ON RESERVATIONS_HAS_SIZE.SIZE_ID = SIZE.ID
The problem is now I'm getting 3 records back. I only want one record back and for example one field with sizes: S, S, L.
Is this possible? And if so, how can I do this?
You can use GROUP_CONCAT():
SELECT GROUP_CONCAT(s.Size SEPARATOR ', ')
FROM RESERVATION r INNER JOIN
RESERVATIONS_HAS_SIZE rhs
ON r.ID = rhs.RESERVATION_ID INNER JOIN
SIZES s
ON rhs.SIZE_ID = s.ID;
I have three tables Guardian, Student and StudentsGuardian. Table information is as under
Guardian:
id(pk)
Student:
id(pk)
name
address
StudentsGuardian:
student_id(fk)
guardian_id(fk)
I want to select those students whose guardian_id=2(suppose). Actually these are relational tables so i am unable to think a way to accomplish it. If i apply join it would return a joint table but i need only the information of those students having guardian_id= specific id.
It could be a basic question but i am stuck in it. Thanks
Use below query:
SELECT s.id, s.name, s.address
FROM Student s
INNER JOIN StudentsGuardian sg ON s.id = sg.student_id
WHERE sg.guardian_id = 'somespecific_id'
SELECT
*
FROM Guardian
INNER JOIN StudentsGuardian ON StudentsGuardian.guardian_id = Guardian.id
INNER JOIN Student ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
SELECT Student.name, Student.address
FROM Student JOIN StudentsGuardian ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
That should do.
I'm trying to join these 4 tables:
customers:
id_customer;
name;
surname
employers:
id_employer;
id_customer;
products_to_employer
id_product;
id_employer;
products
id_product;
name_product;
code;
price;
Where i basically try to get all products of a customer. Basically each product is related whit a id_employer, but each employer is related whit a customer, so what i want is to bring all the products that is not related whit the employer but whit his "parent" the customer.
The result I want to get is:
Result:
Example: variable $id_customer: 4
products result:
id_customer (id nr 4)
name_product;
name_product;
code;
price;
This is the requirement:
Where i basically try to get all products of a customer
So it is just a matter of applying some joins and a where clause for the filter on id_customer
select p.* from products p
join products_to_employer pe on p.id_product = pe.id_product
join employers e on pe.id_employer = e.id_employer
join customers c on e.id_customer = c.id_customer
where c.id_customer = 4
NOTE: There shouldn't be any need of adding the id_customer to the results as you are already filtering by it.
select c.id_customer, c.name, p.name_product, p.code, p.price
from products p
inner join products_to_employer pte on (pte.id_product = p.id_product)
inner join employers e on (e.id_employer = pte.id_employer)
inner join customers c on (c.id_customer = e.id_customer)
where c.id_customer = 4