left join same table - mysql

I have two tables (Agents) and (Customers)
Agents
| id | agent_name | leader_id|
------------------------------
| 1 | AAA | |
| 2 | BBB | 1 |
| 3 | CCC | 1 |
| 4 | DDD | |
| 5 | EEE | 4 |
Customers
| id | customer_name |agent_id|
-------------------------------
| 1 | ABC1 | |
| 2 | ABC2 | 1 |
| 3 | ABC3 | 3 |
| 4 | ABC4 | 3 |
| 5 | ABC5 | 5 |
I used LEFT JOIN to list all Customers with Agent Name
SELECT *, Customers.id AS id, Agents.id AS aid
FROM Customers LEFT JOIN Agents ON Customers.agent_id = Agents.id
How I can list Customers of specific team using leader_id ?

I think this should solve our problem
SELECT *, Customers.id AS id, Agents.id AS aid FROM Customers LEFT JOIN Agents ON Customers.agent_id = Agents.id where Agents.id = {1} or Agents.leader_id = {1}
Where, {1} could be any value
Correct me if this doesn't workout

Related

Combine two tables into one in MySQL

Suppose I have two tables named users and offers
users table
+----+----------+-----------+
| id | username | role |
+----+----------+-----------+
| 1 | A | client |
+----+----------+-----------+
| 2 | B | client |
+----+----------+-----------+
| 3 | C | candidate |
+----+----------+-----------+
| 4 | D | candidate |
+----+----------+-----------+
| 5 | E | candidate |
+----+----------+-----------+
offers table
+----+-----------+--------------+
| id | client_id | candidate_id |
+----+-----------+--------------+
| 1 | 1 | 3 |
+----+-----------+--------------+
| 2 | 2 | 4 |
+----+-----------+--------------+
| 3 | 1 | 5 |
+----+-----------+--------------+
| 4 | 2 | 3 |
+----+-----------+--------------+
| 5 | 2 | 5 |
+----+-----------+--------------+
I want to create a query to combine offers table with users table, like
+----+-----------+-----------------+--------------+--------------------+
| id | client_id | client_username | candidate_id | candidate_username |
+----+-----------+-----------------+--------------+--------------------+
| 1 | 1 | A | 3 | C |
+----+-----------+-----------------+--------------+--------------------+
| 2 | 2 | B | 4 | D |
+----+-----------+-----------------+--------------+--------------------+
| 3 | 1 | A | 5 | E |
+----+-----------+-----------------+--------------+--------------------+
| 4 | 2 | B | 3 | C |
+----+-----------+-----------------+--------------+--------------------+
| 5 | 2 | B | 5 | E |
+----+-----------+-----------------+--------------+--------------------+
I have written 2 queries that are working separately, but I want to combine these into one table.
SELECT offers.candidate_id, users.username AS candidate_username
FROM offers INNER JOIN users ON offers.candidate_id = users.id;
SELECT offers.client_id, users.username AS client_username
FROM offers INNER JOIN users ON offers.client_id = users.id;
You can try below -
SELECT offers.candidate_id,
a.username AS client_username,a1.username as candidate_username
FROM offers left JOIN users a ON offers.client_id = a.id
left join users a1 ON offers.candidate_id = a1.id
You can use Left Join for this purpose like:
SELECT offers.candidate_id,
a.username AS client_username,a1.username AS candidate_username
FROM offers left JOIN users a ON offers.client_id = a.id OR offers.candidate_id = a.id;

mysql query for multiple foreign keys

mysql tables are as follows
+------------+----------------+----------------+
| booking_id | boarding_point | dropping_point |
+------------+----------------+----------------+
| 1 | 2 | 4 |
| 2 | 1 | 2 |
+------------+----------------+----------------+
+-------------+---------------+
| location_id | location_name |
+-------------+---------------+
| 1 | chennai |
| 2 | coimbatore |
| 3 | tiruppur |
| 4 | erode |
| 5 | salem |
+-------------+---------------+
boarding_point and dropping_point are foreign keys for location_id.
Now I want the select query to display like
+------------+----------------+----------------+
| booking_id | boarding_point | dropping_point |
+------------+----------------+----------------+
| 1 | coimbatore | erode |
| 2 | chennai | coimbatore |
+------------+----------------+----------------+
can anyone please suggest me the query to display like above.
Join the booking table twice to the location table:
SELECT
b.booking_id,
t1.location_name,
t2.location_name
FROM booking b
INNER JOIN location t1
ON b.boarding_point = t1.location_id
INNER JOIN location t2
ON b.dropping_point = t2.location_id;
Demo

