Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 718
Wistar Avatar asked Oct 27 '25 12:10

Wistar


2 Answers

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.

like image 91
Gordon Linoff Avatar answered Oct 30 '25 01:10

Gordon Linoff


You are missing the single quotes around Z i.e. your code should be:

UPDATE Foo, Bar
SET Foo.B = Bar.B
WHERE Foo.A = CONCAT('Z', Bar.A);
like image 42
Zeno Avatar answered Oct 30 '25 03:10

Zeno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!