一道查询数据的sql数据库问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 23:23:16
一个数据表Score,有以下字段信息:
sName char 主键 学生姓名
sScore int 非主键 学生分数

写出查询出此表中分数相同的学生信息,要求输出的信息为:
name1,name2,score
select distinct s1.sName,s2.sName,s1.sScore
from score as s1,score as s2
where s1.sName !=s2.sName and s1.sScore =s2.sScore
这样是不对的,因为产生了冗余记录,所以funlove9的回答是不对的,设想有3个学生的分数是相同的,上面sql语句就会产生6条记录,应该输出3条记录才对。

我认为必须将属性重命名才可以否则就会出现冗余
因为用s1.sName无法区分是第一个姓名属性还是第二个姓名属性
select distinct s1.sName as s1.sName1,s2.sName2 as s2.sName2, s1.sScore
from score as s1,score as s2
where s1.sName !=s2.sName and s1.sScore =s2.sScore
加上not exists(s1.sName1=s2.sName2
and s1.sName2=s2.sName1)
这样应该不会出现冗余记录了

select a.sName,b.sName,a.score from Score a,Score b where a.score=b.score and a.sName<>b.sName

!!!
这个问题!! 很高级
你说你怎么逻辑思维也想不出这么抽象的东西啊!
我知道有个关键字 能把相同的去掉 还真不知道怎么实现上面的SQL查询啊

select A.sName,B.sName,A.sScore from Score A
join Score B on Score A.sName=Score B.sName
where A.sScore=B.sScore

真是不好意思 我忘记该怎么写了 好象是这样 就是自己连自己 如果不对就在问问吧.. 毕竟我也不是高手