简体   繁体   中英

How would I constrain my CONCAT statement to just the id from the if statement in MySQL

I've been beating my head against the wall for a while now on this. I've tried following the thought behind Concat in If statement but I can't seem to figure out a way to make my specific need work. I'm now down to a syntax error in my CONCAT statement..'WHERE req.reqCreatedBy = '0' THEN 'Unknown' ELSE users.firstname,' ',users.lastn'. Could anybody give me some help on bringing the first and last name in on this query? I'm at a complete loss.

SELECT req.reqID as Id,
reqDesc.titleText as Title,
req.reqCity as City,
req.reqState as State,
req.areaID as Area,
area.areaname,
reqType.typeTitle as Type,
req.reqCreatedDate as Created,
req.reqEndDate as `End`, 
CONCAT((CASE WHERE req.reqCreatedBy = '0' THEN 'Unknown' ELSE users.firstname,' ',users.lastname END))
AS Recruiter
FROM apps_job_request as req
INNER JOIN apps_job_request_description as reqDesc
ON req.reqTitle = reqDesc.titleID
INNER JOIN apps_job_request_type as reqType
ON reqDesc.typeID = reqType.typeID
INNER JOIN `assemble_users`.area AS area
ON area.areaid = req.areaID
INNER JOIN `assemble_users`.users AS users
ON users.username = req.reqCreatedBy 
WHERE req.reqID is not null
AND req.reqActive = '1'

If check only one value req.reqCreatedBy , there may be simple IF statement, use CASE for multiple checked values.

SELECT req.reqID          as Id,
       reqDesc.titleText  as Title,
       req.reqCity        as City,
       req.reqState       as State,
       req.areaID         as Area,
       area.areaname,
       reqType.typeTitle  as Type,
       req.reqCreatedDate as Created,
       req.reqEndDate     as `End`,
       -- req.reqCreatedBy is '0' OR ''?
       IF(req.reqCreatedBy = '0', 'Unknown', CONCAT(users.firstname, ' ', users.lastname)) AS Recruiter
FROM apps_job_request as req
         INNER JOIN apps_job_request_description as reqDesc
                    ON req.reqTitle = reqDesc.titleID
         INNER JOIN apps_job_request_type as reqType
                    ON reqDesc.typeID = reqType.typeID
         INNER JOIN `assemble_users`.area AS area
                    ON area.areaid = req.areaID
         INNER JOIN `assemble_users`.users AS users
                    ON users.username = req.reqCreatedBy
WHERE req.reqID is not null
  AND req.reqActive = '1'

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