简体   繁体   中英

mysql case then unrecognized keyword and unrecognized token

I want to understand why is this mysql query having error on phpmyadmin Version: 4.7.1.Please see attached screenshot. I read about problem with the version of phpmyadmin then I upgrade my phpmyadmin to the latest but still get this error in mysql. It says unrecognized keyword and unrecognized token when as I check I have a valid 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

在此处输入图片说明

Try to put your case statement in bracket. Then execute query:

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

Try below query. This will give you same output and will be better in performance as you have no extra INNER query:

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;

Even if your contry name is at start or at end, you don't need to append one extra space at start as well as at end and then use LIKE. LIKE will work without that because % is wildcard character which means 0 or more characters .

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-2025 STACKOOM.COM