Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update a column with derived value from another column in mysql

Tags:

sql

select

mysql

I am trying to update the value of a column from another column in the same tabl e- but this fails with " ERROR 1093 (HY000): You can't specify target table 'tab_1' for update in FROM clause "

What I have in Mysql

DT;                  date_custom
2012-10-31 17:00:22; 0
2012-09-31 17:00:21; 0
2012-07-31 17:00:25; 0
2012-10-31 17:43:56; 0
2012-11-31 17:44:09; 0

what I need in the corresponding date_custom field(column)

2012-10-31 
2012-09-31 
2012-07-31 
2012-10-31 
2012-11-31 

In other words, I just want Mysql to pick up the corresponding row for column DT and just DUMP the derived value date_column. This should be on a one-one basis. I do have a combination of keys that uniquely identify a row, but I don't want to use it if I can identify that.

Here's what I tried and did not work .

Before this I created this column - date_custom as below -:
alter table tab_1
add column date_custom int not null;

# simplistic
UPDATE tab_1 SET date_custom = (SELECT SUBSTRING_INDEX(DT," " ,1) FROM tab_1);

I am also aware that I can't modify a column at the same time, while trying to access that - but since this is different columns, things should not fail here, right - or what am I doing wrong ?

# using self joins on subquery
UPDATE tab_1
SET tab_1.date_custom =
(
    SELECT SUBSTRING_INDEX(a.DT," " ,1)
    FROM tab_1 a
    INNER JOIN tab_1 b on  
    a.DT = b.DT and a.AUCTION_ID_64=b.AUCTION_ID_64 # these 2 columns together make up the primary key, but I would like to avoid using this if possible
) # does not work

This corresponds to the thread here You can't specify target table for update in FROM clause

**From the official documentation - "In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:" **

DELETE FROM t WHERE ... (SELECT ... FROM t ...);
UPDATE t ... WHERE col = (SELECT ... FROM t ...);
{INSERT|REPLACE} INTO t (SELECT ... FROM t ...);

Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in the FROM clause. Example:
UPDATE t ... WHERE col = (SELECT * FROM (SELECT ... FROM t...) AS _t ...);
like image 512
ekta Avatar asked Jan 20 '14 06:01

ekta


People also ask

How do you update a column based on another column?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.

How update a column value with another column in the same table in MySQL?

The syntax to modify a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name MODIFY column_name column_definition [ FIRST | AFTER column_name ]; table_name. The name of the table to modify.

How do I replace one column with another value in SQL?

The Replace statement is used to replace all occurrences of a specified string value with another string value. The Replace statement inserts or replaces values in a table. Use the Replace statement to insert new rows in a table and/or replace existing rows in a table.


2 Answers

Use a SELF JOIN, like this:

UPDATE test t1, test t2 
SET t1.date_custom = SUBSTRING_INDEX(t2.dt," " ,1)
WHERE t1.id = t2.id

Working Demo: http://sqlfiddle.com/#!2/9b71cb/1/0

like image 90
Aziz Shaikh Avatar answered Sep 21 '22 12:09

Aziz Shaikh


Just one little modification to the accepted answer:

With a big table the SELF JOIN can take a lot of time to process

You can use UPDATE without SELF JOIN

UPDATE test t1
SET t1.date_custom = SUBSTRING_INDEX(t1.dt," " ,1);

That improve the performace DRASTICALLY

Tested a similar query with SELF JOIN 26000 rows not finish yet in two hours (imagine one with milions entryes !!! ) but without SELF JOIN the same query take less than 2 seconds

like image 22
MTK Avatar answered Sep 19 '22 12:09

MTK