Laravel查询返回意外顺序

时间:2018-06-11 15:25:22

标签: php mysql laravel

我正在使用Laravel Query Builder获取一组行,按2列排序,ticket_id然后id(之前为created_at)。但是,结果按ticket_id(正确)排序,但id(或created_at)次要订单不正确。我是否接近这种错误,误解的命令?

这是我的疑问:

$responses = Response::where('status', '=', 'closed');
                       ->select('id', 'ticket_id', 'created_at', 'author_id')
                       ->orderBy('ticket_id', 'id')
                       ->get();

反过来,返回:

  0 => array:3 [
    "ticket_id" => 14
    "id" => 71
    "time" => "2018-05-25 08:55:03"
  ]
  1 => array:3 [
    "ticket_id" => 14
    "id" => 75
    "time" => "2018-05-25 12:48:45"
  ]
  2 => array:3 [
    "ticket_id" => 14
    "id" => 72
    "time" => "2018-05-25 08:55:53"
  ]
  3 => array:3 [
    "ticket_id" => 13
    "id" => 70
    "time" => "2018-05-25 08:53:50"
  ]
  4 => array:3 [
    "ticket_id" => 13
    "id" => 76
    "time" => "2018-05-29 12:30:46"
  ]
  5 => array:3 [
    "ticket_id" => 13
    "id" => 74
    "time" => "2018-05-25 11:30:36"
  ]

你看到14号票,订单是71,75,72,而不是71,72,75。

这也是表结构:

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| ticket_id  | int(11)          | NO   |     | NULL    |                |
| author_id  | int(11)          | NO   |     | NULL    |                |
| status     | int(11)          | NO   |     | 0       |                |
| content    | text             | NO   |     | NULL    |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
| assets     | text             | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

1 个答案:

答案 0 :(得分:5)

ASC, DESC将列名称作为第一个参数,将方向orderBy('ticket_id', 'id')作为第二个参数。

orderBy('ticket_id', 'desc')->orderBy('id','desc')应为Difference = var LatestDate = Table[Date] var LatestValue = Table[Price] var PreviousDate = Dateadd(Table[Date],-1, day) var PreviousValue = CALCULATE(FIRSTNONBLANK(Table[Price],1), FILTER(Table, Table[Date]=PreviousDate)) RETURN IF(CONTAINS(Table,Table[Date],PreviousDate), LatestValue-PreviousValue , 0)