Optimize multiple join same table - mysql

I have two tables: student, address.
address includes id and location
student includes id, name, roommate_id, location_id.
I need to find student's info with student location and roommate location.
I have used the query:
SELECT
student.name as name, stulocation.location as address,
roommate.name as roommate_name, rmatelocation.location as roommate_address
FROM student
LEFT JOIN student as roommate ON student.roommate_id = roommate.id
LEFT JOIN location as stulocation ON student.location_id = stulocation.id
LEFT JOIN location as rmatelocation ON roommate.location_id = rmatelocation .id
I have joined the same table location twice. Is there any better solution to it, instead of querying same table 2 time?

Related

join three tables or use nested queries with IN

(a) List the names of the cities (from table City) whose name starts with 'p' and are in France (from table Country.
(b) List all columns from the Patient table for the patient's whose email contains '#gmail.com' and are from Atlanta
I have tried to join these tables but I do not seem know which values to join together
select city_name, email, first_name, last_name, pid, title, address_id, gender
from City c join Patient p on c.city_name = p.address_id
where p.email like '#gmail.com%'and c.city_name like 'Atlanta%
The result is supposed to show cities in France that start with 'p' and patients within a table whose email contains #gmail.com and that live in Atlanta.
You should not link the city name with the address id (c.city_name = p.address_id). Similar fields (usually primary and foreign keys) that contain the same structure (type, length, etc.) should be linked.
Since you are using the address ID, the address table must also be added as it contains the city ID. Regarding email search, "contains" means that it may be in the middle, so you must allow any characters at the beginning as well.
Try something like this:
select
c.city_name, p.email, p.first_name, p.last_name, pid, title, p.address_id, p.gender
from Patient as p
join Address as a on p.address_id = a.address_id
join City as c on c.city_id = a.city_id
where p.email like '%#gmail.com%'
and c.city_name = 'Atlanta'

How to concatenate SQL queries across 3 tables

I have 3 tables in my database as follows:
labs
id
lab_name
lab_owner_id (references id in labs_members table)
lab_manager_id (references id in labs_members table)
labs_members
id
person_id (references id in labs_person table)
labs_person
id
first_name
last_name
I need to construct a query to get all of the information from the labs table including the first and last names of the lab owner and lab manager.
I've tried variations of the following query:
SELECT labs.id, labs.lab_name, labs.lab_owner_id, labs.lab_manager_id
FROM labs
LEFT JOIN labs_members on labs.lab_owner_id = labs_members.id
LEFT JOIN labs_members on labs.lab_manager_id = labs_members.id
LEFT JOIN labs_person on labs_person.id = labs_members.person_id
ORDER BY labs.lab_name ASC
However, I haven't had any success getting both the lab manager and lab owner names at the same time. I get an error, or either the lab manager name OR the lab owner name (but not both).
Any assistance is greatly appreciated. Thanks!
You are very very close. One more join to the person table and some table aliases and you'll be there:
SELECT labs.id, labs.lab_name, labs.lab_owner_id, labs.lab_manager_id, owner_person.first_name, owner_person.last_name, manager_person.first_name, manager_person.last_name
FROM labs
LEFT JOIN labs_members as owner on labs.lab_owner_id = labs_members.id
LEFT JOIN labs_members as manager on labs.lab_manager_id = labs_members.id
LEFT JOIN labs_person as owner_person on owner_person.id = owner.person_id
LEFT JOIN labs_person as manager_person on manager_person.id = manager.person_id
ORDER BY labs.lab_name ASC

Listing later dates in comparison to another entry in SQL

How would I list the students who started later than start year of student with id = 8871?
This is what I have so far:
SELECT sid s1
FROM Student
WHERE s1.started <= sid = '8871';
I went to this link for some reference but I'm still having issues:
Query comparing dates in SQL
Student Table has:
Student (Lastname, Firstname, SID, Started)
any help would be great
I'd use a self join where one side of the join contains the students you want and the the reference student 8871:
SELECT a.*
FROM student a
JOIN student b ON b.sid = '8871' AND a.started > b.started

Mysql join query on multiple tables

I have three tables and in those tables these basic fields
contacts
con_id (primary key)
con_name
con_work_id
con_country_id
work
work_id (primary key)
work_company_name
work_country_id
country
country_id (primary key)
country_name
I'm trying to run a query which displays the con_name, work_company_name and then the country name for BOTH the contact and the work company.
I've tried this;
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country ON contacts.con_country_id = country.country_id
LEFT JOIN country ON work.work_country_id = country.country_id
But of course this doesn't work because the last join causes a clash with the second one.
I'm almost there, but can't get the query to display the country_name associated with both the contact AND the work company.
I'd appreciate a way forward.
Many thanks,
Wonder
The following should work:
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country c1 ON contacts.con_country_id = c1.country_id
LEFT JOIN country c2 ON work.work_country_id = c2.country_id
The trick is to add an alias to the table, so that it is possible to distinguish the two.

MySQL get multiple result from one table in one row

The are two tables "costs" and "contacts". Names off all sellers and buyers are in "contacts" table. With following query i retrieve the id of seller and buyer for each item but I want to get their names from "contacts" table
SELECT
costs.id as ID,
costs.idContactPayedBy,
costs.idContactPayedTo
FROM costs
WHERE
costs.idbuilding=286
but I want to get seller and buyers names from contacts table
SELECT
costs.id as ID,
contacts.lastname as seller,
contacts.lastname as buyer
FROM costs , contacts
WHERE
costs.idbuilding=286
and costs.idContactPayedBy = contacts.id
and costs.idContactPayedTo = contacts.id
so the desired result is like this
ID Seller Buyer
21 jackson Brown
29 Bush wilson
SELECT
c.id as ID,
cntby.lastname as seller,
cntto.lastname as buyer
FROM costs AS c
INNER JOIN contacts AS cntby ON c.idContactPayedBy = cntby.id
INNER JOIN contacts AS cntto ON c.idContactPayedTo = cntto.id
WHERE c.idbuilding=286
Note 1: Use INNER JOIN only if idContactPayed[By/To] columns are mandatory (NOT NULL). If these columns allows nulls then you should use LEFT OUTER JOIN. In my opinion, both columns should be mandatory.
Note 2: As a matter of style: please avoid old style joins (ANSI 86/89): FROM table1 a, table2 b WHERE <join condition>.