I'm trying to use LIKE inside an IF clause in mySQL and getting an error.
SELECT nc.web_name AS company_name,
IF((b.booth_number LIKE '%Cafe%', b.booth_number, substring(b.booth_number,4)) AS booth_number,
nc.site_url AS website, IF (nc.ismi_status = 1, 'Member','') AS member_status
FROM booth_sale bs JOIN {ismi.booth} b ON bs.booth_id = b.booth_id
JOIN ismi.new_people np ON bs.contact_id = np.id
JOIN ismi.new_company nc ON nc.id = np.company_id
WHERE b.show_event_id = 298 AND status IN(44,45) ORDER BY 3
The query works fine if I do this:
IF((b.booth_number = 'Cafe', b.booth_number, substring(b.booth_number,4)) AS booth_number
But I need to check for multiple possibilities - there could be Cafe, Cafe1, Cafe2, etc. and I want to return b.booth_number
for any record where that field value includes Cafe.
This is the error I get:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'AS booth_number, nc.site_url AS website, IF (nc.ismi_status = 1, 'Member','') ' at line 2: SELECT nc.web_name AS company_name, IF((b.booth_number LIKE '%Cafe%', b.booth_number, substring(b.booth_number,4)) AS booth_number, nc.site_url AS website, IF (nc.ismi_status = 1, 'Member','') AS member_status FROM {ismi.booth_sale} bs JOIN {ismi.booth} b ON bs.booth_id = b.booth_id JOIN {ismi.new_people} np ON bs.contact_id = np.id JOIN {ismi.new_company} nc ON nc.id = np.company_id WHERE b.show_event_id = 298 AND status IN(44,45) ORDER BY 3
Can anyone tell me what I'm doing wrong, or if there's another way to do this?