mysql选择结果与LIKE和多个零(%00%)混淆

时间:2019-01-10 08:10:39

标签: mysql

我创建了一个搜索查询,以在表中搜索多个字段中的字符串。它适用于每个字符串,除非该字符串仅包含一个或多个零。例如:%000%返回具有2个或更少零的行。

搜索字段:

`par_partno` varchar(20) utf8_general_ci
`par_group_id` varchar(16) utf8_bin
`par_desc` text utf8_general_ci
`par_details` text utf8_general_ci

我已经尝试将par_partno强制转换为char CAST(par_partno AS CHAR),但结果是相同的。

查询:

select `par_id`, `pag_id`, `par_partno`, `par_group_id`,`par_cond`, `par_desc`,`par_price`,
`par_stock`, `par_feature`, `par_weight`, `par_details`, `par_related`, `par_meta`,
`par_picture`, `par_lastmod`
from `parts`
where concat(`par_partno`, `par_group_id`, `par_desc`, `par_details`) like "%000%"
order by `par_group_id` asc 

错误的结果:

[
{"par_id":"145100","pag_id":"1","par_partno":"7101263500","par_group_id":"01-00-12","par_cond":"New","par_desc":"Pedal Assy W\/ Booster Mbc, Clutch Cylinder","par_price":"1450.00","par_stock":"1","par_feature":"0","par_weight":"0.1","par_details":"","par_related":null,"par_meta":null,"par_picture":"0","par_lastmod":"2019-01-04 18:14:12"}, 
{"par_id":"145106","pag_id":"2","par_partno":"7121051100","par_group_id":"01-01-102","par_cond":"New","par_desc":"Seal Intake Valve","par_price":"5.95","par_stock":"1","par_feature":"0","par_weight":"0.1","par_details":"","par_related":"[\"7121040061\"]","par_meta":null,"par_picture":"0","par_lastmod":"2019-01-04 18:14:12"}, 
{"par_id":"145169","pag_id":"2","par_partno":"7122015100","par_group_id":"01-01-91","par_cond":"New","par_desc":"Engine Gasket Set","par_price":"199.00","par_stock":"1","par_feature":"0","par_weight":"6.4","par_details":"","par_related":null,"par_meta":null,"par_picture":"0","par_lastmod":"2019-01-04 18:14:12"}
]

3 个答案:

答案 0 :(得分:2)

我可能会误解您的问题,但是检查的字符串不是例如的串联吗?

  

par_partno”:“ 7101263500”,“ par_group_id”:“ 01-00-12”

如此

  

710126350001-00-12

其中包含一个“ 000”子字符串。

答案 1 :(得分:0)

提示帮助我解决了这个问题。

我删除了concat并替换为:

WHERE LOWER(par_partno) LIKE "%000%" OR LOWER(par_group_id) LIKE "%000%" 
OR LOWER(par_desc) LIKE "%000%" OR LOWER(par_details) LIKE "%000%"

答案 2 :(得分:0)

[GNU]: Variables Used by Implicit Rules的改进:

(太大,无法接受评论)

通过将搜索到的列合并为一个CONCATenation,仅运行一个LIKE查询。

HAVING CONCAT_WS('-',par_partno,par_group_id,par_descpar_details) LIKE "%000%" 

此函数将所有选中的列组合为一个字符串,并用-(不属于LIKE的任何值)将它们分开。

这也删除了LOWER()函数,因为在处理数字时它是不必要的。