這句話換成 連貫操作 ,對新手來說 很難,望高手指點
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)