下面是我的sql语法,如何在laravel中实现,谢谢。

SELECT * FROM `tr_policy_privacy` AS pri
LEFT JOIN
(SELECT * FROM tr_policy_full_create as cri WHERE cri.client_id=156 and cri.policy_type_id = 1)
AS hours
ON pri.id = hours.policy_template_edit

到目前为止,我已经尝试过这个方法,但是我正在试图找到一种在左连接中使用函数的方法。
DB::table('tr_policy_privacy AS pri')
    ->select(['cri.policy_title', 'cri.policy_type', 'cri.policy_std', 'cri.policy_version', 'cri.approved_user_id', 'cri.effective_date', 'cri.last_revised_date', 'cri.id'])
    ->leftJoin(
        DB::raw('(
        SELECT * FROM tr_policy_full_create as pol WHERE pol.client_id=156 and pol.policy_type_id = 1
        ) AS cri'),'pri.id', '=', 'cri.policy_template_edit'
    )
    ->get();

最佳答案

如果原始sql是正确的,那么尝试直接在laravel中执行

$sql = "
SELECT * FROM `tr_policy_privacy` AS pri
LEFT JOIN
(SELECT * FROM tr_policy_full_create as cri WHERE cri.client_id=156 and cri.policy_type_id = 1)
AS hours
ON pri.id = hours.policy_template_edit";

$result = DB::select($sql);

使用DB namespace到类的顶部
use DB;

关于php - 如何形成下面的mysql查询到laravel查询语法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45142042/

10-12 05:31