1

我正在尝试在 mySQL 的 IF 子句中使用 LIKE 并收到错误。

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

如果我这样做,查询工作正常:

IF((b.booth_number = 'Cafe', b.booth_number, substring(b.booth_number,4)) AS booth_number

但我需要检查多种可能性 - 可能有 Cafe、Cafe1、Cafe2 等,我想返回b.booth_number该字段值包括 Cafe 的任何记录。

这是我得到的错误:

PDOException: SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 2 行的 'AS stand_number, nc.site_url AS website, IF (nc.ismi_status = 1, 'Member','') ' 附近使用:SELECT nc. web_name AS company_name, IF((b.booth_number LIKE '%Cafe%', b.booth_number, substring(b.booth_number,4)) ASbooth_number, nc.site_url AS 网站, 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

谁能告诉我我做错了什么,或者是否有其他方法可以做到这一点?

4

1 回答 1

4

您的IF陈述有一个太多的左括号。删除第一个,你应该会很好(当然,等待任何其他 SQL 错误):

...
IF(b.booth_number LIKE '%Cafe%', b.booth_number, substring(b.booth_number,4)) AS booth_number, 
...
于 2012-09-27T20:17:04.720 回答