mysql案例然后无法识别的关键字和无法识别的令牌

时间:2017-06-29 04:27:11

标签: php mysql phpmyadmin

我想了解为什么这个mysql查询在phpmyadmin版本上有错误:4.7.1。请参阅附件截图。我读了关于phpmyadmin版本的问题,然后我将我的phpmyadmin升级到最新但仍然在mysql中得到此错误。当我检查我有一个有效的sql时,它会说无法识别的关键字和无法识别的令牌。

select
      case 
        when venue like '% aklan %' then 'usa'
        when venue like '% italy %' then 'italy'
        when venue like '% china %' then 'china'
        when venue like '% india %' then 'india'
      end as cvenue,
      count(*) as population
    from 
        (
          select concat(' ', venue, ' ') as venue
          from tbl_trainings
        ) T
    group by cvenue

enter image description here

2 个答案:

答案 0 :(得分:3)

尝试将您的案例陈述放在括号中。然后执行查询:

select
      (case 
        when venue like '% aklan %' then 'usa'
        when venue like '% italy %' then 'italy'
        when venue like '% china %' then 'china'
        when venue like '% india %' then 'india'
      end) as cvenue,
      count(*) as population
    from 
        (
          select concat(' ', venue, ' ') as venue
          from tbl_trainings
        ) T
    group by cvenue

答案 1 :(得分:2)

尝试以下查询。这将为您提供相同的输出,并且性能会更好,因为您没有额外的INNER查询:

select
 (case when venue like '%aklan%' then 'usa'
   when venue like '%italy%' then 'italy'
   when venue like '%china%' then 'china'
   when venue like '%india%' then 'india'
 END) as cvenue,
count(*) as population
from tbl_trainings
group by cvenue;

即使您的contry名称在开始或结束时,您也不需要在开始和结束时附加一个额外的空格,然后使用LIKE。 LIKE将在没有它的情况下工作,因为%是通配符,这意味着0 or more characters