I have a table say content table, having fields say
unitId | referenceId | contentId | masterContentId | sequence
Now I need to Insert on duplicate key update the values for sequence, referenceId fields with the values where masterContentId is say '10' and contentId is say '100', for the records where
unitId is say '500' and contentId is say '100'.
i.e.
rowId| unitId| referenceId | contentId | masterContentId | sequence|
-----+-------+-------------+-----------+-----------------+---------+
1 10 1 500 2 a
2 20 2 300 4 b
3 500 3 100 6 c
4 500 4 700 8 d
5 30 5 100 10 e
6 40 6 100 20 f
So, what I was trying to explain is that, I need to update row number 3, with the corresponding values of row 5.
How to do it, using Mysql Insert On duplicate Key Update
Related
hi I need to insert values into a new table (consist of 3 colunms) where I have a column named verad_id which has 250 values and now while inserting data i need to load all the values expect for 6 verad_id which has zero values for reage field.
example main table:-
verad_id reage value
998 0 38
8484 4 43
998 3 33
432 0 23
233 43 26
556 0 53
432 9 33
now i need to insert values expect for verad_id 998 and 432 which has reage value as 0
after inserting table should be like this
verad_id reage value
8484 4 43
998 3 33
233 43 26
556 0 53
432 9 33
I have tried this but not working
tried case 1:-
insert into finaltable
select
verad_id,
reage,
value
from main_table where verad_id = '998' or verad_id = '432' and reage != 0;
tried this but this query is not working
tried case 2:-
insert into finaltable
select
verad_id,
reage,
value
from main_table where verad_id in ( '998' , '432') and reage != 0;
in this case I was able to load data of verad_id '998' and '432' with reage not equal to 0 here i am missing data of other verad_id.
can any one please help me with this
To put it simple what you want to do is get grouped data from table main_table and each group sorted by the column reage in a DESC order.
Once you've got those results you wish to insert them into the table called finaltalbe
In this case you could use a subselect in which you define the order in which the results should be displayed, then select a group around that and MySQL will show the values of the first row in each grouped set.
INSERT INTO finaltable
SELECT * FROM (SELECT * FROM main_table ORDER BY `reage` DESC) AS `sorted` GROUP BY `verad_id`;
this will result in the table finaltable with the following data
| verad_id | reage | value |
|----------|-------|-------|
| 233 | 43 | 26 |
| 432 | 9 | 33 |
| 556 | 0 | 53 |
| 998 | 3 | 33 |
| 8484 | 4 | 43 |
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;
So we got four tables T1,T2,T3,T4
Each table has four columns, id,1,x,2 (id is primary key auto incremented in all tables) and all of them are populated with numbers (40-50 rows each).
How can I compare the first field of each table and column (and same for all the remaining fields) and display the maximum field for each column and its id?
For example, compare column 1/field 1 of T1 with column 1/field 1 of T2 and column 1/field 1 of T3 display the maximum and its id and then do the same for column 2/field 1 OF T1/T2/T3/T4/ etc etc and each of their respective fields for all 40 rows ?
I have read about UNION and JOINS but I don't know how to do this.
Any help would be appreciated.
TABLE 1
ID | 1 | X | 2 |
1 10 20 30
2 5 45 6
3 3 11 12
4 0 14 23
TABLE 2
ID | 1 | X | 2 |
1 100 200 300
2 50 405 60
3 30 101 102
4 0 104 203
ETC ETC
we need to compare the 10 of table 1 column 1 row 1 id=1 with 100 of table 2 row/column/id=1 then the same for x and 2 column.
I have a table that is having 3 columns
vid - auto increment column
video_id - containing numbers
a_id - containing junk numbers
The table looks like below.
Vid Video_id a_id
101 1 3
102 1 3
103 5 3
104 5 3
105 5 3
106 11 3
107 11 3
108 11 3
109 11 3
110 11 3
I want to update a_id column values based on video_id values. Values in a_id should be updated as below.ex: If there are five 11 digit in video_id then the value in a_id should be updated 1 through 5.
Vid Video_id a_id
101 1 1
102 1 2
103 5 1
104 5 2
105 5 3
106 11 1
107 11 2
108 11 3
109 11 4
110 11 5
You can use user defined variables to give rank for each video group and then join with your real table by your auto increment column and update a_id accordingly
update t
join (
SELECT
Vid,
#r:= CASE WHEN Video_id = #g THEN #r+1 ELSE #r:=1 END a_id
,#g:=Video_id
FROM t,(SELECT #r:=0,#g:=0) t1
ORDER BY Video_id
) t1
on(t.Vid =t1.Vid)
set t.a_id = t1.a_id
Demo
Table a
Rowid Msgid Userid
1 3 55
2 3 56
3 3 57
4 4 55
5 4 56
Table Group
RowID GroupID UseriD
1 2 55
2 2 56
3 2 57
4 2 58
5 2 59
6 2 60
7 3 60
8 3 55
Here there is a table a and group table. Rowid primary keys
I want to insert rows into table a
This query will
Insert rows into table a i.e for msgid 3 there is already 55 56 57 so it has to insert only 58 59 60.
Insert into
table a (msgid,Userid)
values(#msgid,#userid)
where userid not in table a
where tbl_a.msgid=3
and tbl_group.groupid = 2
For Msgid 3 I want to check if there are any group members (groupID 2) associated in table a, if not then add a row to it.
ie add to table a
rowid Msgid Userid
6 3 58
7 3 59
8 3 60
So I wont insert the userid 55,56,57 because it is already in the table a for msgid 3. How to do a query for this scenario
Try below code
Insert IGNORE into
table a (msgid,Userid)
values(#msgid,#userid)
where userid not in table a
where tbl_a.msgid=3
and tbl_group.groupid = 2
I am considering that your insert query is correct...
Good Luck!!!
It's actually quite simple:
INSERT INTO TABLE_GROUP
SELECT * FROM TABLE_A
WHERE ... -- you can have or not have a where clause as you like
ON DUPLICATE KEY UPDATE;