我有下面列出的查询,它来自PDO语句。除了collectionId = 3部分之外,查询中的所有内容都可以正常工作。它与其他汉堡人一起回归......
我已经盯着这个看了一会儿,看不出有什么不对,这一切对我来说都很好看?
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` = '3'
AND `Hat`.`colorId` LIKE '%||2||%'
OR `Hat`.`colorId` LIKE '2||%'
OR `Hat`.`colorId` LIKE '%||2'
OR `Hat`.`colorId` LIKE '2'
)
LIMIT 0 , 30
答案 0 :(得分:1)
你能试试吗,为OR分组添加了()
WHERE (
`Hat`.`published` =1
AND `Hat`.`collectionId` = '3'
AND ( `Hat`.`colorId` LIKE '%||2||%'
OR `Hat`.`colorId` LIKE '2||%'
OR `Hat`.`colorId` LIKE '%||2'
OR `Hat`.`colorId` LIKE '2'
)
)
答案 1 :(得分:1)
您的OR条件必须在括号中:
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` = '3'
AND (`Hat`.`colorId` LIKE '%||2||%'
OR `Hat`.`colorId` LIKE '2||%'
OR `Hat`.`colorId` LIKE '%||2'
OR `Hat`.`colorId` LIKE '2')
)
LIMIT 0 , 30
答案 2 :(得分:0)
您没有指定您想要过滤的具体内容,但您的问题很可能是条件的评估与您的想法不同 - 您如何期望OR和AND(没有括号将它们分组)评估?
现在,查询返回满足EITHER
的所有记录`Hat`.`published` =1 AND
`Hat`.`collectionId` = '3' AND
`Hat`.`colorId` LIKE '%||2||%' `
满足任何其他限制的OR:
OR `Hat`.`colorId` LIKE '2||%'
OR `Hat`.`colorId` LIKE '%||2'
OR `Hat`.`colorId` LIKE '2'
这一切都是将括号设置为正确的问题。 我的猜测是你想要所有已发布的记录= 1,collectionId = 3,以及你给出的colorIds之一。 为此,您必须相应地对其进行分组:
WHERE (
`Hat`.`published` =1
AND `Hat`.`collectionId` = '3'
AND
(
`Hat`.`colorId` LIKE '%||2||%'
OR `Hat`.`colorId` LIKE '2||%'
OR `Hat`.`colorId` LIKE '%||2'
OR `Hat`.`colorId` LIKE '2'
)
)