thinkphp5获取当前对象拼接的sql

代码1

$data=db('table as tb1')
->join("table2 tb2","condition")
->where($where)
->field("  tb1.xx_sum , count(tb2.id) as tb2_num ")
->select();
#原生语句类似

 select  tb1.xx_sum , count(tb2.id) as tb2_num 
    join table2 as tb2 on ...
    where ...

现在遇到个问题 我这个操作里面我要wheretb2_num这个别名
类似这样where tb1.xx_sum/tb2_num > 0.54
最终语句写成
代码2

select * from
(
    select  tb1.xx_sum , count(tb2.id) as tb2_num 
    join table2 as tb2 on ...
    where ...
) as t
where  tb1.xx_sum/tb2_num > 0.54

请问代码1在执行select 之前 我能获得语句 比如的到$sql
然后再
代码3

select * from
(
    {$sql}
)
where  tb1.xx_sum/tb2_num > 0.54

试问有这样的方法吗

阅读 2.8k
1 个回答

直接原生sql 命名空间导入thinkphp5的数据库Db类,然后Db::query(sql);即可

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题