Combine two tables into one in MySQL - 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;

Related

left join same table

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

SELECT record NOT IN SET of Record

student table
|----------------------|
| student_id | name |
|------------|---------|
| 1 | Richard |
| 2 | Emily |
| 3 | Hans |
|------------|---------|
lecturer table
|--------------------|
| lecturer_id | name |
|-------------|------|
| 1 | John |
| 2 | Mike |
|-------------|------|
classes table
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 1 | 1 | Basic of algorithm |
| 2 | 1 | Basic of programming |
| 3 | 2 | Database Essentials |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
attendance table
|-----------------------|
| class_id | student_id |
|----------|------------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 4 | 1 |
| 4 | 2 |
|----------|------------|
how to show classes records (from classes table) that not attended by Hans (student) in MySQL?
desired result :
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 2 | 1 | Basic of programming |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
One approach uses EXISTS:
SELECT c.class_id, c.lecturer_id, c.material
FROM classes c
WHERE NOT EXISTS (SELECT 1 FROM attendance a
INNER JOIN student s
ON a.student_id = s.student_id
WHERE a.class_id = c.class_id AND
s.name = 'Hans');
Using joins -
select c.class_id
from attendance a inner join student s on (a.student_id=s.student_id and s.student_id='Hans')
right outer join classes c on (a.class_id=c.class_id)
where a.class_id is null

Using inner join and not in together in mysql

I have a wordpress database with default users table and two custom tables as below
1. wp_users
| id | display_name |
|----|--------------|
| 1 | Ibbs |
| 2 | Nina |
| 3 | rakib |
2. wp_invite
| post_id | user_id | status |
|---------|---------|----------|
| 3342 | 1 | accepted |
| 3342 | 2 | accepted |
| 3342 | 3 | accepted |
3. wp_rating
| id | reviwer | reviewed | post | know | skill | time | comm |
|----|---------|----------|------|------|-------|------|------|
| 2 | 3 | 1 | 3342 | b | b | b | b |
| 5 | 1 | 2 | 2122 | a | c | d | a |
| 7 | 2 | 3 | 3342 | d | a | b | c |
i want to select * from wp_invite where status = accepted, display_name from wp_users and then want to exclude rows from the result where all of these three conditions meet
1. wp_invite.user_id is not equal to wp_rating.reviewer and
2. wp_invite.user_id is not equal to wp_rating.reviewed and
3. wp_invite.post_id is not equal to wp_rating.post.
My desired output for post = 3342 and reviewer = 3
| user_id | display_name |
|---------|--------------|
| 2 | Nina |
| 3 | rakib |
My desired output for post = 3342 and reviewer = 2
| user_id | display_name |
|---------|--------------|
| 1 | Ibbs |
| 2 | Nina |
My desired output for post = 2122 and reviewer = 2
| id | display_name |
|----|--------------|
| 1 | Ibbs |
| 2 | Nina |
| 3 | rakib |
My desired output for post = 2122 and reviewer = 1
| id | display_name |
|----|--------------|
| 1 | Ibbs |
| 3 | rakib |
I have tried the following query but my output is empty:
SELECT wp_invite.user_id, wp_users.display_name, wp_invite.post_id
FROM wp_invite
INNER JOIN wp_users ON wp_invite.user_id = wp_users.id
WHERE (status = 'accepted')
AND user_id NOT IN (SELECT reviewer FROM wp_rating)
AND user_id NOT IN (SELECT reviewed FROM wp_rating)
AND post_id NOT IN (SELECT post FROM wp_rating)
ID should be capitalized:
inner join wp_users on wp_invite.user_id = wp_users.ID

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