急问SQL,如何实现任选一条记录?
我的表里有记录如下
A t1 8
B t1 8
C t1 8
A t2 6
A t3 9
B t3 9
如何以列2和列3为准,任选出一条记录来?比如前三条记录只需要一个,随便哪个都行,后两条记录也是,只选出一条来。
------解决方案--------------------select * from t where f1 in (select min(a.f1) from t a where a.f2=t.f2 )
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
最新版本:20070130
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------select min(co1),col2,col3 from table group by col2,col3
------解决方案--------------------CREATE TABLE T2(ID VARCHAR(10),N1 VARCHAR(10),N2 INT)
INSERT T2
SELECT 'A ', 't1 ', 8
UNION ALL
SELECT 'B ', 't1 ', 8
UNION ALL
SELECT 'C ', 't1 ', 8
UNION ALL
SELECT 'A ', 't2 ', 6
UNION ALL
SELECT 'A ', 't3 ', 9
UNION ALL
SELECT 'B ', 't3 ', 9
SELECT MAX(ID),N1,N2 FROM T2 GROUP BY N1,N2
SELECT MIN(ID),N1,N2 FROM T2 GROUP BY N1,N2
SELECT * FROM T2 A WHERE 1=(SELECT COUNT(1) FROM T2 WHERE A.N1 = N1 AND A.N2 = N2 AND A.ID > = ID )