select data from table already selected from

I have a few tables that I am trying to get data out of. I need to link the cust and emp tables to the sale table. Then, in turn, get the names of the cust and emp from the person table.
I am stuck
Data looks like:
Table SALES
+--------+--------+-------+-------+
| saleID | custID | empID | total |
|--------+--------+-------+-------|
| 1 | 1 | 1 | 3.00 |
|--------+--------+-------+-------|
| 2 | 2 | 3 | 6.00 |
|--------+--------+-------+-------|
| 3 | 3 | 1 | 9.00 |
|--------+--------+-------+-------|
| 4 | 2 | 2 | 2.00 |
|--------+--------+-------+-------|
| 5 | 3 | 3 | 1.00 |
|--------+--------+-------+-------+
Table cust
+--------+---------+----------+
| custID | company | personID
+--------+---------+----------+
| 1 | comp1 + 2 |
+--------+---------+----------+
| 2 | comp2 + 4 |
+--------+---------+----------+
| 3 | comp3 + 6 |
+--------+---------+----------+
Table emp
+--------+----------+----------+
| custID | username | personID |
+--------+----------+----------+
| 1 | emp1 | 1 |
+--------+----------+----------+
| 2 | emp2 | 3 |
+--------+----------+----------+
| 3 | emp3 | 5 |
+--------+----------+----------+
Table person
+----------+------+
| personID | name |
+----------+------+
| 1 | per1 |
+----------+------+
| 2 | per2 |
+----------+------+
| 3 | per3 |
+----------+------+
| 4 | per4 |
+----------+------+
| 5 | per5 |
+----------+------+
| 6 | per6 |
+----------+------+
I want to query to give me this:
+--------+--------+----------+-------+---------+-------+
| saleID | custID | custName | empID | empName | total |
+--------+--------+----------+-------+---------+-------+
| 1 | 1 | per2 | 1 | per1 | 3.00 |
+--------+--------+----------+-------+---------+-------+
| 2 | 2 | per4 | 3 | per5 | 6.00 |
+--------+--------+----------+-------+---------+-------+
| 3 | 3 | per6 | 1 | per1 | 9.00 |
+--------+--------+----------+-------+---------+-------+
| 4 | 2 | per4 | 2 | per3 | 2.00 |
+--------+--------+----------+-------+---------+-------+
| 5 | 3 | per6 | 3 | per5 | 1.00 |
+--------+--------+----------+-------+---------+-------+
Simple JOINs should do the trick:
SELECT
a.saleID,
b.custID,
p1.name as custName,
c.empID,
p2.name as empName,
a.total
FROM SALES a
JOIN cust b
ON a.custID = b.custID
JOIN emp c
ON c.custID = a.custID AND c.empID = a.empID
JOIN person p1
ON p1.personID = b.personID
JOIN person p2
ON p2.personID = c.personID
ORDER BY saleID
Try this out:
select a.saleID, a.custID, d.name as custName,a.empID,e.nname as empName,
a.total
from
sales a
left join
cust b
on a.custID = b.custID
left join
emp c
on a.empID = C.custID
left join
person d
on b.personID = d.personID
left join
person e
on c.personID = e.personID
Let me know in case of any queries.

How to make a selection from two tables?

