简体   繁体   中英

Use CONCAT inside CONCAT in mysql

I have a stored procedure and a part of it is:

SET @TableAlias = " ls_flock AS _flk ";
SET @Select = CONCAT(
    " _flk.Id as Id, _flk.Code as Code, ",
    CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
    " as FlockManager"
  );
SET @Join = " JOIN employee as _em ON _flk.EmployeeId = _em.Id ";

I am unable to CONCAT two columns as a field with a CONCAT function.

And if I remove following line, code works fine:

CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
    " as FlockManager"

I'm getting this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.LastName as FlockManager FROM ls_flock AS _flk JOIN employee as _em ON _flk.' at line 1

I suspect that you want to generate a query string that uses CONCAT() - because I see that the table alias _em is declared further in the @Join variable. For this, you would need to move the function inside the query string.

SET @Select = CONCAT(
    " _flk.Id as Id, ",
    "_flk.Code as Code, ",
    "CONCAT(_em.FirstName, "" "", _em.LastName) s FlockManager"
;

And then, you just don't need the outer CONCAT() :

SET @Select = 
    " _flk.Id as Id, _flk.Code as Code, CONCAT(_em.FirstName, "" "", _em.LastName) as FlockManager";

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