首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sql查询结果依赖于其他表的属性

sql查询结果依赖于其他表的属性
EN

Stack Overflow用户
提问于 2020-11-27 22:13:23
回答 1查看 44关注 0票数 0

汽车:

代码语言:javascript
复制
id name
1  Mercedes
2  Audi

CarAttributes:

代码语言:javascript
复制
id car_id attribute
1  1      fast
2  1      modern
3  1      fancy
4  1      green
5  2      fast
6  2      quiet
7  2      blue
8  2      old

如果返回结果依赖于汽车的属性,sql查询会是什么样子,如下所示:

代码语言:javascript
复制
[fast OR modern] AND [fancy OR old] = would return both cars
[fast OR modern] AND [old OR blue] = would only return the audi car
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-27 22:22:37

您可以将聚合与having子句一起使用:

代码语言:javascript
复制
select car_id
from carattributes
group by car_id
having sum( attribute in ('fast', 'modern')) > 0 and
       sum( attribute in ('fancy', old')) > 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65038797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档