mysql union 不同表。一个SQL输出 混合输

2019-03-14  本文已影响0人  月夜曾朦胧
        $where['v.id'] = session('carowner.id');
        $capital = Db::name('wuser')
        ->field(' c.id as id,c.money as money,c.add_time as add_time,c.memo as memo,c.user_id as name,c.type as type')
        ->alias('v')
        ->join('vehicle_mail_list m','m.user_id = v.id','left')
        ->join('vehicle_task_capital c','c.task_id=m.id')
        ->where($where)
        ->buildSql();

        $wher['vv.id'] = session('carowner.id');
     

        $dd = Db::name('wuser')
        ->field('tt.id as id,tt.money as money,tt.add_time as add_time,tt.memo as memo,vv.name as name,tt.type as type')
        ->alias('vv')
        ->join('vehicle_tally tt','vv.id = tt.user_id')
        ->where($wher)
        ->union($capital)
        ->order('add_time desc')
        ->buildSql();

        $cc = Db::table($dd.'a')
        ->field('id,money,add_time,memo,name,type')
        ->where($w)
        ->paginate(10);

先单查出一组数据   $capital   sql    然后  $dd  ouion 接上,  field 对上号,
在tp5   ¥cc 可以直接带入分页 
无视  where
查询时  还是在 dd 和 capital 查

 SELECT tt.id as id,tt.money as money,tt.add_time as add_time,tt.memo as memo,vv.name as name,tt.type as type FROM `che_wuser` `vv` INNER JOIN `che_vehicle_tally` `tt` ON `vv`.`id`=`tt`.`user_id` WHERE  `vv`.`id` = 14 UNION ( ( SELECT t.id as id,t.money as money,t.add_time as add_time,t.memo as memo,v.name as name,t.type as type FROM `che_wuser` `v` LEFT JOIN `che_vehicle_tally` `t` ON `v`.`id`=`t`.`user_id` WHERE  `v`.`id` = 14 ) ) UNION ( ( SELECT c.id as id,c.money as money,c.add_time as add_time,c.memo as memo,c.user_id as name,c.type as type FROM `che_wuser` `v` LEFT JOIN `che_vehicle_mail_list` `m` ON `m`.`user_id`=`v`.`id` INNER JOIN `che_vehicle_task_capital` `c` ON `c`.`task_id`=`m`.`id` WHERE  `v`.`id` = 14  AND `c`.`type` = '货主结款' ) ) ORDER BY `add_time` DESC


删除里面多余的 一个  union  就是正确的sql   留记录备份,之前一直没用过


上一篇下一篇

猜你喜欢

热点阅读