1

私は3つのテーブルを持っています

  • 2 列のEVENTS : id 、 event-name
  • 2 つの列を持つRELATION : event_id、cat_id
  • 2 列のCATEGORY : 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 slow AND fast

私が試したこと:

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 に答える