Laravel数据库原始连接替换

时间:2018-08-31 22:02:46

标签: mysql laravel

在我的代码下方,我遇到了问题

$data = client_conf::select('cont_id','default_cont_id')->where('client_id',$client_id)->first();
$cont_id = explode(',', $data->cont_id);
$res_ = "'".implode("','", $cont_id)."'";

$custom2 = DB::table('client_conf as a')->where('client_id',$client_id)
    ->select(DB::raw("
        a.cont_id as id_list,
        a.default_cont_id id_default,
        (SELECT bank_fee_trans_inter FROM payout_cont as t WHERE t.cont_id=a.default_cont_id) as fee_trans_inter,
        (SELECT bank_fee_skn FROM payout_cont as t WHERE t.cont_id=a.default_cont_id) as fee_skn,
        (SELECT bank_fee_skn FROM payout_cont as t WHERE t.cont_id=a.default_cont_id) as fee_rtgs,
        (SELECT bank_fee_trans FROM payout_cont as t WHERE t.cont_id in ($res_) and t.bank_cd='$bank_cd') as fee_trans_same,
        (SELECT cont_id FROM payout_cont as t WHERE t.cont_id in ($res_) and t.bank_cd='$bank_cd') as id_trans
    "))
    ->first();

结果$ cont_id为= AAA,BBB

任何人都可以帮助简化查询吗? 所以我可以做一次查询,不需要做两次。 (首先查询要获取cont_id,然后先将其内爆,然后再进行第二次查询)。

问题出在$ res _

我已经在尝试使用concat并替换了,

示例尝试:concat('\'',replace(a.cont_id,',',"','"),'\'')。但获得结果id_trans = null。

如果有人可以帮助解决它,我非常感谢。

最后->first()个更改为->tosql()

SELECT a.cont_id AS id_list, a.default_cont_id id_default, 
       (SELECT bank_fee_trans_inter 
        FROM   payout_cont AS t 
        WHERE  t.cont_id = a.default_cont_id) AS fee_trans_inter, 
       (SELECT bank_fee_skn 
        FROM   payout_cont AS t 
        WHERE  t.cont_id = a.default_cont_id) AS fee_skn, 
       (SELECT bank_fee_skn 
        FROM   payout_cont AS t 
        WHERE  t.cont_id = a.default_cont_id) AS fee_rtgs, 
       (SELECT bank_fee_trans 
        FROM   payout_cont AS t 
        WHERE  t.cont_id IN ( 'AAAA', 'BBB' ) 
               AND t.bank_cd = 'CENA')        AS fee_trans_same, 
       (SELECT cont_id 
        FROM   payout_cont AS t 
        WHERE  t.cont_id IN ( 'AAAA', 'BBB' ) 
               AND t.bank_cd = 'CENA')        AS id_trans 
FROM   `client_conf` AS `a` 
WHERE  `client_id` = ? 

我已经使用find_in_set找到了解决方案。 在我的更新代码下面

$custom2 = DB::table('client_conf as a')->where('client_id',$client_id)
            ->select(DB::raw("
                a.cont_id as id_list,
                a.default_cont_id id_default,
                (SELECT bank_fee_trans_inter FROM payout_cont as t WHERE t.cont_id=a.default_cont_id) as fee_trans_inter,
                (SELECT bank_fee_skn FROM payout_cont as t WHERE t.cont_id=a.default_cont_id) as fee_skn,
                (SELECT bank_fee_rtgs FROM payout_cont as t WHERE t.cont_id=a.default_cont_id) as fee_rtgs,
                (SELECT bank_fee_trans FROM payout_cont t,client_conf j WHERE FIND_IN_SET(t.cont_id,id_list) <> 0 and t.bank_cd='$bank_cd' group by t.bank_fee_trans) as fee_trans_same,
                (SELECT t.cont_id FROM payout_cont t,client_conf j WHERE FIND_IN_SET(t.cont_id,id_list) <> 0 and t.bank_cd='$bank_cd' group by t.cont_id) as id_trans
            "))
            ->first();

0 个答案:

没有答案