teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

追記

2018/10/27 03:40

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -11,4 +11,16 @@
11
11
  select 1 from posts inner join comments on posts.id=comments.post_id
12
12
  where posts.user_id=users.id
13
13
  )
14
+ ```
15
+ 追記
16
+ --
17
+ users の行を求めたいのだから、keyのレベルを合わせるのにはサブクエリーが必須になるんですよね。
18
+ 一応別パターン
19
+ ```SQL
20
+ select *
21
+ from users inner join (
22
+ select user_id from posts inner join comments on posts.id=comments.post_id
23
+ group by user_id
24
+ having count(*)=sum(case when comments.body='foo' then 1 else 0 end)
25
+ ) cond on cond.user_id=users.id
14
26
  ```

1

修正

2018/10/27 03:40

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -5,7 +5,7 @@
5
5
  from users
6
6
  where not exists(
7
7
  select 1 from posts inner join comments on posts.id=comments.post_id
8
- where posts.user_id=users.id and comments.body='foo'
8
+ where posts.user_id=users.id and comments.body<>'foo'
9
9
  )
10
10
  and exists(
11
11
  select 1 from posts inner join comments on posts.id=comments.post_id