MySql fill up table with values depending on other table - mysql

In mySql I have 2 tables.
t1 looks like
id color
------------
1 red
2 green
3 blue
4 purple
t2 looks like
id t1_id ship
------------------------
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 b
I need to fill t2 so that for each entry in t1 there are three entries in t2 - one with "a" in the ship row one with "b" in the ship row and one with "c" in it.
I don't want to have multiple entries with the same t1_id <-> ship combo. So after the query t2 should look like this.
t2 after the query I am looking for
id t1_id ship
------------------------
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 b
7 2 c
8 3 a
9 3 c
10 4 a
11 4 b
12 4 c
Any ideas on how to achieve that?

If you want to put these in the table, you can use:
insert into t2 (t1_id, ship)
select t1.id, s.ship
from t1 cross join
(select distinct ship from t2) s left join
t2
on t2.t1_id = t1.id and t2.ship = s.ship
where t2.t1_id is null;
This inserts the "remaining" values in table2.

Related

Join two table with different columns [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 2 years ago.
I have two table that I want to combine without FULL OUTER JOIN as it not work in h2 db. I have to create select sql query:I have tried much with UNION too but I think it will not be possible with UNION.
Table 1
id c_id s_id p_date
---------------------------------------------
1 1 1 2020-10-10
2 1 1 2020-10-11
3 2 1 2020-10-11
4 2 2 2020-10-12
Table 2
id c_id s_id s_date
---------------------------------------------
1 1 1 2020-10-15
2 1 2 2020-10-16
3 2 2 2020-10-17
4 2 2 2020-10-17
I am expecting below result:
c_id s_id p_date s_date
-------------------------------------------------
1 1 2020-10-10 2020-10-15
1 1 2020-10-11 -
1 2 - 2020-10-16
2 1 2020-10-11 -
2 2 2020-10-12 2020-10-17
2 2 - 2020-10-17
Please help to get this result.
You can use union all like this:
select t1.c_id, t1.s_id, t1.p_date, t2.s_date
from table1 t1 left join
table2 t2
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
union all
select t2.c_id, t2.s_id, t1.p_date, t2.s_date
from table2 t2 left join
table1 t1
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
where t1.c_id is null;
The first subquery gets all the rows where there are matches between the two tables plus rows where table2 has no match for table1.
The second subquery gets the additional rows from table2 that have no match in table1.
Here is a db<>fiddle.

Swap column values across 2 different tables

I have two tables and need to swap the values of a column in each table - I can do this when they are in the same table but when I try to do this with different tables then the second value is already overwritten so gets lost.
For example:
table1
id user_id currency col2 col3......
1 1 10 Bob 2018-04-16
2 2 150 Tom 2018-05-17
3 3 60 Phil 2018-06-04
4 4 125 Jon 2017-12-01
5 5 35 Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 USD 16 Active
2 2 USD 17 Active
3 3 GBP 21 Left
4 4 CAD 16 Active
5 5 AUD 19 Active
I need these to look like:
table1
id user_id currency col2 col3......
1 1 USD Bob 2018-04-16
2 2 USD Tom 2018-05-17
3 3 GBP Phil 2018-06-04
4 4 CAD Jon 2017-12-01
5 5 AUD Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 10 16 Active
2 2 150 17 Active
3 3 60 21 Left
4 4 125 16 Active
5 5 35 19 Active
I tried:
UPDATE table1 t1, table2 t2
SET t1.currency=t2.salary, t2.salary=t1.currency
WHERE t1.user_id=t2.user_id;
but this does not work (currency gets set correctly but not the salary), is it possible to do?
Swap two columns values between two tables looked like a possible solution but the solution is changing table names as all the columns need swopped whereas I only need single columns swapped.
I believe you'll need to use a mix of both DDL and DML to do this.
First off you'll need to rename one of the columns to be swapped and add a column to hold the new value:
alter table table1 change currency salary int;
alter table table1 add currency varchar(3) after salary;
then update each table independently:
update table1 t1, table2 t2
set t1.currency = t2.salary
where t1.user_id = t2.user_id;
update table1 t1, table2 t2
set t2.salary = t1.salary
where t1.user_id = t2.user_id;
and finally remove the extra column:
alter table table1 drop salary;

MySQL filter out subset by group by

1A
a b c
1 1 6
1 1 7
2 1 8
2 2 2
2 2 9
B
a b c
1 1 7
2 2 9
I want to filter out a subset of A
a b c
1 1 6
2 2 2
I am intend to join two tables by group by column a, b
such that to select the value in column c is less than the c value in table B, which is the desired subset.
But don't know how to implement this.
Try this:
SELECT A.* FROM A INNER JOIN B
ON A.a=B.b AND A.c<B.c;
See MySQL Join Made Easy tutorial.

Identical values in one column and unique value pairs

I have 2 columns
A B
1 2
2 2
1 2
3 2
5 2
0 2
4 2
11 4
12 4
11 4
I want the SQL query to return the pairs (A,B) where:
B has appeared 3 or more times
AND (A,B) is unique
The resulting table would be:
A B
1 2
2 2
3 2
5 2
0 2
4 2
You could use a join with a selected table grouped by B having count = 3
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*)= 3
) t2 on t2.b = t1.b
and for 3 or more
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*) >= 3
) t2 on t2.b = t1.b

Select FROM 3 Table

Well I think it's a simple question but I could not found the solution.
Well I have three Tables:
Table 1
id(AS t1id) Name LASTNAME Value
1 a z 50
2 b e 60
3 c k 70
4 d u 60
Table2
id idTable1 Name(AS t2me) Value(AS t2ve)
1 1 er 50
2 1 zx 150
3 2 zc 300
Table 3
id idTable1 Name(AS t3me) Value(AS t3ve)
1 2 erxc 50
2 2 zvvx 150
3 2 zcz 300
How to get this result with SQL
t1id Name LASTNAME t2me t2ve t3me t3ve
1 a z er 50 erdxc 150
1 a z zx 150
2 b e zc 300 erxc 50
2 b e zvvx 150
2 b e zcz 300
Is that possible? If not what could I do?
SELECT t1.id as t1id, t1.Name, t1.LASTNAME,
t2.Name as t2me, t2.Value as t2ve, t3.Name as t3me,
t3.Value as t3ve from Table1 t1
LEFT JOIN Table2 t2 on t1.id = t2.idTable1
LEFT JOIN Table3 on t3.idTable1 = t1.id