mysql - laravel 子查詢的問題
問題描述
要拼成如下語句:
select field1,field2,field3 from table1 inner join (select id from table1 where field4=$field4 limit 100, 20) as temp using(id)
laravel 改怎么寫?···嘗試了許多都不行
感謝~
問題解答
回答1:直接用Laravel的ORM是無法實(shí)現(xiàn)的,至少是目前無法實(shí)現(xiàn),所以這種需求還是乖乖用DB類提供的原生SQL方式吧,比如用DB::select等。
也可以參考一下這個(gè)Laravel 5. Using the USING operator,跟你一樣的需求。
這里有一個(gè)pull請(qǐng)求,為L(zhǎng)aravel添加對(duì)using的支持,不過貌似因?yàn)闆]有添加相應(yīng)的單元測(cè)試被拒了。
回答2:$courses = Orgcourse::where(’classify’,’=’,$num)->join(’collections’,’orgcourses.id’,’=’,’collections.cid’)->select(DB::raw(’*,orgcourses.id as o_id,collections.id as c_id’))->orderBy(’collections.created_at’,’desc’)->paginate(2);類似于這種,能查出來,但是有點(diǎn)繁瑣好像,關(guān)聯(lián)查詢
相關(guān)文章:
1. python - beautifulsoup獲取網(wǎng)頁內(nèi)容的問題2. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題3. docker鏡像push報(bào)錯(cuò)4. docker - 如何修改運(yùn)行中容器的配置5. docker-machine添加一個(gè)已有的docker主機(jī)問題6. fragment - android webView 返回后怎么禁止重新渲染?7. dockerfile - [docker build image失敗- npm install]8. angular.js - 在終端中用yeoman啟用angular-generator報(bào)錯(cuò),求解?9. Android "1"=="1" 到底是true還是false10. android studio總是在processes running好久
