本文介绍了在GROUP里面排序PHP / MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个案例,我必须按照品牌对表格中的结果进行分组,然后再按其他列进行排序。



我从一个数组中获得我的品牌。
问题在于,当我按品牌分组时,此组按组ID排序(组内)。
有没有一种方法来排序(排序)内组?

这是我的数组和mysql查询。

  $ laptop_brands = array(Acer,Apple,Dell,HP-Compaq,IBM-Lenovo,Sony,Toshiba ,华硕,富士通,网关); 
$ b $ get_videos_query =SELECT * FROM users_video WHERE location ='location_1'AND brand IN('。implode(',',$ laptop_brands)。'\')GROUP BY品牌ORDER (Acer,Apple,Dell,HP-Compaq,IBM-Lenovo,Sony,Toshiba,ASUS,Fujitsu,Gateway官方DESC';


解决方案

我找到了解决方案。
Thx每个人的笔记和想法。



所以这是我用来得到正确结果的查询:

  $ laptop_brands = array(Acer,Apple,Dell,HP-Compaq,IBM-Lenovo,Sony,Toshiba,ASUS,Fujitsu ); 


$ query =SELECT * FROM(SELECT * FROM users_video WHERE location ='location_1'AND brand IN('。implode(',',$ laptop_brands)。'' (品牌,宏碁,苹果,戴尔,惠普康柏,IBM-联想,索尼,东芝)作为my_table_tmp GROUP BY品牌ORDER BY FIELD ,华硕,富士通,网关);

Thx全部,尤其是对于morteza kavakebi


I have a case where I have to group results from table by brands and then order by some other column.

I get my brands from an array. The problem is that when I group by brand this group is sorted by ID (inside group). Is there a way to sort (order by) inside group?

Here is my array and mysql query.

$laptop_brands = array("Acer", "Apple", "Dell", "HP-Compaq", "IBM-Lenovo", "Sony", "Toshiba", "ASUS", "Fujitsu", "Gateway");

$get_videos_query = "SELECT * FROM users_video WHERE location = 'location_1' AND brand IN ('" . implode("','", $laptop_brands) . '\') GROUP BY brand ORDER BY FIELD (brand, "Acer", "Apple", "Dell", "HP-Compaq", "IBM-Lenovo", "Sony", "Toshiba", "ASUS", "Fujitsu", "Gateway"), official DESC';
解决方案

I've found the solution. Thx everyone for notes and ideas.

So this is the query I've used to get correct result:

$laptop_brands = array("Acer", "Apple", "Dell", "HP-Compaq", "IBM-Lenovo", "Sony", "Toshiba", "ASUS", "Fujitsu", "Gateway");


$query = "SELECT * FROM (SELECT * FROM users_video WHERE location = 'location_1' AND brand  IN ('" . implode("','", $laptop_brands) . '\') ORDER BY official DESC) as my_table_tmp GROUP BY brand ORDER BY FIELD (brand, "Acer", "Apple", "Dell", "HP-Compaq", "IBM-Lenovo", "Sony", "Toshiba", "ASUS", "Fujitsu", "Gateway")';

Thx all, especially to morteza kavakebi

这篇关于在GROUP里面排序PHP / MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:05