有三个表格如下:
shields: id | name ---------- 122 | Diagnosis monitors: id | name | sheild_id ------------------------- 12 | xxxx | 122 13 | yyyy | 122 14 | zzzz | 122 15 | aaaa | 125 entries: id | entry_type | state --------------------- 12 | MISS | DONE 13 | MISS | DONE 14 | MR. | DONE 18 | MISS | INPROGRESS 19 | MR. | DONE 24 | MS. | DONE
我需要一个查询,让我知道sheild_id
重复的entry_type
。基本上应该有唯一的entry_type
。在上述情况下,结果将是122.
帮助,非常感谢。
答案 0 :(得分:1)
假设monitors.id
链接到entries.id
,以下内容将返回给定条目类型具有多个条目的屏蔽:
select m.shield_id, e.entry_type
from monitors m join
entries e
on m.id = e.id
group by m.shield_id, e.entry_type
having count(*) > 1;
答案 1 :(得分:0)
试试这个:
select
s.id shield_id,
e.entry_type,
count(*) nr_duplicates
from
shields s
left outer join monitors m on m.sheild_id = s.id
left outer join entries e on e.id = m.id
GROUP BY s.id, e.entry_type
having count(*) > 1