简体   繁体   English

在多个值上排序MYSQL Laravel

[英]Sorting on multiple values MYSQL Laravel

I'm currently working on a project where I need to sort a collection using MySQL, but I really can't get this working. 我目前正在一个项目中,需要使用MySQL对集合进行排序,但是我真的无法正常工作。 I have a table with Components. 我有一个带有组件的表。 Components belong to a Training. 组件属于培训。 So a Training can have multiple Components. 因此,培训可以具有多个组成部分。

I made a HTML table with the results, with the following columns: 我用结果创建了一个HTML表,并包含以下各列:

  • Date 日期
  • Time 时间
  • Training_id Training_id
  • Component 零件

What I want, is to sort the components by: - Date, all dates should be sorted ASC - Time, the component that has the earliest time needs to be on top - Component, based on the Time, Components needs te be grouped by the same Training. 我想要的是按以下方式对组件进行排序:-日期,所有日期都应按ASC排序-时间,最早时间的组件需要放在顶部-组件,根据时间,组件需要按同样的训练。

For example, a list with these components: 例如,包含以下组件的列表:

Date: 4-11-2014 日期:2014年4月11日
Time: 11:00 时间:11:00
Training_id: 1 Training_id:1
Component: First component 组成部分:第一个组成部分

Date: 4-11-2014 日期:2014年4月11日
Time: 11:30 时间:11:30
Training_id: 2 Training_id:2
Component: First component of training_id 2 组件:training_id 2的第一部分

Date: 4-11-2014 日期:2014年4月11日
Time: 12:00 时间:12:00
Training_id: 1 Training_id:1
Component: Second component 组成部分:第二部分

Needs to be placed in the order: First component, second component, First component of training_id 2. This because the first component based on time is 'First component'. 需要按以下顺序放置:training_id 2的第一部分,第二部分,第一部分。这是因为基于时间的第一部分是“第一部分”。 The next step is that all components for this Training will be sorted underneath the 'First Component' (and also based on the time). 下一步是,本培训的所有组件都将在“第一个组件”下方(并根据时间)进行排序。 After that The next training comes. 之后,接下来的训练到了。

Does anyone have a clue, I really can't figure this one out.. 有人知道吗,我真的不知道这一点。

This is the order_by functions I have so far: 这是我到目前为止拥有的order_by函数:

    $result =  Components::ofType('Training')
        ->select('components.*')
        ->join('trainings', 'trainings.id', '=', 'components.parent_id')
        ->groupBy('components.id')
        ->orderBy('component_start_date')
        ->orderBy('parent_id')
        ->orderBy('from')
        ->get();

它可能为您工作$ data-> orderBy(“ date”,“ asc”)-> orderBy(“ time”,“ asc”)

$training = DB::table('training')
                        ->leftJoin("component", "component.training_id", "=", "training.id")
                        ->groupBy("trainig.id")
                        ->orderBy("component.Date", "asc")->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM