Update query using select statement of updating table - mysql

In Mssql i want to update a column col1 in table named Table1 using select query which returns col1 from Table1 and add 1 to it.
So i used the following code
update Table1 set col1=(select col1 from Table1) + 1;
But when i used it says you cannot target same table.Is there any ways to work out this function.
Please help to find it out.
Thanks in advsnce

you can do it like that
update Table1 set col1= col1 + 1;

Related

MySQL: Set a 2 variables from 2 columns from a single table

In the LOAD DATA documentation, there's a SET syntax which allows values to be assigned to column.
...
SET col=#variable;
...
I was wondering if the SET can support assignment from 2 columns from a single table, I can't find anything in the documentation that supports tuple assignment:
...
SET (col1, col2) = (SELECT col1, col2 from table where id=1);
...
Anyone have any knowledge on this? Thank you!
Set doesn't work like this, however I know that in TSQL at least you can do the same thing using select.
Select #var1 = col1, #var2= col2
from table

MySQL: how to update column using value before change

There is a table with three column: id, field1, field2.
And there is a row: id=1, field1=1, field2=1.
Run a update SQL: UPDATE my_table SET field1=field2+1, field2=field1+1 WHERE id=1;
I expected the result is: id=1, field1=2, field2=2. But in fact I got: id=1, field1=2, field2=3. Because when calculating field2=field1+1, the value of field1 has changed!
I figure out a SQL to solve this problem:
UPDATE my_table dest, (SELECT * FROM my_table) src
SET dest.field1=src.field2+1, dest.field2=src.field1+1
WHERE dest.id=1;
However I want to insert a record, and if the row was existed then do a update just like above.
INSERT INTO my_table (id, field1, field2) VALUES(1, 1, 1)
ON DUPLICATE KEY UPDATE
field1=field2+1, field2=field1+1;
This SQL has problem same as the first SQL. So how can I do this update using the value before change with ON DUPLICATE KEY UPDATE clause?
Thanks for any help!
Couldn't think of anything else but a temp variable. However, couldn't think of a way to make SQL syntax work, other than this:
set #temp = 0;
update test.test set
f1 = (#temp:=f1),
f1 = f2 + 1,
f2 = #temp + 1
where id = 1;
Hope this helps, and hope even more it helps you find a better way :)
I find a trick way to do this.
Use the IF clause to create temp variable. Field update use temp variable to calculate.
INSERT INTO my_table (id, f1, f2) VALUES(1, 1, 1)
ON DUPLICATE KEY UPDATE
id=IF((#t1:=f1 & #t2:=f2), 1, 1), f1=#t2+1, f2=#t1+1;
There is some point to notice:
The performance is a bit slow. Especially copy TEXT value to temp variable.
If field id need to use IF clause, the expr will be more complicated like:
((#t1:=f1 & #t2:=f2) || TRUE) AND (Your Condition)

Sum total of several MySQL columns stored in another column?

I have several columns in a MySQL database that I would like to add and store in another column:
column1 column2 column3 subtotal
10 10 10 30
Is there a way to accomplish this?
update yourtable set subtotal = col1 + col2 + col3
If you simply update subtotal, you have to maintain its value - ie every time one of the other columns is updated, your code has to remember to update subtotal too.
There are two ways to address this issue:
Create a trigger that fires when the row is updated. This option requires database "kung fu" and isn't immediately obvious to anyone else looking at the table the the trigger exists
Create a VIEW that does the calculation
I think option 2 is the best approach.
Here's how you code a view.
create view mytable_totalled as
select col1, col2, col3, col1 + col2 + col3 as subtotal
from mytable
Once created, the view can be used just like a table (with a few caveats), but you can certainly do any kind of select on it. The new column is calculated on the fly when selected
How about this?
UPDATE tableName SET subtotal = (column1 + column2 + column3)
Update 1
If you really don't need subtotal column in table just use query as
SELECT *, (column1 + column2 + column3) as subtotal
FROM tableName
Demo
Well if this is just a one time deal you could do an update query like so:
UPDATE MyTable set subtotal = (column1 + column2 + column3)
If you want it to be calculated on insert, you could have a calculated column.
I would personally just wait until you need it to calculate it so that information that could easily be derived from your data isn't bloating your DB.

MySQL update query on two tables but update only one table

I have two tables:
Table1: group_name, item_name, status
Table2: group_name, geo
I want to update table1. The default status is 0. I want to update the status of table1 to 1 using a single UPDATE statement.
I want to check for each row in table1 if the group_name exists in table2. If so, I will update status to 1.
I tried this but was not able to get the correct result.
UPDATE table1
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
How can I achieve my desired result?
you can use multiple update table syntax, so your query would be:
UPDATE table1,table2
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
You could use a multi-table update as others have shown. But you can also do it in a slightly simpler way with a single table update statement with a subselect:
UPDATE table1
SET STATUS = 1
WHERE group_name IN (SELECT group_name FROM table2)
Note also that you don't even need the precondition that status in all rows is initially set to zero. You can update all rows to their correct values in a single UPDATE statement:
UPDATE table1
SET STATUS = group_name IN (SELECT group_name FROM table2)
the SET is his command... the actual format is :
update "tablename"
set "columnname" =
"newvalue"
[,"nextcolumn" =
"newvalue2"...]
where "columnname"
OPERATOR "value"
[and|or "column"
OPERATOR "value"];
don't have a SQL database up and running, but I believe the UPDATE command is similar to the FROM command, so i think you have to do
UPDATE table1, table2 SET table1.'status' = 1
WHERE table2.group_name=table1.group_name

MySQL: Update all Columns With Values From A Separate Table

Sometimes if I want to quickly copy records from one table to another (that has the same structure) I use a query like this:
INSERT INTO table2 SELECT * FROM
table1 WHERE id = SOME_VALUE
How can I add a ON DUPLICATE KEY UPDATE to this statement? I tried this:
INSERT INTO SELECT * FROM table1 WHERE
id = 1 ON DUPLICATE KEY UPDATE SELECT
* FROM table1 WHERE id = 1
But I get an error. Is there away to accomplish the query above with out individually listing each column in the query?
P.S. Yes, I realize that it is not good practice to have multiple tables with identical structures, but sometimes you just don't get control over everything in the workplace!
The below UPDATES if there is no PK duplication and INSERTs is there is:
REPLACE INTO table2(field1, field2, field3)
SELECT field1, field2,field3 FROM table1
WHERE id=1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Just use the SELECT field_name from the other table like in dnagirls example