我的查询获取这些产品基于所述结果的查询的次数。我正在尝试对所有存在的实例进行计数,以便按相关性对结果进行排序。我所尝试的一切似乎都会将变量@priority返回为0.任何想法?计数索引存在使用包含多个存在()语句,如果他们在一个单独的表索引中存在
也许是更好地使用加入语句?
谢谢你的帮助。这里是我的MySQL查询:
SELECT `products` . * , @priority
FROM `products`
LEFT JOIN productstypes_index ON productstypes_index.product_id = products.id
WHERE (
EXISTS (
SELECT *
FROM `productstypes_index`
WHERE `productstypes_index`.`product_id` = `products`.`id`
AND `productstypes_index`.`_type_id` = '1'
)
AND (
(
(
EXISTS (
SELECT @priority := COUNT(*)
FROM `producthashtags_index`
WHERE `producthashtags_index`.`product_id` = `products`.`id`
AND `producthashtags_index`.`producthashtag_id` = '43'
)
)
AND (
EXISTS (
SELECT @priority := COUNT(*)
FROM `producthashtags_index`
WHERE `producthashtags_index`.`product_id` = `products`.`id`
AND `producthashtags_index`.`producthashtag_id` = '11'
)
)
)
)
)
ORDER BY `updated_at` DESC;
MySQL忽略EXISTS子查询中的SELECT列表,所以它在那里输入的内容没有区别。 –