1

数据:

  1. 表格形状:shape_id、shape_name
  2. shape_forms:shape_form_id、shape_id、shape_form
  3. shape_form 可以是:0 - circle , 1 - square , 2 - triangle - 每个形状的数量是无限的

我需要 2 个查询来选择

  1. shape_forms 中包含圆形和三角形仅圆形但不包含正方形或仅三角形的所有形状
  2. 所有在 shape_forms 中只包含三角形的形状

请给我一些解决此任务的提示!我被限制不要对 shape_forms 使用“分组依据”,但如果没有适当的解决方案

4

1 回答 1

1

1.

select s.shape_id
from shapes s
inner join shape_forms sf on sf.shape_id = s.shape_id
group by s.shape_id
having 
(
   sum(shape_form = 1) = 0
   and sum(shape_form in (0,2)) >= 2
)
or sum(shape_form <> 0) = 0

2.

select s.shape_id
from shapes s
inner join shape_forms sf on sf.shape_id = s.shape_id
group by s.shape_id
having sum(shape_form <> 2) = 0
于 2012-11-30T17:32:02.043 回答