易截截图软件、单文件、免安装、纯绿色、仅160KB

sql 连接问题! - MS-SQL Server / 基础类

1.下面有表
name birth
a 1981
b 1981
c 1980
d 1981
e 1980
2.写一sql得到下面的结果
name1 name2 birth
a b 1981
a d 1981
b d 1981
c e 1980


下面是我的记过,老是去不了重复的:
select * from testj;
select distinct a.name,b.name,a.year,a.year-1980 
from testj a,testj b where a.year=b.year and a.name!=b.name 
order by a.name,b.name;


但是得到的结果是
a b 1981

b a 1981
是要去掉的。恳请高手帮忙?


啊,上面写错了

哈哈,我自己也错,好多人都搞错!


select a.name,b.name,a.birth,a.birth-1980  
from #tb a left join #tb b on a.name<b.name 
and a.birth=b.birth 
where b.name is not null

用 < 或 >这样避免出现 a,b b,a的情况

还是不对啊,你怎么不自己运行下呢

建表语句

create table TESTJ
(
  NAME VARCHAR2(10),
  YEAR VARCHAR2(20),
  BIRTH VARCHAR2(10)
)

恩,对了,谢谢你!


相关问答:

SQL如何优化问题 - MS-SQL Server / 疑难问题

今天做了一个存储过程   环境是SQL2000数据库  
大致如下
建立临时表
定义员工游标
        循环员工(属于1个公司)  
        ......

求一sql语句 - MS-SQL Server / 疑难问题

现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......

求一个SQL语句 - MS-SQL Server / 基础类

字段1,字段2.....字段N,Status,ParentID
1,Name1....test1,1,99
1,Name1....test1,3,99
1,Name2....test2,1,101
1,Name2....test2,3,101
1,Name3....test3,2,101
1,Name1....test1,4,101
想要的结果是:
1,Na ......

SQL语句问题 - MS-SQL Server / 疑难问题

请教高手:
 以下是数据库中的三条记录,英文为字段名称  
  id planname TaskBeginTime Status
329 2010年03 ......

一条 SQL - Oracle / 基础和管理

有这样一条SQL
 Select Get_Costtaxrate(col1), Get_Tcostvalue(col1) from a
 
其中Get_Costtaxrate、Get_Tcostvalue都是函数,这两个函数里面都是查找一个大表,Get_Tcostvalue还需要调用Get_C ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号