combine information from two tables into one table - mysql

I need to combine information from two tables into one table, the following table is
table 1
+----+---------------+---------+
|id_k| name | value |
+----+---------------+---------+
| 1 | enak | 4 |
| 2 | nor | 3 |
+----+---------------+---------+
table 2
+------+------+---------+
| id_d | id_k | feel |
+------+------+---------+
| 1 | 1 | good |
| 2 | 1 | better |
| 3 | 1 | verygood|
+------+------+---------+
result should be
+------+------+-------+------------------------+
| id_d | name | value | feel |
+------+------+-------+------------------------+
| 1 | enak | 4 | good, better, verygood |
| 2 | nor | 3 | |
+------+------+-------+------------------------+
this is my code [not worked]
select k.name, k.value, s.feel
from table1 as k
left join table2 as s on s.id_k=k.id_k

http://sqlfiddle.com/#!9/3a7564/1
SELECT t1.id_k,
t1.`name`,
t1.`value`,
GROUP_CONCAT(t2.feel) AS feel
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id_k = t2.id_k
GROUP BY t1.id_k

You could use the gorup_concat function to concatinate the values from table2 to a coma-delimited string in the result:
SELECT table1.id_k, name, value, GROUP_CONCAT(feel SEPARATOR ', ') AS feel
FROM table1
JOIN table2 ON table1.id_k = table2.id_k
GROUP BY table1.id_k

Related

combine two tables in mysql with different column names [duplicate]

This question already has answers here:
How to join two tables mysql?
(4 answers)
Closed 1 year ago.
Trying to combine two tables with different columns and rows
TABLE_1
| id_product | product_state_1 |
|---------------------|------------------|
| 106002 | 1 |
| 114000 | 0 |
| 106005 | 1 |
| 106004 | 1 |
TABLE_2
| id_product | product_state_2 |
|---------------------|------------------|
| 106002 | 0 |
| 114000 | 1 |
| 106005 | 1 |
| 109005 | 1 |
Required result_table
| id_product | product_state_1 | product_state_2 |
|---------------------|------------------|------------------|
| 106002 | 1 | 0 |
| 114000 | 0 | 1 |
| 106005 | 1 | 1 |
| 109005 | NULL | 1 |
| 106004 | 1 | NULL |
I tried the following code with the UNION
SELECT * FROM TABLE_1
UNION
SELECT * FROM TABLE_2;
but the following code does not give the required result.
Use LEFT and RIGHT JOIN along with UNION:
SELECT t1.id_product, t1.product_state_1, t2.product_state_2
FROM TABLE_1 t1
LEFT JOIN TABLE_2 t2
ON t1.id_product = t2.id_product
UNION
SELECT t2.id_product, t1.product_state_1, t2.product_state_2
FROM TABLE_1 t1
RIGHT JOIN TABLE_2 t2
ON t1.id_product = t2.id_product;
DB Fiddle

Combining multiple JOINS(inner and Left)into one Select

