请参阅下面的mysql表数据。我想做的是每个日期都有MIN(价格),但也选择匹配的some_id。
SELECT date, MIN(price), some_id
FROM table
GROUP BY date
这不起作用,因为mysql 5.7引入了完全分组模式,因为它是默认的。
有什么想法吗?请注意,它是一个包含数百万行的大型表。
date price some_id
2016-04-09 10.66 3535475
2016-04-09 11.53 2429075
2016-04-09 11.53 2119575
2016-04-09 12.53 1431700
2016-04-09 12.55 2119375
2016-04-10 8.58 885050
2016-04-10 13.20 3535475
2016-04-10 14.27 2429075
2016-04-10 14.27 2119575
2016-04-10 14.70 13591
2016-04-11 11.98 885050
2016-04-11 12.44 2119375
2016-04-11 13.00 3535475
2016-04-11 13.79 2119575
2016-04-11 14.27 2429075
2016-04-11 14.91 13409
2016-04-11 15.38 2152375
2016-04-11 15.64 1431700
答案 0 :(得分:2)
尝试将您的查询用作派生表,并JOIN
返回原始表格以获取归档some_id
:
SELECT t1.date, t1.price, t1.some_id
FROM table t1
JOIN (
SELECT date, MIN(price) as min_price
FROM table
GROUP BY date
) t2 ON t1.date = t2.date AND t1.price = t2.min_price
答案 1 :(得分:0)
为了将来参考,默认情况下强制执行的full_group_by
模式可以帮助您编写更多符合SQL的数据库查询。例如,PostgreSQL默认情况下也强制执行此操作。
你可以通过两种方式做你想做的事。第一种方法是在聚合之前使用WHERE
限制数据集:
SELECT date, MIN(price) FROM table WHERE some_id = [YOUR ID HERE] GROUP BY date
第二个是在完成汇总后使用HAVING
选择some_id
:
SELECT some_id, date, MIN(price) FROM table GROUP BY some_id, date HAVING some_id = [YOUR ID HERE]
Here's a good answer describing the difference between WHERE and HAVING
答案 2 :(得分:0)
如果先获得灌浆然后执行
之类的连接,该怎么办?SELECT t.some_id, xxx.`date`, xxx.minprice
from table t join (
SELECT date, MIN(price) as minprice
FROM table
GROUP BY date) xxx on t.date = xxx.date;
答案 3 :(得分:0)
来源MySQL 5.7 Reference Manual - 13.20.3 MySQL Handling of GROUP BY
您可以在不禁用ONLY_FULL_GROUP_BY的情况下实现相同的效果 使用
ANY_VALUE()
来引用非聚合列。
因此:
SELECT date, MIN(price), ANY_VALUE(some_id)
FROM table
GROUP BY date
答案 4 :(得分:0)
这应该有效:
UIColor *selectedColor = [UIColor whiteColor];
for (UIControl *subview in [segmentControl subviews]) {
[subview setTintColor:selectedColor];
}
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:12.0f]};
[segmentControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
[segmentControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
[segmentControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
NSForegroundColorAttributeName:[UIColor redColor]}
forState:UIControlStateSelected];
子句适用于群组。