![](/img/trans.png)
[英]In MySQL 5.7 how would I select all parent table rows with less than 2 related child table rows
[英]Mysql - Select rows which have less than x “child rows”
我有一張這樣的桌子:
planet_id | planet_name
1 | Test planet
2 | Test planet 2
我有第二張桌子
area_id | area_name | planet_id
1 | test_area | 1
我需要一個選擇查詢來僅選擇面積小於5的行星。 我怎么做?
您可以將按區域划分的行星分組並按行星分組。 然后您可以使用過濾
select one.planet_id, one.planet_name
from first_table one
join second_table two
on (one.planet_id = two.planet_id)
group by one.planet_id, one.planet_id
having count(two.area_id) < 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.