回答編集履歴
3
推敲
test
CHANGED
@@ -40,7 +40,7 @@
|
|
40
40
|
|
41
41
|
WHERE not exists(
|
42
42
|
|
43
|
-
slect 1 from comments
|
43
|
+
select 1 from comments
|
44
44
|
|
45
45
|
where company_id = r.company_id
|
46
46
|
|
@@ -88,7 +88,7 @@
|
|
88
88
|
|
89
89
|
WHERE not exists(
|
90
90
|
|
91
|
-
slect 1 from comments
|
91
|
+
select 1 from comments
|
92
92
|
|
93
93
|
where company_id = r.company_id
|
94
94
|
|
2
追記
test
CHANGED
@@ -54,4 +54,60 @@
|
|
54
54
|
|
55
55
|
```
|
56
56
|
|
57
|
+
インラインビューで、`comments`を最新のものだけにしたものと結合する方法もあります。
|
58
|
+
|
59
|
+
```SQL
|
60
|
+
|
61
|
+
SELECT r.id AS リクエストID
|
62
|
+
|
63
|
+
, r.company_id AS 企業ID
|
64
|
+
|
65
|
+
, r.post_id AS リストID
|
66
|
+
|
67
|
+
, r.text AS リクエストテキスト
|
68
|
+
|
69
|
+
, r.created_at AS 最新のリクエスト送信日
|
70
|
+
|
71
|
+
, c.id AS 企業ID
|
72
|
+
|
73
|
+
, c.company_name
|
74
|
+
|
75
|
+
, p.id AS リストID
|
76
|
+
|
77
|
+
, p.title
|
78
|
+
|
79
|
+
, p.category_id
|
80
|
+
|
81
|
+
, p.price
|
82
|
+
|
83
|
+
, p.tag
|
84
|
+
|
85
|
+
FROM (
|
86
|
+
|
87
|
+
select * from comments r
|
88
|
+
|
89
|
+
WHERE not exists(
|
90
|
+
|
91
|
+
slect 1 from comments
|
92
|
+
|
93
|
+
where company_id = r.company_id
|
94
|
+
|
95
|
+
and post_id = r.post_id
|
96
|
+
|
97
|
+
and created_at > r.created_at
|
98
|
+
|
99
|
+
)
|
100
|
+
|
101
|
+
) r
|
102
|
+
|
103
|
+
INNER JOIN companies c ON r.company_id = c.id
|
104
|
+
|
105
|
+
INNER JOIN posts p ON r.post_id = p.id
|
106
|
+
|
107
|
+
where r.company_id = :id
|
108
|
+
|
109
|
+
```
|
110
|
+
|
111
|
+
|
112
|
+
|
57
113
|
投稿テーブルを軸にすると、commentsテーブルで取得するのは`text`のみですから、それだけサブクエリーで取得する方法もあります。
|
1
追記
test
CHANGED
@@ -53,3 +53,5 @@
|
|
53
53
|
and r.company_id = :id
|
54
54
|
|
55
55
|
```
|
56
|
+
|
57
|
+
投稿テーブルを軸にすると、commentsテーブルで取得するのは`text`のみですから、それだけサブクエリーで取得する方法もあります。
|