Weaving Wong

总觉得该写点啥...

嗨,我是Weaving,一名机器学习爱好者.


分享读书、学习、生活感悟

MySQL——分组查询表内某字段最大值所在的记录

# 示例 

CREATE TABLE orders
(id VARCHAR(10),
statu CHAR(1),
goods_id VARCHAR(10),
price DECIMAL(12,2)
);
 

INSERT INTO orders(id,goods_id,statu,price) VALUES('1','g','1',100);
INSERT INTO orders(id,goods_id,statu,price) VALUES('2','g','1',200);
INSERT INTO orders(id,goods_id,statu,price) VALUES('3','g','0',300);
INSERT INTO orders(id,goods_id,statu,price) VALUES('4','g','1',400);
INSERT INTO orders(id,goods_id,statu,price) VALUES('5','c','0',150);
INSERT INTO orders(id,goods_id,statu,price) VALUES('6','c','1',250);
INSERT INTO orders(id,goods_id,statu,price) VALUES('7','c','0',350);
INSERT INTO orders(id,goods_id,statu,price) VALUES('8','c','1',400);

# 方法一:使用子查询
SELECT a.* 
FROM 
orders a ,(SELECT b.goods_id,MAX(b.price) price 
FROM orders b GROUP BY b.goods_id) c 
WHERE a.goods_id = c.goods_id AND a.`price` = c.price


 # 使用in 和exists 
SELECT a.* FROM orders a 
WHERE NOT EXISTS
(SELECT 1 FROM orders b 
WHERE  a.goods_id = b.goods_id AND a.price <b.price)


SELECT * FROM orders 
WHERE price NOT IN 
(SELECT a.price FROM orders b,orders a 
WHERE  a.goods_id = b.goods_id AND a.price <b.price)
最近的文章

JDATA-”京东杯“-用户对品类下店铺购买预测_季军方案+比赛攻略

方案开源地址:季军方案 比赛链接:“京东杯”2019第六届泰达创新创业挑战赛-用户对品类下店铺购买预测 比赛数据在此:https://pan.baidu.com/s/1Lnda54VN6IuEYF9GMxAk3g 提取码:9ttp 队伍名称: 优生801 第一部分:赛题解读 1、赛题背景目前,京东零售集团第三方平台签约商家超过21万个,实现了全品类覆盖,为维持商家生态繁荣、多样和有序,全面满足消费者一站式购物需求,需要对用...…

竞赛总结继续阅读
更早的文章

LeetCode shell 编程实践

统计词频写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。为了简单起见,你可以假设:words.txt只包括小写字母和 ’ ‘ 。每个单词只由小写字母组成。单词间由一个或多个空格字符分隔。示例:假设 words.txt 内容如下:the day is sunny the thethe sunny is is你的脚本应当输出(以词频降序排列):the 4is 3sunny 2day 1说明:不要担心词频相同的单词的排序问题,每个单词出现的频率都是唯一的。你可...…

shell继续阅读