按行分组按最低价格排序

时间:2018-04-18 12:01:32

标签: mysql

我有一张表'条款':

([id], [t_id], [d_start], [d_end], [price], [departure_from])  

值:

(12,10, 2018-5-5, 2018-5-15, 95, london),  
(11,10, 2018-5-5, 2018-5-15, 60, london),  
(10,10, 2018-5-5, 2018-5-15, 90, london),  
( 9, 10, 2018-5-5, 2018-5-15, 90, prag),  
( 8, 10, 2018-5-5, 2018-5-15, 85, prag),   
( 7, 10, 2018-5-5, 2018-5-15, 70, prag),   
( 6, 10, 2018-6-8, 2018-6-18, 30, london),    
( 5, 10, 2018-6-8, 2018-6-18, 20, london),   
( 4, 10, 2018-6-8, 2018-6-18, 90, london),   
( 3, 10, 2018-6-8, 2018-6-18, 90, prag),  
( 2, 10, 2018-6-8, 2018-6-18, 10, prag),   
( 1, 10, 2018-6-8, 2018-6-18, 70, prag); 

我希望按价格按d_start,d_end,departure_from分组,我想先按价格订购。
我有疑问:

(SELECT * FROM 
  (SELECT * FROM terms 
   WHERE t_id=10 ORDER BY price) t
GROUP BY d_start,d_end,departure_from ORDER BY d_start) 

结果我想:4行最小价格:
(11,10,2018-5-5,2018-5-15,60,伦敦),
(7,10,2018-5-5,2018-5-15,70,prag),
(5,10,2018-6-8,2018-6-18,20,20,伦敦),
(2,10,2018-6-8,2018-6-18,10,prag),

我的第一个问题。感谢您的任何帮助。

我发现结果对我有用:
(SELECT * FROM   (SELECT * FROM terms    WHERE_t_id = 10 GROUP BY d_start,d_end,departure_from,price ORDER BY d_start)t GROUP BY d_start,d_end,departure_from)

你怎么看?没关系?

2 个答案:

答案 0 :(得分:0)

根据您提供的信息,您遇到的主要问题是变量名称。只需为每个人添加一个``。

像:

SELECT * FROM (SELECT * FROM terms WHERE t_id=10 ORDER BY price) GROUP BY `start`,`end`,`from` ORDER BY start

答案 1 :(得分:0)

您需要为此使用相关子查询...

groupby