不久前,我参与了一个MS-SQL项目,我记得一个“IS IN”的东西。我在MySQL项目上尝试过它并没有用。
有同等的吗?解决办法:
以下是我尝试运行的完整查询:
SELECT *
FROM product_product, product_viewhistory, product_xref
WHERE
(
(product_viewhistory.productId = product_xref.product_id_1 AND product_xref.product_id_2 = product_product.id)
OR
(product_viewhistory.productId = product_xref.product_id_2 AND product_xref.product_id_1 = product_product.id)
)
AND product_product.id IS IN
(SELECT DISTINCT pvh.productId
FROM product_viewhistory AS pvh
WHERE pvh.cookieId = :cookieId
ORDER BY pvh.viewTime DESC
LIMIT 10)
AND product_viewhistory.cookieId = :cookieId
AND product_product.outofstock='N'
ORDER BY product_xref.hits DESC
LIMIT 10
这很大......但我感兴趣的部分是:
AND product_product.id IS IN
(SELECT DISTINCT pvh.productId
FROM product_viewhistory AS pvh
WHERE pvh.cookieId = :cookieId
ORDER BY pvh.viewTime DESC
LIMIT 10)
基本上说我希望产品位于该子查询的“前10名”中。
你如何通过MySQL实现这一目标(同时努力提高效率)?
答案 0 :(得分:15)
您正在寻找IN
:
AND product_product.id IN
(SELECT DISTINCT pvh.productId
FROM product_viewhistory AS pvh
WHERE pvh.cookieId = :cookieId
ORDER BY pvh.viewTime DESC
LIMIT 10)
答案 1 :(得分:2)
尝试IN
(不是“是”)