作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的 MySQL 数据库包含存储在 4 个类别中的多个产品。所有信息都在同一个表中:
id - name - description - price - category
我正在尝试按此顺序列出它们。我想知道如果可能的话只能通过 MySQL 请求来执行此操作。
Cat1 : product-name-with-lower-price1
Cat2 : product-name-with-lower-price1
Cat3 : product-name-with-lower-price1
Cat4 : product-name-with-lower-price1
Cat1 : product-name-with-lower-price2
Cat2 : product-name-with-lower-price2
Cat3 : product-name-with-lower-price2
Cat4 : product-name-with-lower-price2
Cat1 : product-name-with-lower-price3
Cat2 : product-name-with-lower-price3
Cat3 : product-name-with-lower-price3
Cat4 : product-name-with-lower-price3
等...
很抱歉没有提供任何我测试过的源代码,因为我尝试过的所有东西都不起作用。我想使用 MySQL Order By 子句,但我不知道如何使用。
非常感谢
编辑:
产品按较低的价格列出。在提取每只猫的较低价格后,我们按价格 ASC 排序。然后提取每只猫的下一个较低价格并按价格 ASC 等排序......
所以它看起来像这样:
Cars : Aygo 8000$
Moto : Suzuki 8200$
Bus : Renault 8700$
Truck : Peugeot 9000$
Cars : Toyota 9300$
Bus : Renault 9400$
Truck : DMG 9600$
Moto : Harley 14000$
Bus : Mercedes 12000$
Moto : BMW : 18000$
Cars : Mercedes 11000$
Truck : Renault 10000$
最佳答案
一种可能的方法:
SELECT id, price, category
FROM (
SELECT @rownum :=
(CASE
WHEN @cat = t.category THEN @rownum + 1
ELSE (@cat := t.category) IS NOT NULL
END) criteria,
t.*
FROM Table1 t
ORDER BY category, price
) tt
ORDER BY tt.criteria,
FIELD(tt.category, 'Cat1', 'Cat2', 'Cat3');
SQL Fiddle . FIELD
函数用于设置类别的特定排序;如果您同意它们按字母顺序排序,您可以将其替换为 ORDER BY tt.criteria, tt.category
。
关于mysql - MySQL 中特定的 ORDER BY 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27087416/
我是一名优秀的程序员,十分优秀!