小编典典

SQL查询多个AND和OR不起作用

sql

我有一个单元格,其中包含用双管道分隔的值。我正在尝试使用以下内容搜索此单元格的内容,(其中10是要搜索的数字)%10、10%,%10%和10

我的查询似乎只返回10。没有其他变化。有人可以告诉我为什么它不起作用吗?

提前谢谢了。(您在下面看到的SQL查询是从准备好的PDO查询语句中导出的内容)

SELECT  `Hat`.`id` AS  `Hat_id` ,  `Hat`.`hatCode` AS  `Hat_hatCode` ,  `Hat`.`hatCodeOther` AS  `Hat_hatCodeOther` ,  `Hat`.`name` AS  `Hat_name` ,  `Hat`.`description` AS  `Hat_description` , `Hat`.`colorId` AS  `Hat_colorId` ,  `Hat`.`collectionId` AS  `Hat_collectionId` ,  `Hat`.`mainPicture` AS  `Hat_mainPicture` ,  `Hat`.`subPicture` AS  `Hat_subPicture` ,  `Hat`.`type` AS  `Hat_type` , `Hat`.`featured` AS  `Hat_featured` ,  `Hat`.`published` AS  `Hat_published` ,  `Hat`.`deleted` AS  `Hat_deleted` 
FROM  `modx_hats` AS  `Hat` 
WHERE (
(
`Hat`.`published` =1
AND  `Hat`.`collectionId` =  '1'
)
AND  `Hat`.`colorId` =  '%||10||%'
OR  `Hat`.`colorId` =  '10||%'
OR  `Hat`.`colorId` =  '%||10'
OR  `Hat`.`colorId` =  '10'
)
LIMIT 0 , 30

阅读 246

收藏
2021-04-28

共1个答案

小编典典

我认为您应该使用LIKE而不是=:

AND  `Hat`.`colorId` LIKE '%||10||%'
OR  `Hat`.`colorId` LIKE '10||%'
OR  `Hat`.`colorId` LIKE '%||10'
OR  `Hat`.`colorId` =  '10'
2021-04-28