db_sqls.html 11.7 KB
<!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&#x4E2D;&#x590D;&#x6742;sql&#x7684;&#x5199;&#x6CD5;">Thinkphp&#x4E2D;&#x590D;&#x6742;SQL&#x7684;&#x5199;&#x6CD5;</h1>
<h3 id="1-sql-&#x591A;&#x8868;&#x8054;&#x63A5;&#x7684;&#x5199;&#x6CD5;&#xFF1A;">1. SQL &#x591A;&#x8868;&#x8054;&#x63A5;&#x7684;&#x5199;&#x6CD5;&#xFF1A;</h3>
<pre><code>$spc_common_default = D(&quot;spc_common_default&quot;);
$filter[&apos;S.spc_common_id&apos;] = array(&apos;eq&apos;,71);
$model_list  = $spc_common_default-&gt;table(&apos;__SPC_COMMON_DEFAULT__ S&apos;)
    -&gt;join(&apos;LEFT JOIN __SPC_PGROUP_DEFAULT__ U ON S.spc_common_id = U.spc_common_id&apos;)
    -&gt;field(&apos;S.*&apos;)
    -&gt;where($map)
    -&gt;select();
</code></pre><p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x76F8;&#x5F53;&#x4E8E;&#x4EE5;&#x4E0B;SQL&#x8BED;&#x53E5;&#xFF1A;</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-&#x5199;&#x6CD5;">2. SQL Group &#x5199;&#x6CD5;:</h3>
<pre><code>$model_list = $this-&gt;table(&apos;__ALBUM_CLASS__ A&apos;)-&gt;join(&apos;LEFT JOIN __ALBUM_PIC__ B ON A.aclass_id=B.aclass_id&apos;)
    -&gt;field(&apos;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&apos;)
    -&gt;where($condition)
    -&gt;group(&apos;A.aclass_id&apos;)
    -&gt;order(&apos;A.create_time&apos;)
    -&gt;select();
</code></pre><h3 id="3-&#x7EDF;&#x8BA1;&#x67E5;&#x8BE2;countsum">3. &#x7EDF;&#x8BA1;&#x67E5;&#x8BE2;(count,sum...)</h3>
<pre><code>$model-&gt;table(&apos;__ALBUM_CLASS__&apos;)-&gt;where($condition)-&gt;count();
$model-&gt;table(&apos;__ALBUM_CLASS__&apos;)-&gt;count(&apos;columnName&apos;);
$sumScore = $User-&gt;sum(&apos;score&apos;);
</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>