回答編集履歴
2
インデックス
answer
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
こんな感じでしょうか?(あれ?既存の回答と同じかな?)
|
2
2
|
- 元データ
|
3
3
|
```SQL
|
4
|
-
create table users(id int primary key,name varchar(10));
|
4
|
+
create table users(id int primary key,name varchar(10),index(name));
|
5
5
|
insert into users values(1,'userA'),(2,'userB'),(3,'userC'),(4,'userD'),(5,'userE');
|
6
|
-
create table posts(id int primary key,user_id int);
|
6
|
+
create table posts(id int primary key,user_id int,index(user_id));
|
7
7
|
insert into posts values(1,1),(2,2),(3,3),(4,4);
|
8
|
-
create table comments(id int primary key,post_id int,body varchar(30));
|
8
|
+
create table comments(id int primary key,post_id int,body varchar(30),index(post_id,body));
|
9
9
|
insert into comments values(1,1,'foo'),(2,1,'bar'),(3,2,'foo'),(4,2,'foo'),(5,3,'foo');
|
10
|
-
|
11
10
|
```
|
12
11
|
※userEはpostsがない
|
13
12
|
※userDはpostsがあるけどpostsがcommentsを持っていない
|
1
修正
answer
CHANGED
@@ -11,15 +11,17 @@
|
|
11
11
|
```
|
12
12
|
※userEはpostsがない
|
13
13
|
※userDはpostsがあるけどpostsがcommentsを持っていない
|
14
|
-
※
|
14
|
+
※userAはcommentsがfooを含んでいないものを持っている
|
15
15
|
|
16
16
|
- 検索
|
17
17
|
```SQL
|
18
18
|
select * from users as t1
|
19
19
|
inner join posts as t2 on t1.id=t2.user_id
|
20
|
-
and
|
20
|
+
and exists(
|
21
|
-
select 1 from comments
|
21
|
+
select 1 from comments
|
22
|
-
group by
|
22
|
+
group by post_id
|
23
|
+
having sum((body regexp 'foo'))=count(*)
|
23
|
-
|
24
|
+
and post_id=t2.id
|
24
|
-
)
|
25
|
+
);
|
25
|
-
```
|
26
|
+
```
|
27
|
+
※ロジックがおかしかったので修正しました
|