鍍金池/ 問(wèn)答/PHP/ thinkphp5獲取當(dāng)前對(duì)象拼接的sql

thinkphp5獲取當(dāng)前對(duì)象拼接的sql

代碼1

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

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

現(xiàn)在遇到個(gè)問(wèn)題 我這個(gè)操作里面我要wheretb2_num這個(gè)別名
類似這樣where tb1.xx_sum/tb2_num > 0.54
最終語(yǔ)句寫成
代碼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

請(qǐng)問(wèn)代碼1在執(zhí)行select 之前 我能獲得語(yǔ)句 比如的到$sql
然后再
代碼3

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

試問(wèn)有這樣的方法嗎

回答
編輯回答
不二心

直接原生sql 命名空間導(dǎo)入thinkphp5的數(shù)據(jù)庫(kù)Db類,然后Db::query(sql);即可

2018年8月2日 00:30