DB的用法的问题

将 sql 语句转成 DB 语句?

sql语句:
select top 10 * from (SELECT distinct field FROM testTable) newTable

注:SELECT distinct field FROM testTable 是一个db,不要一条直接执行
top这是要将数据进行分页
问:这条sql 怎么用DB的方法写?
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

老哥你是真的倔!

$sql = \DB::table("items")->select('id')->distinct();
\DB::table(\DB::raw("({$sql->toSql()}) as newTable"))->limit(10)->get();
4年前 评论
ytoz (楼主) 4年前
ytoz (楼主) 4年前
ytoz (楼主) 4年前
ytoz (楼主) 4年前
ytoz (楼主) 4年前
Constantine 4年前
讨论数量: 5
TestTable::fromSub(TestTable::select('field')->distinct(),'newTable')
    ->limit(10)
    ->get()
4年前 评论
ytoz (楼主) 4年前

模型大部分方法都是 DB 继承过去了,也就是模型支持的方法,大部分 DB 也支持!

4年前 评论
ytoz (楼主) 4年前
\DB::table(\DB::table("items")->select('id')->distinct(),'newTable')->limit(10)->get();

Laravel

4年前 评论
ytoz (楼主) 4年前
ytoz (楼主) 4年前

老哥你是真的倔!

$sql = \DB::table("items")->select('id')->distinct();
\DB::table(\DB::raw("({$sql->toSql()}) as newTable"))->limit(10)->get();
4年前 评论
ytoz (楼主) 4年前
ytoz (楼主) 4年前
ytoz (楼主) 4年前
ytoz (楼主) 4年前
ytoz (楼主) 4年前
Constantine 4年前