laravel 数据库leftjoin的类似问题解决

2020-03-2811:18:10后端程序开发Comments2,953 views字数 1574阅读模式

解决类似问题:如文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

一张客户表,一张客户历史所属领导的表,在现在展示客户信息,要展示每条客户现在所属领导和上次所属领导文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

客户表                                                                                             客户历史领导表文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

id      name    sex  age                                                      id       new_leader     old_leader    customer_id     created_at文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

1        张三     男     18                                                       1          王总                李总                1                     2020-03-19文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

2       李四      女     19                                                       2          张总                王总                1                     2020-03-22文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

3         王总                 李总                2                     2020-03-21文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

本来一开始我想历史客户表添加数据时候就删除上一条记录,保存一条新的记录,当时日后如果要展示客户有多少个历史领导就不好办啦,客户也不多,所以这么写啦文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

SELECT
`customers`.*, `d`.`old_leader`,`new_leader `
`customers`.`created_at` AS `created_date`,
DATE_FORMAT(birthd, '%m%d ') AS birthd_sort
FROM
`customers`
LEFT JOIN (
SELECT
a.*
FROM
customer_distribute_logs AS a,
(
SELECT
b.`customer_id`,
MAX() AS `created_at`
FROM
customer_distribute_logs AS b
GROUP BY
b.`customer_id`
) AS c
WHERE
a.`created_at` = c.`created_at`
AND a.customer_id = c.customer_id文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

) as d ON `customers`.`id` = `d`.`customer_id`
WHERE
`customers`.`id` > 0
AND `customers`.`deleted_at` IS NULL文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

上面原生的sql 语句,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

下面 laravel项目中也是主要使用原生sql,因为laravel 的orm 高级语法没吃透,不会写文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

public function getQuery()
{
    $customers = (new Customer)->on('mysql-slave')->where('', '>', '0');
    $sql = '((SELECT a.* FROM customer_distribute_logs AS a,(SELECT    b.`customer_id`,MAX() AS `created_at`FROM
         customer_distribute_logs AS b GROUP BY    b.`customer_id`)AS c WHERE a.`created_at`=c.`created_at` AND a.customer_id = c.customer_id) as d)';

    $customers = $customers->leftJoin(DB::Raw($sql), '', '=', '')
        ->select('customers.*', '',`` 'customers.created_at as created_date',
            DB::raw("DATE_FORMAT(birthd , '%m%d ') as birthd_sort")
        );
    return $customers;
}

方法来获取model对象的 ,现在不怎么好看,但可以满足我需求,先这样,下次想出来在改进文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17961.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/17961.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定