1 CREATE TEMPORARY TABLE tmp_table ( 2 id INTEGER not NULL, 3 uname VARCHAR(10) NOT NULL, 4 ucode VARCHAR(10) NOT NULL, 5 uprice INTEGER NOT NULL 6 ); 7 8 INSERT INTO tmp_table VALUES( 9 6, 10 '张三', 11 '891', 12 19 13 );
数据集如上。按照uname,ucode分组,
我需要得到的结果是:
取出 uprice 最小值 和对应的那条数据的ID,如果是重复的,就取最小ID
效果图如下:
请各位大神帮忙看看,小弟不胜感激。
SELECT ns.id, ns.name, ns.course, ns.score FROM ( SELECT id, name, course, score FROM score ORDER BY score DESC) AS ns GROUP BY ns.name ORDER BY ns.id;
select a.* from tmp_table a where (select count(1) from tmp_table b where b.uname=a.uname and b.ucode>=a.ucode)<=1 order by a.uname,a.ucode
SELECT * FROM ( SELECT * FROM tmp_table ORDER BY uprice asc) as ns
GROUP BY ns.uname,ns.ucode
一楼的原理是挺对的,但是应该是升序。