使用具有字符类型的Like和Concat

时间:2015-03-13 19:27:09

标签: mysql char sql-like concat varchar

我有一个MySQL语句试图查找行(CHAR(36))的ID是否在字符串内。

我的陈述是:

select * from `options` 
 where concat('%',`OptionID`,'%') 
 like '{\"4d519a3d-c99b-11e4-8eda-b8ca3a83b4c8\":\"62.50\"}';

但是这个失败并且没有返回任何东西,而这个

select * from `options` where
 `OptionID`='4d519a3d-c99b-11e4-8eda-b8ca3a83b4c8';

工作并返回我的行。我认为这是因为ID字段是CHAR字段而不是VARCHAR字段所以我尝试了这个

select * from `options` where 
 concat('%',cast(`OptionID` as char(38)) ,'%') 
 like '{\"4d519a3d-c99b-11e4-8eda-b8ca3a83b4c8\":\"62.50\"}';

但那仍然没有奏效,现在我迷失了。我想避免迭代json字符串并创建一组条件,如

(`OptionID`={$JsonKey[0]} OR `OptionID`={$JsonKey[1]} OR ... )

由于这些json字符串可能会变得非常大,但如果因为性能或我试图做的最佳选择不能以任何其他方式完成,那么我就会这样做就这样做吧。

1 个答案:

答案 0 :(得分:1)

这个概念会奏效,但你会倒退。你需要反转字段。

这样的事情:

select * 
from `options` 
where '{\"4d519a3d-c99b-11e4-8eda-b8ca3a83b4c8\":\"62.50\"}'   
      like concat('%',`OptionID`,'%') 
相关问题