How to set sql field value equal to another value in the same table

I have a table that contains values ​​for different locations, where location id and the item number combination form the primary key . Many items are the same in both places when it comes to value, etc. I need to fix one location information that has somehow changed by the user. I am looking for advice on how to set the value for location 1 for item 1 to the same as location 2 item 1 . I know how to do this if the value is in another table, but I'm not sure how to do it when the values ​​are in the same table. Any advice would be greatly appreciated.

+4
source share
4 answers

I think you need this:

 UPDATE l1 SET COST = l2.COST FROM TableA l1 INNER JOIN TableA l2 ON l2.Location = 2 AND l1.item = l2.item WHERE l1.location = 1 and l1.item = 1 
+4
source

I think you are looking for this:

 UPDATE table SET col1 = col2 

Sets the value of "col1" to "col2" in all rows of the table "table"

+9
source

Is this what you want?

 UPDATE table t1 JOIN table t2 SET t1.col1 = t2.col1, t1.col2 = t2.col2, ... WHERE t1.item_number = 1 and t1.location_id = 1 AND t2.item_number = 1 and t2.location_id = 2 
+1
source

if you want the value of one column to be set in the same table, another value of the column, then use this query

 UPDATE table_name SET column_1 = column_2 

column_1 suppose it is empty in the whole record and column_2 this column you want to put the same value in column_1 then use the example below:

 UPDATE customer SET `blank_column` = `student_name` 

Run the query and blank_column can be filled in the same way as page_name

0
source

Source: https://habr.com/ru/post/1443689/


All Articles