How to make a selection from two tables? - mysql

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

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;

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

Get MAX value rows from second table on join

I have 2 tables, first one keeps names and second is related to it on in = cid. I need to get only the highest date row from second table, once. Please look below for clearer explanation:
table1 a
+----+-------+
| id | name |
+----+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
+----+-------+
table2 c
+----+-------+------------+
| id | cid | galiojaiki |
+----+-------+------------+
| 1 | 1 | 2015-04-30 |
| 2 | 1 | 2015-09-30 |
| 3 | 1 | 2015-03-10 |
| 4 | 2 | 2015-06-30 |
| 5 | 2 | 2015-07-30 |
| 6 | 3 | 2015-05-11 |
| 7 | 4 | 2015-05-10 |
+----+-------+------------+
Expected result:
+------------+-------+
| galiojaiki | name |
+------------+-------+
| 2015-09-30 | name1 |
| 2015-07-30 | name2 |
| 2015-05-11 | name3 |
| 2015-05-11 | name4 |
+------------+-------+
My query:
SELECT a.*, c.galiojaiki FROM `y6fdt_igym_abonementai` AS a
INNER JOIN
(
SELECT max(galiojaiki) FROM y6fdt_igym_sutartys
) c
on c.cid= a.id
GROUP BY c.abonementas
How anout a simple aggregation using MAX?
Something like
SELECT a.name,
MAX(b.galiojaiki) as galiojaiki
FROM `y6fdt_igym_abonementai` AS a INNER JOIN
`y6fdt_igym_sutartys` as b ON a.ID = b.CID
GROUP BY a.name
SQL Fiddle DEMO

Select rows from table by id of a left join table

I have 3 tables:
+-------------+ +-------------+ +-------------+
| hotel | | hot_cat | | category |
+------+------+ +------+------+ +-------------+
| id | name | | hid | cid | | id | name |
+------+------+ +------+------+ +-------------+
| 1 | X | | 1 | 1 | | 1 | cat1 |
+------+------+ +------+------+ +-------------+
| 2 | Y | | 1 | 2 | | 2 | cat2 |
+------+------+ +------+------+ +-------------+
| 3 | Z | | 2 | 2 | | 3 | cat3 |
+------+------+ +------+------+ +-------------+
| 2 | 3 | | 4 | cat4 |
+------+------+ +-------------+
| 2 | 4 |
+------+------+
I want to select hotels where category is some value, but with all other categories assigned to this hotel. I have this query:
SELECT hot.*,GROUP_CONCAT(cat.name SEPARATOR '<br>') AS cats
FROM hotel hot
LEFT JOIN hot_cat hc ON hc.hid = hot.id
LEFT JOIN category cat ON cat.id = hc.cid
WHERE cat.id = 2
GROUP BY hot.id
So I get this:
+------+------+-------------------------+
| id | name | cats |
+------+------+-------------------------+
| 1 | X | 'cat2' |
+------+------+-------------------------+
| 2 | Y | 'cat2' |
+------+------+-------------------------+
What I want to achieve:
+------+------+-------------------------+
| id | name | cats |
+------+------+-------------------------+
| 1 | X | 'cat1<br>cat2' |
+------+------+-------------------------+
| 2 | Y | 'cat2<br>cat3<br>cat4' |
+------+------+-------------------------+
I also want it to work without the where clause and get hotels without category assigned:
+------+------+-------------------------+
| id | name | cats |
+------+------+-------------------------+
| 1 | X | 'cat1<br>cat2' |
+------+------+-------------------------+
| 2 | Y | 'cat2<br>cat3<br>cat4' |
+------+------+-------------------------+
| 3 | Z | '' |
+------+------+-------------------------+
Start your query at the hot_cat table, and then join into hotel. It's one extra jump through the tables, but it solves the problem.
SELECT hot.*,GROUP_CONCAT(cat.name SEPARATOR '<br>') AS cats
FROM hot_cat hc1
LEFT JOIN hotel hot ON hc1.hid = hot.id
LEFT JOIN hot_cat hc ON hc.hid = hot.id
LEFT JOIN category cat ON cat.id = hc.cid
WHERE hc1.id = 2
GROUP BY hot.id
If, instead, you don't need the WHERE then you can just use your existing query without the WHERE clause.

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