如何编写MySQL查询来检查多行?

如何编写MySQL查询来检查多行?,mysql,Mysql,我有一个MySQL表,其中包含有关产品功能的数据: feature_id feature_product_id feature_finder_id feature_text feature_status_yn 1 1 1 Webcam y 2 1 1 S

我有一个MySQL表,其中包含有关产品功能的数据:

feature_id    feature_product_id    feature_finder_id    feature_text    feature_status_yn
1             1                     1                    Webcam          y
2             1                     1                    Speakers        y
3             1                     1                    Bluray          n
我想编写一个MySQL查询,允许我搜索给定功能产品id中具有“y”功能产品id的所有产品,并返回功能产品id。目的是将其用作一个搜索工具,允许我将结果筛选为仅与请求的功能集匹配的产品id

查询
SELECT feature\u id FROM product\u features,其中feature\u finder\u id='1'和feature\u status\u yn='y'
将返回给定产品的所有功能。但是,当它们位于单独的行上时,如何选择所有具有“y”值的产品(功能\u产品\u id)

多个查询可能是实现这一点的一种方法,但我想知道是否有一种纯粹基于SQL的更优雅的解决方案。

@matt

从cms_finder_product_features中选择feature_product_id='1'和(feature_id='1'和feature_status_yn='y')、和(feature_id='2'和feature_status_yn='y')、以及(feature_id='3'和feature_status_yn='y')

UpperQuery从不返回任何内容或始终返回零行。 你会被和和或弄糊涂

查询应如下所示

从cms_finder_product_features中选择feature_product_id='1'和((feature_id='1'和feature_status_yn='y')或(feature_id='2'和feature_status_yn='y')或(feature_id='3'和feature_status_yn='y')

您可以使用更简单的方法

从cms_finder_product_功能中选择功能_product_id,其中功能_finder_id='1'和功能_id in('1','2','3'),功能_status_yn='y'

@matt

从cms_finder_product_features中选择feature_product_id='1'和(feature_id='1'和feature_status_yn='y')、和(feature_id='2'和feature_status_yn='y')、以及(feature_id='3'和feature_status_yn='y')

UpperQuery从不返回任何内容或始终返回零行。 你会被和和或弄糊涂

查询应如下所示

从cms_finder_product_features中选择feature_product_id='1'和((feature_id='1'和feature_status_yn='y')或(feature_id='2'和feature_status_yn='y')或(feature_id='3'和feature_status_yn='y')

您可以使用更简单的方法


从cms_finder_product_功能中选择功能_product_id,其中功能_finder_id='1'和功能_id in('1','2','3'),功能_status_yn='y'

我认为马特是对的,但要理解你真正想要什么有点困难

如果要检索给定状态设置为“是”的产品列表,如果存在产品表(主键产品id):

选择不同的p.product\u id 来自产品p 在f.feature\u product\u id=p.product\u id上左连接特征f 其中f.feature_status_yn='y' 和f.feature_product_id='[ARG]'

马特的要求应该更快,我认为无论如何,因为你过滤功能加入之前

但是,如果您是sql新手,请尝试我的请求并首先尝试第一部分: 选择不同的p.product\u id 来自产品p 在f.feature\u product\u id=p.product\u id上左连接特征f


这样,您就可以看到表是如何连接的,以及如何利用这种连接…

我认为Matt是对的,但要理解您真正想要的是什么有点困难

如果要检索给定状态设置为“是”的产品列表,如果存在产品表(主键产品id):

选择不同的p.product\u id 来自产品p 在f.feature\u product\u id=p.product\u id上左连接特征f 其中f.feature_status_yn='y' 和f.feature_product_id='[ARG]'

马特的要求应该更快,我认为无论如何,因为你过滤功能加入之前

但是,如果您是sql新手,请尝试我的请求并首先尝试第一部分: 选择不同的p.product\u id 来自产品p 在f.feature\u product\u id=p.product\u id上左连接特征f


这样,您就可以看到您的表是如何连接的,以及如何利用此连接…

问题不清楚,标题和内容也不匹配。问题不清楚,标题和内容也不匹配。@sail谢谢!这正是我需要的。干杯从cms_finder_product_features中选择DISTINCT(feature_product_id),其中feature_finder_id='1'和feature_id位于('1','2','3'),feature_status_yn='y'@Salil谢谢!这正是我需要的。干杯从cms_finder_product_features中选择DISTINCT(feature_product_id),其中feature_finder_id为“1”,feature_id为('1','2','3'),feature_status_yn为“y”