I have table 'articles'
+-------------+
| articles |
+----+--------+
| id | title |
+----+--------+
| 1 | title1 |
+----+--------+
| 2 | title2 |
+----+--------+
| 3 | title3 |
+----+--------+
table 'catalogue'
+---------------------+
| catalogue |
+----+--------+-------+
| id | group | name |
+----+--------+-------+
| 1 | group1 | name1 |
+----+--------+-------+
| 2 | group1 | name2 |
+----+--------+-------+
| 3 | group2 | name3 |
+----+--------+-------+
| 4 | group2 | name4 |
+----+--------+-------+
binding table 'bindTable'
+------------+--------------+-------+
| bindTable |
+------------+--------------+-------+
| id_article | id_catalogue | value |
+------------+--------------+-------+
| 1 | 2 | 1 |
+------------+--------------+-------+
| 1 | 3 | 4 |
+------------+--------------+-------+
| 3 | 1 | 2 |
+------------+--------------+-------+
| 3 | 3 | 1 |
+------------+--------------+-------+
| 3 | 4 | 3 |
+------------+--------------+-------+
and i need to get result as in table 'result', where i can get pairs "catalogue_name : value" for selected item from table 'article'
+-----------------------------------------------------+
| result |
+------------+---------------+----------------+-------+
| article_id | article_title | catalogue_name | value |
+------------+---------------+----------------+-------+
| 1 | title1 | group1_name2, | 1 |
| | | group2_name3 | 4 |
+------------+---------------+----------------+-------+
| 3 | title3 | group1_name1, | 2 |
| | | group2_name3, | 1 |
| | | group2_name4 | 3 |
+------------+---------------+----------------+-------+
Can anyone tell me a query string with one DB query? Thank you for attention.
My vision:
SELECT b.id_article, a.title, c.group, c.name, b.value
FROM bindTable b
JOIN articles a ON a.id = b.id_articles
JOIN catalogue c ON c.id = b.id_catalogue
WHERE b.id_article = 1
but i need one row with pairs c.name&b.value for one a.id
A select with inner join
select a.article_id, a.article_title, b.catalogue_name, b.value
from bindTable as c
inner join articles as a on a.id = c. article_id
inner join catalogues as b on c. id_catalogue = b.id
This should do it.
SELECT articles.id AS article_id,
articles.title AS article_title,
CONCAT_WS('_', catalogue.group, catalogue.name) AS catalogue_name,
bindTable.value AS value
FROM bindTable
INNER JOIN articles ON bindTable.id_article = articles.id
INNER JOIN catalogue ON bindTable.id_catalogue = catalogue.id

Don't know how to merge two MySQL tables

I'm new in SQL queries. I have a problem with the query.
I have table books:
+----+-------+--------+-----------+
| id | title | author | publisher |
+----+-------+--------+-----------+
| 1 | Book1 | 1 | 1 |
| 2 | Book2 | 1 | 2 |
| 3 | Book3 | 2 | 1 |
| 4 | Book4 | 2 | 2 |
| 5 | Book5 | 2 | 3 |
+----+-------+--------+-----------+
And I'm have another table, which contains copies of books with given book_id.
+----+---------+
| id | book_id |
+----+---------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 4 |
| 8 | 4 |
| 9 | 5 |
+----+---------+
All I need is to merge these two tables into one to have table sorted by amount of book copies.
I found solution to get table sorted by amount of book copies:
select book_copies.book_id, count(*) total_count
from book_copies
group by book_id
having count(*) > 0
order by count(*) desc;
+---------+-------------+
| book_id | total_count |
+---------+-------------+
| 1 | 3 |
| 2 | 2 |
| 4 | 2 |
| 3 | 1 |
| 5 | 1 |
+---------+-------------+
Now I don't know how to merge them.
I've tried like this:
select books.title from books
left join
(select book_copies.book_id, count(*) total_count
from book_copies
group by book_id
having count(*) > 0
order by count(*) desc)
as total_table on books.id = total_table.book_id;
But all I get was this:
+-------+
| title |
+-------+
| Book1 |
| Book2 |
| Book3 |
| Book4 |
| Book5 |
+-------+
Could you help me please?
EDIT: by merging I meant smth like this:
+-------+--------+-----------+-----+
| title | author | publisher | tot |
+-------+--------+-----------+-----+
| Book1 | 1 | 1 | 3 |
| Book2 | 1 | 2 | 2 |
| Book4 | 2 | 2 | 2 |
| Book3 | 2 | 1 | 1 |
+-------+--------+-----------+-----+
Many thanks to #Marco for the answer!
Dmitriy
I think you could try:
SELECT b.title, b.author, b.publisher, COUNT(bc.book_id) AS tot
FROM books b LEFT JOIN book_copies bc
ON b.id = bc.book_id
GROUP BY b.id
EDITED:
If you want sort, you can try
SELECT * FROM
(SELECT b.title, b.author, b.publisher, COUNT(bc.book_id) AS tot
FROM books b LEFT JOIN book_copies bc
ON b.id = bc.book_id
GROUP BY b.id) g
ORDER BY g.tot DESC