好吧,这看起来很简单,但这让我很困惑,我在网上找不到我想要的东西。这可能真的很简单,我刚刚度过了漫长的一天,无法让它开始工作。
我有两张桌子: variant_detail,variant_setting
样本数据:
+--------------------------+-----------------------+
| variant_detail.vad_pd_id | variant_detail.vad_id |
+--------------------------+-----------------------+
| 3 | 3 |
| 18 | 25 |
| 3 | 69 |
| 5789 | 8954 |
| 89 | 254 |
| 1880 | 6987 |
| 246 | 879 |
| 5789 | 774 |
+--------------------------+-----------------------+
+----------------------------+------------------------------------------+
| variant_setting.vas_vad_id | variant_setting.vas_discontinued_product |
+----------------------------+------------------------------------------+
| 3 | TRUE |
| 25 | TRUE |
| 69 | FALSE |
| 8954 | TRUE |
| 254 | FALSE |
| 6987 | FALSE |
| 879 | FALSE |
| 774 | TRUE |
+----------------------------+------------------------------------------+我试图得到的只是行vad_pd_id,其中variant_setting.vas_discontinued_product = TRUE。
这不是一个标准的连接,因为不是所有的变体将停止。我试过各种不同的方法!例如,vad_pd_id =3将没有条目,因为一行为TRUE,另有一行为FALSE,但是,vad_pd_id = 5789将有一个条目,因为这两行都为真。
下面是我尝试过的代码:
Select distinct vad_pd_id From variant_detail Inner Join variant_setting On variant_detail.vad_id = variant_setting.vas_vad_id where variant_setting.vas_discontinued_product = TRUE
期待来自所有技术人员的帮助!
编辑
现在我需要更新一个单独的表: product_analysis。字段是pa_pd_id
所以代码就像
update update product_analysis set product_analysis.pa_l_9 =1 *using the select function just created
发布于 2018-04-17 15:39:33
参见bool_and聚合函数(文档)。
如果所有输入值都为真,则为true,否则为false。
您需要聚合vas_discontinued_product值并使用bool_and()。因此,删除WHERE条款:
where variant_setting.vas_discontinued_product = TRUE
并增加:
GROUP BY vad_pd_id
HAVING bool_and(variant_setting.vas_discontinued_product)并且您不必为布尔条件添加= TRUE。
编辑
事实证明,这不是PostgreSQL,您不能使用bool_and。但是有一种替代方法应该适用于(我希望)所有的SQL风格。
将boolean转换为int并使用min聚合函数。FALSE::int = 0和TRUE::int = 1,所以如果min返回1,就意味着所有的值都是TRUE。
GROUP BY vad_pd_id
HAVING min(CAST(variant_setting.vas_discontinued_product AS integer)) = 1发布于 2018-04-17 15:38:56
试试这个:
select vad_pd_id from variant_detail inner join variant_setting on variant_detail.vad_id = variant_setting.vas_vad_id group by vad_pd_id having bool_and(vas_discontinued_product) is true
它将返回值5789和18 -这是唯一有所有真相的.
https://stackoverflow.com/questions/49881822
复制相似问题