Filling a mysql table combining data from two tables - mysql

I have two tables
Table 1 Table2
id_1 id_2 name
1 10 name1
2 20 name2
3
4
5
I need a query to get this another one, where for each id_1 the result gets as many registers as table 2 has.
Table3
id_3 id_1 id_2 name
1 1 10 name1
2 1 20 name2
3 2 10 name1
4 2 20 name2
5 3 10 name1
6 3 20 name2
7 4 10 name1
8 4 20 name2
9 5 10 name1
10 5 20 name2
Could you help me?
Thanks
EDIT:
Well, thanks to both of you.
Finally I got an easy solution.
SELECT * FROM table1 CROSS JOIN table2
I didnĀ“t know the CROSS JOIN operator. And that gives you the combination of each register from the first table with each register from the second.
Thanks again

Try with this:
INSERT INTO Table3 SELECT t1.id_1,t2.id_2,t2.name FROM Table1 t1, Table2 t2

This query should work:
insert into table3 (id_1,id_2,name) values select id_1,id_2,name from table1,table2;
I guess that id_3 is an autoincrement

Related

MySQL two (almost) identical table merge the values and default unique values

I have two almost identical MySQL tables besides a date column, and I would like to merge them with a select query to create a final table displaying both dates.
If there is a unique acc_id in one table -> i want to default the amount in the other to 0.
Here is an example of what i mean:
Table1:
id
acc_id
name
aug_amount
1
123
name1
100
2
456
name2
200
3
333
name3
300
Table2:
id
acc_id
name
sep_amount
1
123
name1
200
2
456
name2
300
3
444
name4
400
Merged table:
acc_id
name
aug_amount
sep_amount
123
name1
100
200
456
name2
200
300
333
name3
300
0
444
name4
0
400
Is there a way i can achieve this with a singular SQL query? I've been playing around for a while but i cant seem to crack it.
Help appreciated! Thanks for reading.
Ignoring the PK column, here is one solution where we union the two tables and then select from it:
select acc_id, name, sum(aug_amount) as aug_amount, sum(sep_amount) as sep_amount
from (
select acc_id, name, aug_amount, 0 as sep_amount
from table1
union
select acc_id, name, 0, sep_amount
from table2
)z
group by acc_id, name
order by name;
acc_id
name
aug_amount
sep_amount
123
name1
100
200
456
name2
200
300
333
name3
300
0
444
name4
0
400
View on DB Fiddle

select the min value of a id from other table

I want to select the min value of a id from other table
here my table1
id
grades
1
1
2
2
1
3
1
4
2
5
my table2
id
name
1
andy
2
lucy
3
kevin
I tried this
select table2.id, name, min(table1.grades) as grade from table1, table2
but it only shows 1 record
what I expected
id
name
grade
1
andy
1
2
lucy
2
3
kevin
0(or null?)
thanks
SELECT id, table2.name, MIN(table1.grades) grade
FROM table1
JOIN table2 USING (id)
GROUP BY 1,2;

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: Find "same" Data

I am currently in the need to find entrys matching the same pattern in a connection table.
The Table looks like
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
3 1 1 5
4 1 2 4
5 5 1 3
6 5 2 7
so my basic information is the data of job_id 15
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
I want to find job_id 5 because the data in ext_id and data1 is the same as in job 15. the data of job_id 1 differs, so I don't want to find that.
Any idea on how to do it?
I believe you want this:
select *
from your_table
group by data1,
ext_id
having count(*) > 1
This post explains it:
How to find duplicates in 2 columns not 1
EDIT
I believe this should return all rows that have mathcing data1 and ext_id values
select * from table t1
INNER JOIN table t2 ON t1.data1=t2.data1 and t1.ext_id=t2.ext_id

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.