Skip to content
Advertisement

UPDATE statement using the same table in subquery

The results I get are

The autogenNumber is generated by first searching for other numbers that share the first three digits. Then 6 digits from that number are picked and another 4 random digits are subsituted to the end.

The above sql query generates the autogen number exactly as I need it.

However, now the issue arises when I want to update the column security_phrase in this list using the similar query below.

Gives me an error:

1093 – Table ‘vl1’ is specified twice, both as a target for ‘UPDATE’ and as a separate source for data

I have also tried

Not working and does not give the intended results…

Any help

Advertisement

Answer

MySQL does not allow referencing the table being updated again in another subquery, unless it is inside the FROM clause (Derived Table).

Now, in your particular case, we will need to put the complete SELECT query block as a Derived Table. As discussed in chat, lead_id is your Primary Key, so we will join back using the PK to update the rows accordingly.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement