How to do one select two query with join table - mysql

I have two table as follows:
- tblEmployee
employeeID | Name
10 | sothorn
20 | lyhong
30 | sodaly
40 | chantra
50 | sangha
60 | bruno
- tblSale
ID | employeeID | employeeSaleID
1 | 30 | 10
2 | 10 | 40
3 | 50 | 20
I would like to select from tableSale and join with tblEmployee result that:
1 | sodaly | sothorn
2 | sothorn | chantra
3 | sangha | lyhong

Here is a sample query on your data.
http://sqlfiddle.com/#!2/b74ca/5/0

Simply select all rows of the tblSale table, and join tblEmployee table twice:
SELECT s.ID, e1.Name, e2.Name
FROM tblSale s
INNER JOIN tblEmployee e1
ON e1.employeeID = s.employeeID
INNER JOIN tblEmployee e2
ON e2.employeeID = s.employeeSaleID

Try this:
Here you need to use inner join to get data from both tables.
select
id, e1.name as name1, e2.name as name2
from
tblSale s, tblEmployee e1, tblEmployee e2
where
s.employeeID=e1.employeeID
and
s.employeeSaleID=e2.employeeID
order by
s.id
Thanks

This is simple
QUERY
SELECT
tblSale.ID,
l.Name,
r.Name
FROM tblSale
INNER JOIN tblEmployee l On l.employeeID = tblSale.employeeID
INNER JOIN tblEmployee r ON r.employeeID = tblSale.employeeSaleID
FIDDLE
OUTPUT
| ID | LNAME | RNAME |
|----|---------|---------|
| 1 | sodaly | sothorn |
| 2 | sothorn | chantra |
| 3 | sangha | lyhong |

just join the employee table twice, once to employeeID, once to employeeSaleID

Related

Special Join On 1 to N mapped columns

This question is an extension of AJC's Join On 1 to N mapped columns.
What if EMPLOYEE_DETAILS is like this:
+---------+-------------+--------------+
| EMP_ID | AREA_CODE | SECTOR_CODE |
+---------+-------------+--------------+
| 1223 | 5001 | 1001 |
| 3224 | (NULL) | 2001 |
| 3225 | 6001 | (NULL) |
+---------+-------------+--------------+
Then how would it be possible to map the respective names like this output:
+---------+-------------+--------------+
| EMP_ID | AREA_NAME | SECTOR_NAME |
+---------+-------------+--------------+
| 1223 | AREA 1 | SECTOR 1 |
| 3224 | (NULL) | SECTOR 2 |
| 3225 | AREA 2 | (NULL) |
+---------+-------------+--------------+
I thought about doing a union of joins like so,
select e.emp_id, a.LOCATION_NAME as area, s.LOCATION_NAME as sector
from EMPLOYEE_DETAILS e
join LOCATION_DETAILS a on a.id = e.AREA_CODE
join LOCATION_DETAILS s on s.id = e.SECTOR_CODE
union
select e.emp_id, a.LOCATION_NAME as area, NULL as sector
from EMPLOYEE_DETAILS e
join LOCATION_DETAILS a on a.id = e.AREA_CODE
union
select e.emp_id, NULL as area, s.LOCATION_NAME as sector
from EMPLOYEE_DETAILS e
join LOCATION_DETAILS s on s.id = e.SECTOR_CODE;
But then this would require me to use this as an internal query to get the expected result.
Is there a more concise way to go about doing this?
Why not left join twice?
select e.emp_id, a.location_name as area, s.location_name as sector
from employee_details e
left join location_details a on a.id = e.area_code
left join location_details s on s.id = e.sector_code
I can advice solution with single join like:
select
e.emp_id,
group_concat(if(ld.id = e.area_code, ld.location_name, null)) as area,
group_concat(if(ld.id = e.sector_code, ld.location_name, null)) as sector
from EMPLOYEE_DETAILS e
left join LOCATION_DETAILS ld
on (ld.id = e.area_code or ld.id = e.sector_code)
group by e.emp_id;
Here the fiddle: SQLize.online

SQL Join tables and concat names from table 2

