Laravel 中用 DB::写复杂的 sql 语句,遇到问题,问题是不会写了?
select cust_id,cust_name,cust_res,COUNT(cust_name) as num
FROM (select * from cust_table GROUP BY cust_name,cust_res)
GROUP BY cust_name HAVING (num<2 AND cust_res='n');
这个复杂sql语句用DB::怎么写??求教
关于 LearnKu
别沉啊
说实话,我感觉ORM的数据库引擎用起来比直接用查询构造器要舒服的多!
@Vlongen 求教用orm的写法,刚接触laravel
你可以使用'DB::Raw'来写,或者使用Eloquent吧,可以多看看Eloquent的文档,还是比较简单的,先从最简单的返回,然后再逐步增加内容。
@wanghan 你试下
你这个SQL 语法很有问题吧。
SELECT cust_id,cust_res,COUNT(cust_name) as
numFROM cust_table WHERE cust_res =‘n’ GROUP BY cust_name HAVINGnum<2\App\Model\CustTable::select(DB::raw('count(cust_name) as
num, cust_res,cust_id'))->where('cust_res','n')
->groupBy('cust_name')
->having('num','<',2)->get();
@Nocarefree @Kerwin @Epona 谢谢你们,问题已经解决了!!!