简体   繁体   English

MySQL或Yii2 ActiveRecord

[英]MySQL or Yii2 ActiveRecord

I have table with test_id and created_at 我有带有test_id和created_at的表

Column "created_at" has different values. 列“ created_at”具有不同的值。 I want to executed SQL query, where split by column "created_at". 我要执行SQL查询,并按“ created_at”列拆分。 Where 哪里

This structure 这种结构

I want to get this structure of array: 我想得到数组的这种结构:

$all = array('created_at'=>'somevalues', 'test'=>array()); 

It depends how many values we are talking about. 这取决于我们在谈论多少个价值观。 If you have 100 different created_at values your solution will run 100+1 queries. 如果您有100个不同的created_at值,则您的解决方案将运行100 + 1个查询。 So you will put a lot of pressure on the server. 因此,您将对服务器施加很大压力。 If you have a lot of created_at values a better solution would be to just get the data, order it by created_at and go through the results and group them manually. 如果您有很多created_at值,那么更好的解决方案是仅获取数据,然后按created_at对其进行排序,然后遍历结果并将其手动分组。 I believe this will be a lot, lot faster than what you are trying to do. 我相信这会比您尝试的速度快很多。 Something like 就像是

$model = Model::find()->orderBy(['created_at' => SORT_ASC])->all();
foreach($model as $item) {
     $data[$item->created_at]['results'][] = $item;
     // $data[$item->created_at]['created_at'] = $item->created_at; if you really need to 
}

In the end you will end up with something almost exactly how you wanted it. 最后,您将获得几乎完全想要的东西。

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

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