In Mysql 5.5 I´ve three tables like:
employee
ID | firstname | lastname
-------------------------
1 | John | Doe
2 | Henry | Fonda
employee_projects
ID | employee_id | project_id
------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
projects
ID | name
----------------------
1 | house
2 | cottage
3 | castle
How do I JOIN employee over emloyee_project with projects that I get as result:
ID | firstname | lastname | projects
-------------------------------------
1 | John | Doe | house, cottage
2 | Henry | Fonda | castle
You can join the tables together and use group_concat to aggregate the rows into csv values.
select e.*, t.projects
from employee e
left join (
select ep.employee_id, group_concat(p.name) as projects
from employee_projects ep
join projects p on ep.project_id = p.id
group by ep.employee_id
) t on e.id = t.employee_id;
You can use MySQL's group_concat:
select e.id
, e.firstname
, e.lastname
, group_concat(p.name) as projects
from employee e
left join
employee_projects ep
on e.id = ep.employee_id
left join
projects p
on ep.project_id = p.id
group by
e.id
, e.firstname
, e.lastname

How to use joins in MySQL in this case

I have an employee table and a department table. How to can I use join to get the required below result. manager_id in the employee table is nothing but the employee id. Please help me to find out the answer
Employee Table
id | name | manager_id | department_id
----------------------------------------
1 | A | NULL | 1
2 | B | 1 | 2
3 | C | NULL | 3
4 | D | 3 | 2
Department Table
id | department_name
-------------------------
1 | Admin
2 | HR
3 | Finance
Required OutPut
id | name | manager_name | department_name
-----------------------------------------------
1 | A | NULL | Admin
2 | B | A | HR
3 | C | NULL | Finance
4 | D | C | HR
Use both Inner join and outer join
select E1.ID, E1.Name, E2.Name, D.department_name
FROM Employee E1
LEFT OUTER JOIN Employee E2 ON E2.ID = E1.manager_id
INNER JOIN Department D ON D.id = E1.department_id
SELECT E1.id, E1.name, E2.name as manager_name, D1.department_name
FROM Employee E1
LEFT JOIN Employee E2 ON (E1.id = E2.manager_id)
JOIN Department D2 ON (E1.department_id = D1.id)
You need first do the self join to find the manager name and after that nee to join with the Department to find the appropriate Department name.
This will help you.
SELECT e.id as id, e.name as name e.manager_id as manager_id,
d.department_name as department_name
FROM Employee as e
JOIN Department as d
ON e.department_id = d.id

Get the count of records for table with foreign key

I have the following tables:
Persons:
person_id | name |
-------------------------
1 | John |
2 | Adam |
3 | Elen |
-------------------------
Orders:
order_id | person_id | product |
---------------------------------------------
1 | 1 | TV |
2 | 1 | Radio |
3 | 1 | toothbrush |
4 | 2 | mp3 player |
5 | 2 | watch |
6 | 3 | ps 3 |
---------------------------------------------
Now I need to query above tables to get the following result:
person_id | name | order_count |
-----------------------------------------
1 | John | 3 |
2 | Adam | 2 |
3 | Elen | 1 |
-----------------------------------------
I tried something like:
SELECT u.person_id, u.name, COUNT(o.order_id) FROM persons AS p LEFT JOIN orders AS o ON (p.person_id=o.person_id);
unfortunately this doesn't work. Do you have any idea how to solve this ?
select Persons.person_id, Persons.name, p.order_count
from Persons
inner join (select person_id, count(*) as order_count from Orders group by person_id) p
on Persons.person_id = p.person_id
If you need also get that persons, who doesn't have any orders, then use left join instead of inner join
TRY THIS
SELECT persons.id, persons.name, COUNT(orders.id) AS order_count FROM orders LEFT JOIN persons ON (persons.person_id=orders.person_id) GROUP BY persons.name
Try this....
"select o.person_id,p.name,count(o.person_id) as order_count from tblorders o
Join tblpersons p on o.person_id=p.person_id
group by o.person_id,p.name"
Try this
select o.person_id,p.name,count(o.person_id) as order_count from tblorders o
Join tblpersons p on o.person_id=p.person_id
group by o.person_id,p.name
select p.person_id, p.name, o.cnt
from Persons p
left join
(select person_id , count(order_id) as cnt from Orders group by person_id) o
on p.person_id = o.person_id

MYSQL Query : How to UNION two table with ORDER BY

