MySQL“IS IN”等价于?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2649811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:48:04  来源:igfitidea点击:

MySQL "IS IN" equivalent?

sqlmysqlsql-server

提问by Nathan H

A while ago I worked on a MS-SQL project and I remember a "IS IN" thing. I tried it on a MySQL project and it did not work.

不久前,我参与了一个 MS-SQL 项目,我记得“IS IN”这件事。我在 MySQL 项目上尝试过,但没有奏效。

Is there an equivalent? Workaround?

有等价物吗?解决方法?

Here is the full query I am trying to run:

这是我尝试运行的完整查询:

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

It's pretty big ... but the part I am interested in is:

它相当大......但我感兴趣的部分是:

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)

Which basically says I want the products to be in the "top 10" of that sub-query.

这基本上是说我希望产品在该子查询的“前 10 名”中。

How would you achieve that with MySQL (while trying to be efficient)?

您将如何使用 MySQL 实现这一目标(同时努力提高效率)?

回答by davidtbernal

You're looking for IN:

您正在寻找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)

回答by nickf

Try IN(no "is")

尝试IN(没有“是”)