My problem is that i have 4 tables, that i want to combine. Table1 has Tickets, Table2 with users, Table 3 with admins and Table4 with files. My Select was woring great when i had only 3 tables, without files, my select back then was something like this
SELECT
Table1.TicketNumber,
Count(Table1.TicketNumber) as "number of tickets",
Table2.UserName,
Table3.AdminName
FROM Table1
inner join
table2 on table1.ID_U=table2.ID
inner join
table3 on table1.ID_A=table3.ID
GROUP BY Table1.TicketNumber
Then i decided to add to my select another table(Table4), from which i would sum number of files for corresponding Ticket, and my Select is something like this:
SELECT
Table1.TicketNumber ,
Table1.count,
Table4.count
FROM
( SELECT TicketNumber,
count(*) AS count
FROM Table1
GROUP BY TicketNumber
)Table1
LEFT JOIN
( SELECT TickerNumber,
count(*) as count
FROM Table2
GROUP BY TicketNumber
) Table2 ON Table1.Name=Table2.Name
My problem is when i try to in somehow merge these two selects to get all that i want, i get syntax error in my INNER JOIN that Table1.ID_U doesnt exist.
Here is simplified struct of my Tables
Table1 Table 2/3
+----+--------------+------+------+----------+ +----+----------------+
| ID | TicketNumber | ID_U | ID_A | SomeData | | ID | User/Admin Name|
+----+--------------+------+------+----------+ +----+----------------+
| 0 | T001 | 1 | 1 | blah | | 0 | Name |
| 1 | T002 | 2 | 3 | blah | | 1 | Name |
| 2 | T002 | 2 | 3 | blah | | 2 | Name |
| 3 | T003 | 2 | 2 | blah | | 3 | Name |
| 4 | T004 | 3 | 1 | blah | | 4 | Name |
+----+--------------+------+------+----------+ +----+----------------+
My Table4
+----+------------+----------+
| ID | TicketName | FileName |
+----+------------+----------+
| 0 | T002 | Name |
| 1 | T002 | Name |
| 2 | T003 | Name |
| 3 | T004 | Name |
| 4 | T007 | Name |
+----+------------+----------+
My goal is to reach Select that looks like this
+----+--------------+----------------+--------------+----------+-----------+
| ID | TicketNumber | HowManyTickets | HowManyFiles | User | Admin |
+----+--------------+----------------+--------------+----------+-----------+
| 0 | T001 | 1 | 0 | UserName | AdminName |
| 1 | T002 | 2 | 2 | UserName | AdminName |
| 2 | T003 | 1 | 1 | UserName | AdminName |
| 3 | T004 | 5 | 1 | UserName | AdminName |
+----+--------------+----------------+--------------+----------+-----------+
Unfortunaly all i am capable of doing is either getting
TicketNumber, HowManyTickets, HowManyFiles,
or
TicketNumber, HowManyTickets, User, Admin
It seems your result ID is generated since its not anymore the same to your TicketID.
Here's my suggestion, using row_number() to generate the ID, then use sum() aggregation function to group your ticketNumber. left join will give you 0 result on your Ticket T001.
select row_number() over (order by t1.TicketNumber) as ID
, t1.TicketNumber
, sum(case when coalesce(t1.TicketNumber, '') = '' then 0 else 1 end) as HowManyTickets
, coalesce(t4.numFiles, 0) as HowManyFiles
, t2.Name as USER
, t3.Name as Admin
from table1 t1
left join table2 t2 on t2.ID = t1.ID_U
left join table3 t3 on t3.ID = t1.ID_A
left join
(select count(1) as numFiles, TicketNumber from table4 group by TicketNumber) t4 on t4.TicketNumber = t1.TicketNumber
group by t1.TicketNumber, t4.numFiles

How can i join 2 tables with option values on certain column

On table1
+-------+-------+
| unid1 | unid2 |
+-------+-------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+-------+-------+
On table 2
+---------+-------+-------+------+
| tableid | unid1 | unid2 | type |
+---------+-------+-------+------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 1 |
| 3 | 3 | 3 | 1 |
| 4 | 3 | 0 | 2 |
| 5 | 3 | 0 | 2 |
| 6 | 4 | 4 | 3 |
| 7 | 5 | 5 | 3 |
+---------+-------+-------+------+
Expected result.
+-------+-------+------+
| unid1 | unid2 | type |
+-------+-------+------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 3 | 0 | 2 |
| 3 | 0 | 2 |
+-------+-------+------+
My SQL code
select t1.*, t2.* from table1 t1
left join table2 t2 on t1.unid1 = t2.unid1 and t1.unid2 = t2.unid2 and
t2.type in (1 , 2);
My SQL query does not give the result that i wanted, how can i get the result that i want.
Your expected output implies that you want to retain all rows in table2 whose unid1 values can be found in table1. If so, then we can just inner join these two tables on the unid1 column. This assumes that unid1 is unique in table1.
SELECT t2.unid1, t2.unid2, t2.type
FROM table2 t2
INNER JOIN table1 t1
ON t2.unid1 = t1.unid1;
If I understand you correctly, you want to output values wherein the rows from Table1 matches on Table2 in both columns unid1 and unid2 and that the type must be contained with your desired values.
SELECT b.unid1, b.unid2, b.type
FROM table1 a
INNER JOIN table2 b
ON a.unid1 = b.unid1
WHERE EXISTS
(
SELECT 1
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.unid1 = t2.unid1
AND t1.unid2 = t2.unid2
WHERE t2.type in (1,2)
AND a.unid1 = t1.unid1
)
Here's a DEMO.

How to calculate the number of total votes when there is two Posts tables?

