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

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

Related

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.

combine information from two tables into one table

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

How to fetch data from two tables using mysql query with some conditions applied to the second table?

I want to generate a result from a MySql query with below requirement.
Table 1 :
---------------
| nid | type |
---------------
| 1 | forum |
| 2 | forum |
| 3 | forum |
| 4 | forum |
---------------
Table 2
-----------------------
| nid | cid | created |
-----------------------
| 1 | 32 | 123456 |
| 2 | 65 | 123457 |
| 4 | 67 | 123458 |
| 1 | 61 | 123491 |
| 1 | 78 | 123497 |
| 2 | 23 | 123498 |
| 1 | 12 | 123698 |
| 4 | 54 | 132365 |
| 4 | 81 | 135698 |
| 1 | 30 | 168965 |
-----------------------
Now i require result like below. (Condition : I need the nid from first table, smallest cid for the corresponding nid in second table WHERE type = 'forum')
--------------
| nid | cid |
--------------
| 1 | 12 |
| 2 | 23 |
| 4 | 67 |
--------------
You can try this
SELECT tbl1.nid,
min(tbl2.cid) as cid
FROM table1 tbl1
INNER JOIN table2 tbl2 ON tbl1.nid=tbl2.nid
GROUP BY tbl2.nid;
SQL Fiddle
Try this
SELECT t1.nid,
min(t2.cid) as cid
FROM table1 t1
INNER JOIN table2 t2 ON t1.nid=t2.nid
GROUP BY t2.nid;
This could also work
select nid, min(cid) cid
from table2
group by nid
The above queries are have issues in group by clause. Kindly check this query.
SELECT t1.NID, MIN(t2.CID) AS cis from
TAB1 t1 inner join TAB2 t2 on t1.nid = t2.nid
group by t1.nid

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;

MySQL - How to update atable based on an internal ordering of the table

If I make a selection from my MySQL table like:
SELECT * FROM mytable ORDER BY country_id, category
I get the following table:
+------------+----------+-------+
| country_id | category | order |
+------------+----------+-------+
| 1 | A | 0 |
| 1 | B | 0 |
| 1 | F | 0 |
| 3 | A | 0 |
| 3 | C | 0 |
| 5 | B | 0 |
| 5 | L | 0 |
| 5 | P | 0 |
+------------+----------+-------+
What I would like to do is update the order column so that the value of that column is the order of that row for it's country_id. In other words the final table should look like this:
+------------+----------+-------+
| country_id | category | order |
+------------+----------+-------+
| 1 | A | 1 |
| 1 | B | 2 |
| 1 | F | 3 |
| 3 | A | 1 |
| 3 | C | 2 |
| 5 | B | 1 |
| 5 | L | 2 |
| 5 | P | 3 |
+------------+----------+-------+
If I use the original query given at the top in a subquery, that would give me the correct order but I can't figure out how to iterate through the table and suspect I thinking about it wrongly.
How can I get this result?
Many thanks
Try to use this query -
UPDATE mytable t1
JOIN (
SELECT t1.country_id, t1.category, COUNT(*) `order`
FROM mytable t1
LEFT JOIN mytable t2
ON t2.country_id = t1.country_id AND t2.category <= t1.category
GROUP BY
t1.country_id, t1.category
) t2
ON t1.country_id = t2.country_id AND t1.category = t2.category
SET t1.`order` = t2.`order`
As an aside note, you can remove order field from the table because this can calculated this on the fly.