문제

my sql query is:

mysql> select emp_id where classes like '%'XII'%';

but i am getting this error:

ERROR 1064 (42000): 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 'where classes like '%'XII'%'' at line 1

I can understand that error is due to single quotes i have used in like arguments but i have string say 'X','XI','XII' in my database. that's why i am querying with single quote because if i query for class X in String 'XI','XII' then it will return emp_id of those who are teaching XI and XII standard which is undesirable.

Can i use escape characters for special characters in like argument.

도움이 되었습니까?

해결책

You can escape a single quote (') by doubling it (''):

select emp_id FROM my_table where classes like '%''XII''%';

Note: These are two single quotes, not a double quote.

다른 팁

Use double qoutes to enclose search pattern

select emp_id from tableName 
where classes like "%'XII'%";

Please dont store values as comma seprated strings , this is not a good database design practice. See why here , esp Lasse V. Karlsen answer

So instead of

empid   classes
1       'X','XI','XII'

store this as separate rows

empid   classes
1       'X'
1       'XI'
1       'XII'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top