質問編集履歴
2
追加情報対応
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,6 +5,12 @@
|
|
5
5
|
顧客データと紐づいている見積情報、請求書情報、を取得したいのですが、
|
6
6
|
|
7
7
|
下記のようにSQLを書くと見積情報が無い場合、請求情報を引くことができません。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
追記==========
|
12
|
+
|
13
|
+
顧客データは更に上位概念の会社IDと紐づいています。
|
8
14
|
|
9
15
|
|
10
16
|
|
@@ -20,7 +26,9 @@
|
|
20
26
|
|
21
27
|
where
|
22
28
|
|
23
|
-
customer.customer_id = 123456
|
29
|
+
#customer.customer_id = 123456
|
30
|
+
|
31
|
+
customer.company_id = 123456
|
24
32
|
|
25
33
|
```
|
26
34
|
|
@@ -44,7 +52,9 @@
|
|
44
52
|
|
45
53
|
where
|
46
54
|
|
47
|
-
customer.customer_id = 123456
|
55
|
+
#customer.customer_id = 123456
|
56
|
+
|
57
|
+
customer.company_id = 123456
|
48
58
|
|
49
59
|
```
|
50
60
|
|
@@ -72,7 +82,9 @@
|
|
72
82
|
|
73
83
|
where
|
74
84
|
|
75
|
-
customer.customer_id = 123456
|
85
|
+
#customer.customer_id = 123456
|
86
|
+
|
87
|
+
customer.company_id = 123456
|
76
88
|
|
77
89
|
```
|
78
90
|
|
@@ -85,3 +97,83 @@
|
|
85
97
|
顧客データに紐づく、見積情報と請求情報、どちらかがあれば取得できるようにするには
|
86
98
|
|
87
99
|
どうしたら良いのか教えていただけないでしょうか。
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
ここから追記==========
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
saziさんの
|
110
|
+
|
111
|
+
> 見積と請求を全外部結合(Full JOIN)した結果との結合にすれば良いかと思います。
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
上記コメントと頂いたSQLを元に、SQLを再構築しました。
|
116
|
+
|
117
|
+
私の使ってるMySQLだとFULL JOINできないのでUNION使わないとダメだそうです。
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```SQL
|
122
|
+
|
123
|
+
select q1.estimate_id,q1.invoice_id
|
124
|
+
|
125
|
+
from
|
126
|
+
|
127
|
+
(
|
128
|
+
|
129
|
+
select estimate.estimate_id,estimate.invoice_id
|
130
|
+
|
131
|
+
from customer
|
132
|
+
|
133
|
+
left join estimate
|
134
|
+
|
135
|
+
on (estimate.customer_id = customer.customer_id)
|
136
|
+
|
137
|
+
where
|
138
|
+
|
139
|
+
customer.company_id = ?
|
140
|
+
|
141
|
+
and
|
142
|
+
|
143
|
+
estimate.estimate_id is not null
|
144
|
+
|
145
|
+
union
|
146
|
+
|
147
|
+
select invoice.estimate_id,invoice.invoice_id
|
148
|
+
|
149
|
+
from customer
|
150
|
+
|
151
|
+
left join invoice
|
152
|
+
|
153
|
+
on (invoice.customer_id = customer.customer_id)
|
154
|
+
|
155
|
+
where
|
156
|
+
|
157
|
+
customer.company_id = ?
|
158
|
+
|
159
|
+
and
|
160
|
+
|
161
|
+
invoice.estimate_id is not null
|
162
|
+
|
163
|
+
) as q1
|
164
|
+
|
165
|
+
group by q1.estimate_id;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
上記SQLでこのスレッドの問題については上手く行ったのですが、
|
174
|
+
|
175
|
+
そもそも問題を間違えていることに今気が付きました。
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
皆さん、本当にありがとうございました。
|
1
タグを追加して欲しいと要望があったため
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|