在 MySQL 查询中查找索引值必须全部存在的行

Find rows in a MySQL query where the index values must all exist

我正在尝试查找存在于索引中的行,但是,即使有与我的索引匹配的行,我的结果仍然为空。我的查询逻辑有问题吗?

这是我的测试查询:

SELECT  `the_products` . * 
FROM  `the_products` 
INNER JOIN  `producttypes_index` ON  `producttypes_index`.`the_product_id` =  `the_products`.`id` 
AND  `producttypes_index`.`type_id` =  '1'
INNER JOIN  `producthashtags_index` ON  `producthashtags_index`.`the_product_id` =  `the_products`.`id` 
WHERE
`producthashtags_index`.`producthashtag_id` 
IN ('41')
AND  
`producthashtags_index`.`producthashtag_id` 
IN ('42')
AND 
`producthashtags_index`.`producthashtag_id` 
IN ('6')
ORDER BY updated_at DESC

在这里您可以看到 the_product_id 54433 存在于索引 table producthashtags_index 中使用查询:

SELECT * 
FROM  `producthashtags_index` 
WHERE  `the_product_id` =54433

结果:

id      producthashtag_id   the_product_id
25433   6                   54433
25434   41                  54433
25435   42                  54433

然后你可以看到它也存在于索引 table producttypes_index 中使用查询:

SELECT * 
FROM  `producttypes_index` 
WHERE  `the_product_id` =54433

结果:

type_id          the_product_id
1                54433

删除

AND  `producttypes_index`.`type_id` =  '1'

您还可以像这样将 3 个 IN 合并为一个来优化您的代码:

AND  `producthashtags_index`.`producthashtag_id` IN('42','41','6')