MySQL Understanding Basic Joins - mysql

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

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'

Unsolvable SQL Query

I need help with a MySQL-Query.
Table Company:
(PK) ID_Company
CompanyName
...
Table Address:
(PK) ID_Address
(FK) ID_Company
Street
...
Table ContactPerson:
(PK) ID_ContactPerson
(PK) ID_Company
PersonName
...
Now I want so create a SELECT statement to get a list with all customers. This should look like that:
ID_Company CompanyName Street PersonName
1 Google Test Joe
But a company can have none, one or multiple addresses and contact persons. I only want to select one address and one contact person in this query. And this is the point where I don't know how to do that.
One thing I tried is to select the MIN of ID_Address, but that doesn't work if a company doesn't have a address and and a contact person.
I hope you understand what I am trying to do.
Thank you all for your help! :)
You can use left join and aggregation:
select c.ID_Company, c.company_name, min(street), min(person_name)
from company c left join
address a
on c.ID_Company = a.ID_Company left join
ContactPerson cp
on cp.ID_Company = a.ID_Company
group by c.ID_Company, c.company_name;

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 get count from two related tables

I am using MySQL and database server and I have two tables one is for customer and another for customer_contacts.
Here are the table structures:
customer(
id(pk, ai)
name
email
)
and
customer_contacts(
id(ai)
customer_id
first_name
last_name
)
Now my question is:
lets say I have one customer has many customer_contacts like this
customer
id name email
1 john john#example.com
and customer_contacts is like this (first row)
id customer_id first_name last_name
1 1 john doe
2 1 johnp pual
like this
So here I want to get all the contact details count for the id john. So can some one tell me how to get that?
Any help and suggestions will be really appreciable. Thanks
If the id is already known (as suggested by the question, it would be a plain
SELECT COUNT(*) from customer_contacts
WHERE customer_contacts.id = 1;
You just need to know the number of contactacs for the customer John, right?
SELECT COUNT(*)
FROM customer cus INNER JOIN customer_contacts con ON cus.id = con.customer_id
WHERE cus.name = 'john'
Nevertheless, It would be better if you know the id of John. Your query would be this:
SELECT COUNT(*)
FROM customer_contacts
WHERE customer_id = 1
Simply join both tables and use count() with group by c.id use where to filter records
select c.*,
count(*) total_contacts
from customer c
left join customer_contacts cc on(c.id = cc.customer_id)
group by c.id
Fiddle Demo

Join query with flattened result

I have the following entities
AddressType is simply an enum field that define if the Email is Personal/Work/Other.
Is it possible to do a query that returns a flattened result like the one in the following sample?
CustomerID Full Name Personal Email Work Email
----------- -------------- ----------------- -----------------------
1 John Doe johndoe#hotmail.com john.doe#company.com
select c.CustomerID,
c.FullName as [Full Name],
epersonal.AddressText as [Personal Email],
ework.AddressText as [Work Email]
from Customer c
left outer join Email epersonal on c.CustomerID = epersonal.CustomerID
and epersonal.AddressType = 'personal'
left outer join Email ework on c.CustomerID = ework.CustomerID
and epersonal.AddressType = 'work'
Two main choices:
1) Select it as typical (with two rows, one for each email), then use the pivot operator to flatten.
Example of pivot (I call it an example as I wrote it in notepad. It may be slightly wrong, but it should point you the right way):
select
CustomerID,
FullName
[1] as WorkEmail,
[2] as HomeEmail
from
(select
c.CustomerID, c.FullName, e.AddressText, e.AddressType
from
Customer c
join emails e on e.CustomerID = c.CustomerID) as Source
pivot (
AddressText
FOR AddressType in ([1], [2])
)
2) Join to the email table twice, once for each type of address. Suggest outer joins so if one is missing you still get the other.