thinkphp3.2.3 連慣 寫法

來源:互聯網
上載者:User
關鍵字 php
這句話換成 連貫操作 ,對新手來說 很難,望高手指點

SELECT * FROM `hx_shop_period` WHERE ( id in ( SELECT DISTINCT  `pid` FROM `hx_shop_record` WHERE ( uid=101600 ) ORDER BY create_time desc  ) ) ORDER BY state asc,end_time desc LIMIT 0,20

回複內容:

這句話換成 連貫操作 ,對新手來說 很難,望高手指點

SELECT * FROM `hx_shop_period` WHERE ( id in ( SELECT DISTINCT  `pid` FROM `hx_shop_record` WHERE ( uid=101600 ) ORDER BY create_time desc  ) ) ORDER BY state asc,end_time desc LIMIT 0,20

//擷取一個sql語句而不執行$sub_query = M()->table('hx_shop_record')->distinct(true)->field('pid')->where(['uid'=>101600])->order('create_time DESC')->buildSql();//使用子查詢作為條件,擷取結果$rst = M()->table('hx_shop_period')->where('id in ' .$sub_query)->order('state ASC,end_time DESC')->limit(0,20)->select();

在主流的資料庫操作類庫中都會有連貫操作的功能,其實並不麻煩,反而還非常好用.好處有:
1.不用記sql子語句的順序
2.避免注入風險
3.封裝了一些常用的方法,使用簡單
4.多次拼裝最後執行,可以組織複雜的語句

$res = $model->field('pid')->where('uid=101600')->order('create_time desc')->select(false);
$where['id'] = array('IN',$res)
$this->where($where)->order('state asc,end_time desc')->limit(0,20)->select();

好吧,其實我也沒碰到這種,上面是看文檔憑經驗大概寫的。
我直接推薦你用 $Model->query(原生SQL)

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.