代码之家  ›  专栏  ›  技术社区  ›  EmmyS

在mysql中的IF中使用LIKE[closed]

  •  1
  • EmmyS  · 技术社区  · 14 年前

    我试图在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服务器版本相对应,以便使用正确的语法 靠近AS booth_number,nc.site_url AS网站,IF(nc.ismi_status=1, 第2行的'Member','')':SELECT nc.web_name AS company_name, IF((b.booth_number LIKE“%Cafe%”,b.booth_number, 子字符串(b.booth_number,4))AS booth_nnumber,nc.site_url AS网站, 如果(nc.ismi_status=1,'Member','')AS Member_status FROM {ismi.booth_salel}bs加入{ismi.展位}b打开bs.booth_id=b.booth_id 加入{ismi.new_people}np ON bs.contact_id=np.id加入 {ismi.new_company}nc ON nc.id=np.company_id其中b.show_event_id= 298 AND status IN(44,45)ORDER BY 3(按3的顺序与状态)

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

    1 回复  |  直到 14 年前
        1
  •  4
  •   newfurniturey    14 年前

    你的 IF 语句的左括号太多。删除第一个,你应该会很好(当然,还有其他SQL错误):

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