sql 查询一个字段多个值都有的
如:有表:t1namenumbermoneymonth...........................a1123103a112353a112373a225844a2...
如:有表:t1
name number money month
...........................
a1 123 10 3
a1 123 5 3
a1 123 7 3
a2 258 4 4
a2 258 2 4
a3 147 1 1
a4 256 7 1
如何用sql语句查出符合如下条件的结果:
name和number相同的只取一条记录(随便一条),只取相同的,显示这条所有字段
如最后结果应为:
name number money month
...........................
a1 123 10 3
a2 258 4 4 展开
name number money month
...........................
a1 123 10 3
a1 123 5 3
a1 123 7 3
a2 258 4 4
a2 258 2 4
a3 147 1 1
a4 256 7 1
如何用sql语句查出符合如下条件的结果:
name和number相同的只取一条记录(随便一条),只取相同的,显示这条所有字段
如最后结果应为:
name number money month
...........................
a1 123 10 3
a2 258 4 4 展开
5个回答
展开全部
DB2/Oracle /SQL SERVER 2005以上可以这样写
select name, number, money, month
from (SELECT name, number, money, month, row_number() over(partition by name, number order by money) rk from tab) t
where rk = 1
select name, number, money, month
from (SELECT name, number, money, month, row_number() over(partition by name, number order by money) rk from tab) t
where rk = 1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select name ,number ,min(money) money, min(month) month,count(1)
from t1 group by name ,number having count(1) >1
from t1 group by name ,number having count(1) >1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select name, number, min(money) money, min(month) month
from t1
group by name, number;
from t1
group by name, number;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-09-15
展开全部
通用的:
select * from t1
where exists(select * from(select name,number,max(money) as money from t1 group by name,number) as tmp
where name=t1.name and number=t1.number
and money=t1.money)
select * from t1
where exists(select * from(select name,number,max(money) as money from t1 group by name,number) as tmp
where name=t1.name and number=t1.number
and money=t1.money)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
去查下group by子句的用法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询