thinkphp5执行原生的sql查询语句

发布时间:2021-12-11 21:09:03 阅读:2131次

我们在查询数据库时

有时候可能需要直接用原生的sql来查询数据库,这样比较简单粗暴

以下为thinkphp5执行原生的sql语句

<?php
use think\Db;
...
...
...
$username = '13812345678';
$sql = "select sum(total_price) as total_price from (select sum(total_price) as total_price from goods_order where username='$username' and goods_type in(2,3) and pay_time>0  and pay_type!=3 union select sum(total_price) as total_price from goods_order_history where username='$username' and goods_type in(2,3) and pay_time>0 and pay_type!=3) a";

echo $sql;

$third_party = Db::name('user')->query($sql);

print_r($third_party[0]['total_price']);

...
...
...
?>

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:thinkphp5执行原生的sql查询语句 出自老鄢博客 | 欢迎分享