Thursday, 30 August 2018

How to update the table, with the selected values ​​of another table

I want to update the field TAG in accountinfo table with the value ABCD with results of a select like query in a diferent table of the same DB. The query doesn't return any errors, but neither update.

UPDATE accountinfo
SET TAG= 'ABCD'
WHERE 'ABCD%' IN (select NAME from hardware where NAME LIKE 'ABCD%');


select NAME from hardware where NAME LIKE 'ABCD%'

ABCDABELCHIOR
ABCDABELMARQUES
ABCDAG200CX1


The IN operator is an equality comparison. Your use of the '%' in the string literal makes it look as if you want to some sort of LIKE comparsion... matching the first four characters.
The predicate in the WHERE clause of your query is going to evaluate to either TRUE or FALSE, for every in the accountinfo table, no matter what the row in accountinfocontains. You're either going to update none of the rows, or all of the rows.
Which rows in accountinfo do you want to update?
Is there any "matching" of rows in accountinfo with rows in hardware? How are you going to know if the rows "match"?

EDIT:
There are no rows that match accountinfo in hardware.
So, the information we have to go on: there's a column named TAG in the accountinfotable. And that's the only column we know about.
There is a column named NAME in the hardware table. And there are rows in the table with NAME values that begin with 'ABCD'.
Again, which rows in accountinfo did you want to update? How are you going to identify which rows to update?

0 comments:

Post a Comment