Here is the structure of my tables:
// table1
+----+-------+--------------------+
| id | color | content |
+----+-------+--------------------+
| 1 | blue | it is a bad color |
| 2 | red | it is a good color |
| 3 | green | I don't like this |
+----+-------+--------------------+
// table2
+----+-------+---------------------+
| id | site | description |
+----+-------+---------------------+
| 1 | stack | help to programmers |
| 2 | google| everything you like |
+----+-------+---------------------+
// votes
+----+-----------+---------+-------+
| id | table_code| post_id | value |
+----+-----------+---------+-------+
| 1 | 1 | 1 | 1 | // table1, post1, +1upvote (blue)
| 2 | 1 | 2 | -1 | // table1, post2, -1downvote (red)
| 3 | 2 | 1 | 1 | // table2, post1, +1upvote (stack)
+----+-----------+---------+-------+
Also here is my query:
select t3.*, (select sum(value) from votes v where t3.id = v.post_id) total_votes
from (
select * from table1
union all
select * from table2
) t3
Here is my output:
+----+-------+---------------------+-------------+
| id | color | content | total_votes |
+----+-------+---------------------+-------------+
| 1 | blue | it is a bad color | 2 | // Problem (it should be 1)
| 2 | red | it is a good color | -1 |
| 3 | green | I don't like this | 0 |
| 1 | stack | help to programmers | 2 | // Problem (it should be 1)
| 2 | google| everything you like | 0 |
+----+-------+---------------------+-------------+
As you see in the this ^ table, the calculation of total_votesis wrong. How can I fix it?
Note: According to reality, I can not combine table1 and table2. So, please don't tell me your structure is crazy.
You have to also specify table_code in the UNION:
select t3.*,
(select sum(value)
from votes v
where t3.id = v.post_id and
v.table_code = t3.table_code) total_votes
from (
select *, 1 table_code from table1
union all
select *, 2 from table2
) t3
Using table_code in the correlated sub-query we can pick the correct values from votes table.

SQL sorting with multiple criteria

I have two tables.
Tab1:
+------------+
| id | title |
+------------+
| 1 | B |
| 2 | C |
| 3 | A |
| 4 | A |
| 5 | A |
| 6 | A |
| ... |
+------------+
Tab2:
+-------------------------------------------+
| id | item_id | item_key | item_value |
+-------------------------------------------+
| 1 | 1 | value | $4 |
| 2 | 1 | url | http://h.com/ |
| 3 | 2 | value | $5 |
| 4 | 3 | url | http://i.com/ |
| 5 | 3 | value | $1 |
| 6 | 3 | url | http://y.com/ |
| 7 | 4 | value | $2 |
| 8 | 4 | url | http://z.com/ |
| 9 | 5 | value | $1 |
| 10 | 5 | url | http://123.com/ |
| ... |
+-------------------------------------------+
item_id is a foreign key from tab1.
How do I make it so I get a table of ids from Tab1 in order according to criteria from both tables. The criteria are the following:
Order ASC by title. If title is the same,
Order DESC by value. If both title and value is the same,
Prioritize items who's 'url' key contains '123.com'.
The resulting table with the ordered results would be:
+------------+
| id | title |
+------------+
| 4 | A |
| 5 | A |
| 3 | A |
| 6 | A |
| 1 | B |
| 2 | C |
| ... |
+------------+
The results should include items that don't have the one, both, or none of the fields from Tab2 set.
As far as I understand, a simple join will do it. You'll have to join Tab2 twice, since you want to order by values from both rows.
SELECT Tab1.id, Tab1.title
FROM Tab1
JOIN Tab2 t2_val ON t2_val.item_id = Tab1.id AND t2_val.item_key='value'
JOIN Tab2 t2_url ON t2_url.item_id = Tab1.id AND t2_url.item_key='url'
ORDER BY title,
t2_val.item_value DESC,
t2_url.item_value LIKE '%123.com%' DESC
An SQLfiddle to test with.
A little complicated, because when you do the join you will get multiple rows. Here is an approach that aggregates tab2 before doing the join:
select t1.*
from Tab1 t1 left outer join
(select id,
max(case when item_key = 'value' then item_value end) as value,
max(case when item_key = 'url' then item_value end) as url
from Tab2 t2
group by id
) t2
on t1.id = t2.id
order by t1.title, t2.value desc,
(t2.url like '%123.com%') desc;