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;
Related
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
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
I have table A:
id name product_id shipped
1 Apple 10 100
2 Orange 11 110
3 Banana 12 0
4 Mango 13 0
And Table B:
id product_id qty order_id
1 10 100 2
2 11 110 2
3 12 120 3
4 13 130 2
I need to update the quantity
UPDATE table_B SET qty=0 WHERE order_id=2
AND table_B.product_id = table_A.product_id
The table_A.product_id is get from a script. How can I do this? Thank you
UPDATE table_B b
LEFT JOIN table_A a
ON b.product_id = a.product_id
SET b.qty= 0 WHERE b.order_id=2
I'm trying to perform a UPDATE JOIN query in MySQL
I need to do the following: Add the table_1.won to table_2.total_winnings for a given session
+++ Table_1 +++
--id-- --name-- --selection-- -potential_winnings-- -- won -- --session--
1 John a 67 0 1
2 Jame b 10 **10** 1
3 David c 43 0 1
4 Sam b 20 **20** 1
5 Alex b 30 **30** 1
6 Rob b 1000 0 2
+++ Table_2 +++ (BEFORE)
--id-- --Total_winnings-- -- session --
1 4534 1
2 885 1
3 0 1
4 5 1
5 10 1
6 5465 2
My desired output is below
input : winning selection = b
session =1
+++ Table_2 +++ (AFTER)
--id-- --Total_winnings-- -- session --
1 4534 1
2 **895** 1
3 0 1
4 **25** 1
5 **40** 1
6 5465 2
I can do this by selecting each user from table_1 who has won and looping over there entry in table_2, but I have a large number of items to process now, so I think I need a join of somesort to accomplish this..
I'm currently doing
UPDATE table_2 SET Total_winnings = Total_winnings + 10 WHERE id = 2 AND session = 1
If anyone would know how to do this, or has a simple example of a SQL join with and UPDATE query that would be most useful. I have seen other examples of this, but I can never figure out what it going on in the SQL!!
You're looking for something like this?
UPDATE table_2
join table_1 on table_1.id = table_2.id
SET Total_winnings = Total_winnings + won
WHERE session = 1 and selection = 'b'
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.