gpt4 book ai didi

mysql - Laravel 按特定列排序等于 id

转载 作者:行者123 更新时间:2023-11-29 15:50:32 25 4
gpt4 key购买 nike

我有一个名为点(id,name,with)列的表,列描述了哪个点通过 id 与另一个点相关,我需要什么来选择这些数据,这些数据按与 laravel elequant 相关的每个点排序

id   name   with
1 point1 3
2 point2 6
3 point3 1
4 point4 5
5 point5 4
6 point6 2

$data = $this->model
->select('id', 'name', 'with')
->where('with', '!=', NULL)
->orderByRaw(DB::raw("FIELD('id', 'with')"));

我需要这样的东西:

id   name   with
1 point1 3
3 point3 1
2 point2 6
6 point6 2
4 point4 5
5 point5 4

最佳答案

这应该按照您想要的方式工作!

已编辑

$data = $this->model
->select(DB::raw('id, name, with, CONCAT( GREATEST(id,with) , LEAST(id,with)) as sortCrit' )
->whereNotNull('with')
->orderBy('sortCrit');

顺便说一下,最好不要使用“with”作为列名。

关于mysql - Laravel 按特定列排序等于 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785488/

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