简体   繁体   中英

SQL CONCAT String + Column in WHERE clause

I got a numeric value in Foo.A and it has its equivalent in Bar but with a string prefix ("Z"). I'm trying to append the "Z" to the Bar.A col value . I also tried with CONCAT but without any success. This following codes returns "Unknown column Z".

UPDATE Foo, Bar
SET Foo.B = Bar.B
WHERE Foo.A = Z + Bar.A

For example 14 (Foo.A) = Z14 (Bar.A).

If your syntax works, then it is likely you are using MySQL. In any case, the problem is that you need quotes around string constants. So try this:

UPDATE Foo join
       Bar
       on Foo.A = concat('Z', Bar.A)
    SET Foo.B = Bar.B;

You should always use single quotes for string and date constants, regardless of the database. That is the ANSI standard and it reduced the possibility of error.

You are missing the single quotes around Z ie your code should be:

UPDATE Foo, Bar
SET Foo.B = Bar.B
WHERE Foo.A = CONCAT('Z', Bar.A);

Actually I found it. I missed the "" in CONCAT .

This is working:

UPDATE Foo, Bar
SET Foo.B = Bar.B
WHERE Foo.A = CONCAT("Z", Bar.A)

Where is you join statement you are using table B in the update statement and table A in the where condition. This is not gonna work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM