MySQL 在 group by 子句上使用多个条件?
我有一个邻里表,由邻里 ID 和邮政编码组成。
+--------------+---------+
| Neighborhood | zipcode |
+--------------+---------+
| 1 | 12345 |
| 2 | 12346 |
| 3 | 12357 |
+--------------+---------+
有与邻域相关的 oneToMany Person 记录:
+--------+--------------+-----------+
| Person | neighborhood | eye color |
+--------+--------------+-----------+
| 1 | 1 | blue |
| 2 | 1 | grey |
| 3 | 1 | brown |
| 4 | 2 | blue |
| 5 | 2 | brown |
| 6 | 3 | hazel |
+--------+--------------+-----------+
我正在寻找至少有一个蓝眼睛的人且没有灰眼睛的人的邻域(在本例中,neighborhood=2 将满足条件)。
我将如何构建查询以按行中的邻域进行分组?
I have a table of Neighborhoods, comprised of a neighborhood_id and a zip_code.
+--------------+---------+
| Neighborhood | zipcode |
+--------------+---------+
| 1 | 12345 |
| 2 | 12346 |
| 3 | 12357 |
+--------------+---------+
There are oneToMany Person records that relate to a neighborhood:
+--------+--------------+-----------+
| Person | neighborhood | eye color |
+--------+--------------+-----------+
| 1 | 1 | blue |
| 2 | 1 | grey |
| 3 | 1 | brown |
| 4 | 2 | blue |
| 5 | 2 | brown |
| 6 | 3 | hazel |
+--------+--------------+-----------+
I am looking to identify neighborhoods where there is at least one person with blue eyes and no people with grey eyes (in this case, neighborhood=2 would meet the condition).
How would I structure the query to group by neighborhoods across rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很简单:
This is simply:
尝试:
演示
Try:
Demo