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;
Related
I want to do a left join on the following two tables and link it with another table
for eg.
I have a table called students
SID (pk)
fname
lname
assignment table
aid (pk)
dur
SID (fk)
pid (fk)
professor table
pid (pk)
pname
I will take fname from first table and pid from second table left join them and display corresponding pname from professor table.
So i want the table to look like this later
Fname Pname
Raju Jack
RAm Null
jim john
Thanks
I have written the code like this but it is not working
select students.fname, professor.pname
from student
LEFT JOIN professor ON professor.pid = assignment.pid
i want to do left join so i will get fname and pid , but instead i want to use pid to get pname and display fname and pname
You can do one thing if you want output like this.
Instead of using left join you can use simply join.
I have checked it out so try this code.
select s.fname, p.p_name
from students s
join assignment a on s.s_id = a.s_id
join professor p on a.p_id = p.p_id
Hope you will get right result.
I was hoping to create a view of assignment and professor tables with columns aid, sid, pname and combining them as assignment.pid=professor.pid .
Later I would use left join of student table on the view ....
What do you guys think ? Please chime in your thoughts ....
Thanks
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
I have two tables named company and customers.
In company table there are 3 fields ID, Company, and Type as:
ID Company Type
1 ABC Running
2 XYZ Current
Now again in customers table I submitted the value of company and customers, but here the value of company is submitted as an ID as:
Company Customer Name
1 monty
2 sandeep
now I want to search the company name in customer table but when i put company name in search box its showing nothing because the value of company name is in ID form in customer tabe.how can i achieve it.
Here is my query for search:
$sql = "Select * from customers where name like '%$term%' or location like '%$term%' or company like '%$term%'";
By JOINing the two tables:
Select *
from customers AS cust
INNER JOIN companies AS comp ON cust.Company = comp.Id
where comp.location like '%$term%'
or comp.company like '%$term%'
try this
SELECT co.*, cu.* FROM company as co, customers as cu where co.company_id = cu.company_id and co.company_name = '%$term%';
I have 2 tables such as below:
Table 1 contain UserID and Name while table 2 contain UserID, Skill_ID, and SkillName
What I want to do is, I want to search someone who has 2 skill, searched via SkillName.
example: I have Andy as Name where he have 2 skill PHP and C# so when i Search PHP & C#, Andy will be shown as result.
Can anybody help me? Thank you.
SELECT a.Name
FROM table1 a
INNER JOIN table2 b
ON a.userID = b.user_ID
WHERE b.skill_name IN ('PHP','C#')
GROUP BY a.Name
HAVING COUNT(*) = 2
the design of your table is not well normalized. The type of relationship is Many-to-Many and in this case, this should be three tables, Users, Skills and the linking table User_Skills.
A suggested design would be:
Table User
UserID (PK)
Name
Table Skills
SkillID (PK)
SkillName
Table User_Skills
UserID (FK)
SkillID (FK)
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