1

我有 3 张桌子

  • 包含 2 列的事件: id ,事件名称
  • 具有 2 列的关系:event_id、cat_id
  • CATEGORY有 2 列:id、cat_name

这是我的数据:

[EVENT]
id           event-name
--------------------
1            dance
2            walk
3            run

[RELATION]
event_id     cat_id
--------------------
1            1
1            2
2            1
3            3

[CATEGORY]
id           cat_name
---------------------
1            slow
2            fast
3            very fast

我希望 EVENT 名称(在这种情况下为“ dance ” )内容 CATEGORY慢而

我尝试了什么:

Select * from EVENT e
left join RELATION r on e.id = r.event_id
left join CATEGORY c on r.cat_id = c.id
where c.cat_name = "slow" and c.cat_nam = "fast"
4

2 回答 2

1

您想要两者兼有的事件。尝试这个:

select e.event_name
from event e join
     relation r
     on e.id = r.event_id join
     category c
     on c.id = r.cat_id
group by e.event_name
having max(case when cat_name = 'slow' then 1 else 0 end) = 1 and
       max(case when cat_name = 'fast' then 1 else 0 end) = 1
于 2013-01-18T03:37:47.293 回答
0

另一种方法:

SELECT event_name
FROM event e
JOIN relation r      ON e.id = r.event_id
LEFT JOIN category c ON r.cat_id = c.id
WHERE cat_name='slow'
  AND EXISTS
  (SELECT 1
 FROM relation r
 JOIN category c ON r.cat_id=c.id
WHERE r.event_id = e.id
      AND c.cat_name='fast');
于 2013-01-18T04:33:40.490 回答