join three tables or use nested queries with IN - mysql

(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'

Related

Optimize multiple join same table

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?

Join 3 tables on mysql in specific situation

Hi I'm a newbie to this and I'm attempting to join 3 different tables:
Invoice: CustID, VIN, InvoiceType
Customer: CustID, Zip
Zipcode: Zip, City
I know I need to use a join clause but I'm not exactly sure how to show the specific City of the associated customer tied to the invoice along with all fields provided in invoice and show a specific InvoiceType. Here is what I currently have and I'm really where to go next or what other options to consider.
SELECT invoiceno, invoice.customerid, vin, invoicetype, city
FROM invoice, customer, zip
WHERE invoicetype = 'sell'
try this
select
i.CustID,
i.VIN,
i.InvoiceType,
c.CustID,
z.Zip,
z.City
from
Invoice i
inner join
Customer c
on i.CustID=c.CustID
inner join
zip z
on c.zip=z.zip
WHERE
i.invoicetype = 'sell'

MySQL, Show value of column if match exist else leave as null

I'm sure this has been asked before but can't find the answer.
I have 3 tables OWNER, CAR, HOUSE
OWNER has 2 columns id and name
CAR has 3 columns id, ownerId and cartype
HOUSE has 4 columns id, ownerId, address, country
I want to write a SQL query that gets the owners name, cartypes, and addresses that are in Sweden
Here comes the tricky part. I want all the owners names and cartypes in the result table even if they don't own a house in Sweden. Can I get all that in 1 query or do I need to use 2? How would that query look?
You should be able to accomplish this with a simple left join:
SELECT O.name, C.cartype, H.address, H.country
FROM OWNER AS O
JOIN CAR AS C ON O.id = C.ownerid
LEFT JOIN HOUSE AS H ON O.id = H.ownerid AND Ucase(H.country) = "SWEDEN"
This will always give you a list of owners and their car types, in addition, it will give you a list of those that also happen to have a house address in sweden.
First you need to join the table then add new column in query by using CASE to check
SELECT o.* , c.* ,h.*,
(CASE WHEN h.county ='sweden' THEN h.county ELSE NULL END) AS HasCountry
FROM OWNER o
JOIN CAR c ON (c.ownerId =o.id)
JOIN HOUSE h ON (h.ownerId =o.id)

Is there a join that returns a table where one row has been merged?

I have a table called phone, that has 1 column called names and another called number. I have a second table called address, that has 1 column called family and another called address. family and names have the same values however family has some repeats. I would like to join these into a table that has family and names as 1 column and then the phone and address as another. However, because of the size mismatch (more addresses than phone numbers) I am not sure if this is possible.
Thanks!
Is this what you are expecting?
Table 1
Name number
Bob 1234
Tom 3456
Table 2
family address
Bob MD
Bob NJ
Bob NY
Tom NC
Result
Name Number Address
Bob 1234 MD/NJ/NY
Assuming " was going for having the address column stay the same and the numbers just be repeated for each matching family" as in one of the comments:
select
A.family, A.address, P.number
from
address A
left join phone P on A.family = P.Name
order by
A.family, A.address, P.number
Fiddle
FTR, if it were me i would probably be more interested in this instead:
select
A.family,
group_concat(distinct A.address separator '\n') Address,
group_concat(distinct P.number separator ',') Phone
from
address A
left join phone P on A.family = P.Name
group by family
order by
A.family, A.address desc, P.number

MySQL Understanding Basic Joins

I am struggling to understand basic MySQL joins.
Basically I've got 2 tables one with a customer first name and address id in it and another with the actual address.
Rather than just displaying the customer name and an address id I want it to display the customer name and the actual address.
My basic select statement is like this:
SELECT firstName, addressId FROM Customer
It will display something like this:
firstName addressId
---------------------
Bob 56
Rather than than I would like to join the addressId with an actual address in another table
Like this:
firstName address
----------------------------------
Bob 45 Somewhere street
Is there anyone who can show me the best way to achieve this?
Also can anyone recommend a good tutorial for joins?
SELECT a.name, b.address
FROM Customer a INNER JOIN AddressList b on a.addressID = b.addressID
To learn more about joins, see the article below,
Visual Representation of SQL Joins
Your condition is inner join,
This is the simplest, most understood Join and is the most common. This query will return all of the records in the left table (Customer) that have a matching record in the right table (address). This Join is written as follows:
SELECT firstName, address FROM Customer
INNER JOIN address ON Customer.addressId=address.addressId
SQL_LIVE_DEMO
Sample Output :
FIRSTNAME ADDRESS
Bob 45 Somewhere street
Lets say that you have following tables:
Customer(ID, FName, LName, AddressID)
Address(AddressID, Streeet, HNUmber, City)
This will display customers address instead of AddressID:
SELECT c.ID, c.Fname, c.LName, a.Street, a.HNumber, a.City
FROM Customer c, Address a
WHERE
c.AddressID = a.AddressID
It would be:
SELECT firstName, address FROM Address As A
INNER JOIN Customer as C ON C.addressId=A.addressId
Visual Representation of JOINS
Put the same 'addressid' in both the name and address tables then join the two as:
select name, address from customer join addresses
on customer.addressid = addresses.addressid;
Assuming that both tables have an address id, you could use
SELECT firstname, address, from table1 JOIN table2
ON table1.addressid = table2.addressid