使用包含多个 EXIST() 语句的查询计算索引存在的次数
Count the number of times an index exists using a query containing multiple EXIST() statements
我的查询根据这些产品是否存在于单独的 table 索引中来获取这些产品的结果。我正在尝试计算它们存在的所有实例,以便我可以按相关性对结果进行排序。我尝试的一切似乎 return 变量 @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 列表,因此您在其中键入的内容没有区别。这已记录在案 here。
使用联接的方法如下所示:
SELECT p.id,
COUNT(case when phi.product_id is not null then 1 end) AS instances
FROM products p
INNER JOIN productstypes_index pti ON pti.product_id = p.id AND pti.`_type_id` = 1
LEFT JOIN producthashtags_index phi ON phi.product_id = p.id AND phi.producthashtag_id IN (11,43)
GROUP BY p.id
ORDER BY instances DESC;
我已经删除了额外的反引号,我认为它们是不必要的,而且如果你的表中的 id
列是整数,你不需要引号。
您可以不用那些 exists
,也可以不用变量。此外,如果您在联接的 table 上有 exists
条件,则 left join
没有任何意义。那你还不如做更高效的inner join
,把额外的类型条件放在join条件里
可以通过哈希标签的计数来计算优先级,但仅限于具有 id in ('43', '11')
.
的标签
SELECT products.*
count(distinct producthashtags_index.producthashtag_id) priority
FROM products
INNER JOIN productstypes_index
ON productstypes_index.product_id = products.id
AND productstypes_index._type_id = '1'
INNER JOIN producthashtags_index
ON producthashtags_index.product_id = products.id
AND producthashtags_index.producthashtag_id in ('43', '11')
GROUP BY products.id
ORDER BY updated_at DESC;
我的查询根据这些产品是否存在于单独的 table 索引中来获取这些产品的结果。我正在尝试计算它们存在的所有实例,以便我可以按相关性对结果进行排序。我尝试的一切似乎 return 变量 @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 列表,因此您在其中键入的内容没有区别。这已记录在案 here。
使用联接的方法如下所示:
SELECT p.id,
COUNT(case when phi.product_id is not null then 1 end) AS instances
FROM products p
INNER JOIN productstypes_index pti ON pti.product_id = p.id AND pti.`_type_id` = 1
LEFT JOIN producthashtags_index phi ON phi.product_id = p.id AND phi.producthashtag_id IN (11,43)
GROUP BY p.id
ORDER BY instances DESC;
我已经删除了额外的反引号,我认为它们是不必要的,而且如果你的表中的 id
列是整数,你不需要引号。
您可以不用那些 exists
,也可以不用变量。此外,如果您在联接的 table 上有 exists
条件,则 left join
没有任何意义。那你还不如做更高效的inner join
,把额外的类型条件放在join条件里
可以通过哈希标签的计数来计算优先级,但仅限于具有 id in ('43', '11')
.
SELECT products.*
count(distinct producthashtags_index.producthashtag_id) priority
FROM products
INNER JOIN productstypes_index
ON productstypes_index.product_id = products.id
AND productstypes_index._type_id = '1'
INNER JOIN producthashtags_index
ON producthashtags_index.product_id = products.id
AND producthashtags_index.producthashtag_id in ('43', '11')
GROUP BY products.id
ORDER BY updated_at DESC;