can anyone help me solve this problem? i've been trying to query this but i keep getting error.
MySQL code:
SELECT * FROM
(
SELECT BU.*, BD.BOOK_TITLE AS BOOK_TITLE, BD.BOOK_COMPANY AS COMPANY,
BD.RETURN_DATE AS RETURN
FROM BOOK_USER BU
INNER JOIN BOOKING_DETAIL BD ON (BU.USR_ID = BD.USR_ID)
UNION
SELECT BU.*, "NEW REGISTERED" AS BOOK TITLE, 'RENT-A-BOOK' AS COMPANY,
BU.REGISTER_DATE AS RETURN
FROM BOOK_USER BU
) AS BU
GROUP BY BU.USR_ID
The Tables:
BOOK_USER
+---------+----------+---------------+
| USR_ID | USR_NAME | REGISTER_DATE |
+---------+----------+---------------+
| 1 | john | 2011-09-20 |
+---------+----------+--------------+
| 2 | jane | 2011-12-05 |
+---------+----------+--------------+
| 3 | doe | 2012-02-16 |
+---------+----------+--------------+
| 4 | mary | 2012-02-02 |
+---------+----------+--------------+
BOOKING_DETAIL
+---------+----------+------------+-----------+--------------+
| BOOK_ID | USR_ID | BOOK_TITLE | COMPANY | RETURN_DATE |
+----------+--------+-------------+-----------+--------------+
| 1 | 1 | DEAR JOHN |ABC PVT LMT| 2011-11-01 |
+---------+---------+-------------+-----------+--------------|
| 2 | 1 | LUCKY | DEF | 2012-03-18 |
+---------+---------+-------------+-----------+--------------|
| 3 | 1 | THE RISE | GHI | 2012-06-12 |
+---------+---------+-------------+-----------+--------------|
| 4 | 2 | HELLO | TIMES | 2012-01-11 |
+---------+---------+-------------+-----------+--------------|
| 5 | 2 | SHOPAHOLIC | | 2012-08-31 |
+---------+---------+-------------+-----------+--------------|
| 6 | 3 | LOST | | 2012-06-20 |
+---------+---------+-------------+-----------+--------------|
The result should return the latest RETURN_DATE and SORTED by USR_ID.
eg:
John, THE RISE, GHI,2012-06-12
Jane,SHOPAHOLIC,RENT-A-BOOK,2012-08-31
doe, LOST,RENT-A-BOOK,2012-06-20
mary, NEW REGISTERED,RENT-A-BOOK , 2012-02-02
The query constains a subquery which gets the latest RETURN_DATE for each user.
SELECT a.*, c.BOOK_TITLE, c.RETURN_DATE
FROM book_user a
INNER JOIN
(
SELECT usr_ID, MAX(return_DATE) maxDate
FROM booking_detail
GROUP BY usr_ID
) b ON a.usr_ID = b.usr_ID
INNER JOIN booking_detail c
ON b.usr_ID = c.usr_ID AND
b.maxDate = c.return_DATE
SQLFiddle Demo
UPDATE
use LEFT JOIN and COALESCE
SELECT a.USR_ID,
a.USR_NAME,
COALESCE(c.BOOK_TITLE,'RENT-A-BOOK') BOOK_TITLE,
COALESCE(c.RETURN_DATE, a.REGISTER_DATE) RETURN_DATE
FROM book_user a
LEFT JOIN
(
SELECT usr_ID, MAX(return_DATE) maxDate
FROM booking_detail
GROUP BY usr_ID
) b ON a.usr_ID = b.usr_ID
LEFT JOIN booking_detail c
ON b.usr_ID = c.usr_ID AND
b.maxDate = c.return_DATE
SQLFiddle Demo
Try that :
SELECT *
FROM (
(
SELECT BU.*, BD.BOOK_TITLE, BD.BOOK_COMPANY AS COMPANY,BD.RETURN_DATE AS RETURN FROM BOOK_USER BU INNER JOIN BOOKING_DETAIL BD ON (BU.USR_ID = BD.USR_ID)
) UNION ALL (
SELECT BU.*, BD.BOOK_TITLE, 'RENT-A-BOOK' AS COMPANY,BD.RETURN_DATE AS RETURN FROM BOOK_USER BU INNER JOIN BOOKING_DETAIL BD ON (BU.USR_ID = BD.USR_ID)
)
) BU
GROUP BY BU.USR_ID
You can directly get the output using following query check the query:
SELECT BU.*, IFNULL(BD.BOOK_TITLE, 'NEW REGISTERED') AS BOOK_TITLE, IFNULL(BD.BOOK_COMPANY, 'RENT-A-BOOK') AS COMPANY,BD.RETURN_DATE AS RETURN
FROM BOOK_USER BU
LEFT JOIN (SELECT * FROM (SELECT USR_ID, BOOK_TITLE, BOOK_COMPANY, RETURN_DATE FROM BOOKING_DETAIL ORDER BY RETURN_DATE DESC) AS A GROUP BY USR_ID) AS BD ON (BU.USR_ID = BD.USR_ID)
ORDER BY BU.USR_ID;