【问题标题】:Using Like and Concat with Char Type对 Char 类型使用 Like 和 Concat
【发布时间】:2015-03-13 19:27:08
【问题描述】:

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

我的声明是:

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 字符串可能会变得非常大,但如果由于性能原因这是最佳选择,或者我试图做的事情无法以任何其他方式完成,那么我将改为那样做.

【问题讨论】:

    标签: mysql char sql-like concat varchar


    【解决方案1】:

    这个概念会起作用,但你是在倒退。您需要反转字段。

    类似这样的:

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

    【讨论】:

    • 愚蠢的我,我早该知道的。谢谢!
    猜你喜欢
    • 2019-06-15
    • 2011-09-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    相关资源
    最近更新 更多