db_sqls.html
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Thinkphp中SQL的写法 | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../gitbook/style.css">
<link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="../../Thinkphp/Others/functions.html" />
<link rel="prev" href="../../Thinkphp/Javascript/evshop_js.html" />
</head>
<body>
<div class="book"
data-level="1.5.1"
data-chapter-title="Thinkphp中SQL的写法"
data-filepath="Thinkphp/DB/db_sqls.md"
data-basepath="../.."
data-revision="Wed May 11 2016 00:54:11 GMT+0800 (中国标准时间)"
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="../../index.html">
<i class="fa fa-check"></i>
简介
</a>
</li>
<li class="chapter " data-level="1" >
<span><b>1.</b> Thinkphp</span>
<ul class="articles">
<li class="chapter " data-level="1.1" >
<span><b>1.1.</b> Structure</span>
<ul class="articles">
<li class="chapter " data-level="1.1.1" data-path="Thinkphp/Structure/evshop_structure.html">
<a href="../../Thinkphp/Structure/evshop_structure.html">
<i class="fa fa-check"></i>
<b>1.1.1.</b>
EVShop框架结构图
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2" >
<span><b>1.2.</b> Controller</span>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="Thinkphp/Controller/evshop_controller.html">
<a href="../../Thinkphp/Controller/evshop_controller.html">
<i class="fa fa-check"></i>
<b>1.2.1.</b>
EVShop之Controller篇
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" >
<span><b>1.3.</b> View</span>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="Thinkphp/View/evshop_view.html">
<a href="../../Thinkphp/View/evshop_view.html">
<i class="fa fa-check"></i>
<b>1.3.1.</b>
EVShop之View篇
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" >
<span><b>1.4.</b> Javascript</span>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="Thinkphp/Javascript/evshop_dialog.html">
<a href="../../Thinkphp/Javascript/evshop_dialog.html">
<i class="fa fa-check"></i>
<b>1.4.1.</b>
EVShop之Dialog篇
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="Thinkphp/Javascript/evshop_js.html">
<a href="../../Thinkphp/Javascript/evshop_js.html">
<i class="fa fa-check"></i>
<b>1.4.2.</b>
EVShop之Javascript篇
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" >
<span><b>1.5.</b> DB</span>
<ul class="articles">
<li class="chapter active" data-level="1.5.1" data-path="Thinkphp/DB/db_sqls.html">
<a href="../../Thinkphp/DB/db_sqls.html">
<i class="fa fa-check"></i>
<b>1.5.1.</b>
Thinkphp中SQL的写法
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" >
<span><b>1.6.</b> Others</span>
<ul class="articles">
<li class="chapter " data-level="1.6.1" data-path="Thinkphp/Others/functions.html">
<a href="../../Thinkphp/Others/functions.html">
<i class="fa fa-check"></i>
<b>1.6.1.</b>
Thinkphp中常用功能
</a>
</li>
<li class="chapter " data-level="1.6.2" data-path="Thinkphp/Others/UsefulCodes.html">
<a href="../../Thinkphp/Others/UsefulCodes.html">
<i class="fa fa-check"></i>
<b>1.6.2.</b>
EVShop中备用代码片段
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="../../" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="thinkphp中复杂sql的写法">Thinkphp中复杂SQL的写法</h1>
<h3 id="1-sql-多表联接的写法:">1. SQL 多表联接的写法:</h3>
<pre><code>$spc_common_default = D("spc_common_default");
$filter['S.spc_common_id'] = array('eq',71);
$model_list = $spc_common_default->table('__SPC_COMMON_DEFAULT__ S')
->join('LEFT JOIN __SPC_PGROUP_DEFAULT__ U ON S.spc_common_id = U.spc_common_id')
->field('S.*')
->where($map)
->select();
</code></pre><p>以上代码相当于以下SQL语句:</p>
<pre><code>SELECT S.* FROM ev_spc_common_default as S
LEFT JOIN ev_spc_group_default as U
ON S.spc_common_id=U.spc_common_id
WHERE s.spc_common_id=71
</code></pre><h3 id="2-sql-group-写法">2. SQL Group 写法:</h3>
<pre><code>$model_list = $this->table('__ALBUM_CLASS__ A')->join('LEFT JOIN __ALBUM_PIC__ B ON A.aclass_id=B.aclass_id')
->field('A.aclass_id,A.app_id,A.aclass_name,A.aclass_des,A.aclass_conver,A.aclass_dic,A.is_default,A.create_time,count(B.aclass_id) as ClassCount')
->where($condition)
->group('A.aclass_id')
->order('A.create_time')
->select();
</code></pre><h3 id="3-统计查询countsum">3. 统计查询(count,sum...)</h3>
<pre><code>$model->table('__ALBUM_CLASS__')->where($condition)->count();
$model->table('__ALBUM_CLASS__')->count('columnName');
$sumScore = $User->sum('score');
</code></pre>
</section>
</div>
</div>
</div>
<a href="../../Thinkphp/Javascript/evshop_js.html" class="navigation navigation-prev " aria-label="Previous page: EVShop之Javascript篇"><i class="fa fa-angle-left"></i></a>
<a href="../../Thinkphp/Others/functions.html" class="navigation navigation-next " aria-label="Next page: Thinkphp中常用功能"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="../../gitbook/app.js"></script>
<script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
<script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>