gpt4 book ai didi

mysql - 根据 Laravel 和 MySQL 是否为 null 按两列排序

转载 作者:行者123 更新时间:2023-11-29 09:34:46 25 4
gpt4 key购买 nike

我有一个可为空的关系,有时会回退到字符串。因此,在对结果进行排序时,我想合并 brands.nameads.mixed_brand_name

我想要的订单示例:

  1. brands.name:奥迪 mixed_brand_name:null
  2. brands.name:null mixed_brand_name:BMW blabla
  3. brands.name:雪铁龙 mixed_brand_name:null

这就是我现在正在做的事情,但是所有带有品牌广告都会排在第一位(因为 brand_id 对于那些没有品牌的广告来说是空的品牌关系。

Ad::join('brands', 'brands.id', '=', 'ads.brand_id')
->orderBy('brands.name')
->orderBy('ads.mixed_brand_name');

最佳答案

使用自定义订单 orderByRaw

Ad::leftJoin('brands', 'brands.id', '=', 'ads.brand_id')
->orderByRaw('IFNULL(brands.name, ads.mixed_brand_name)');

关于mysql - 根据 Laravel 和 MySQL 是否为 null 按两列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57876988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com