gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:01:55 25 4
gpt4 key购买 nike

我有一个包含产品特性数据的 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 查询,允许我搜索所有具有给定 feature_product_id 的“y”feature_status_yn 值的产品并返回 feature_product_id。目的是将其用作搜索工具,以允许我将结果过滤为仅与请求的功能集匹配的产品 ID。

SELECT feature_id FROM product_features WHERE feature_finder_id = '1' AND feature_status_yn = 'y' 的查询将返回给定产品的所有功能。但是,当它们位于不同的行时,如何选择所有具有“y”值的产品 (feature_product_id)?

多个查询可能是一种方法,但我想知道是否有更优雅的完全基于 SQL 的解决方案。

最佳答案

@马特

从 cms_finder_product_features 中选择 feature_product_id WHERE feature_finder_id = '1' AND (feature_id = '1' AND feature_status_yn = 'y') AND (feature_id = '2' AND feature_status_yn = 'y') AND (feature_id = '3 ' AND feature_status_yn = 'y') ;

Abovequery 从不返回任何内容或始终返回零行。您对 AND 和 OR 感到困惑。

查询应该如下所示

从 cms_finder_product_features 中选择 feature_product_id WHERE feature_finder_id = '1' AND ( (feature_id = '1' AND feature_status_yn = 'y') OR (feature_id = '2' AND feature_status_yn = 'y') OR (feature_id = ' 3' AND feature_status_yn = 'y') ) ;

你可以用更简单的方式使用

SELECT feature_product_id FROM cms_finder_product_features WHERE feature_finder_id = '1' AND feature_id in ('1', '2', '3') AND feature_status_yn = 'y' ;

关于mysql - 如何编写 MySQL 查询来检查多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2574372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com