查询的问题

duanduan1122 2003-12-30 07:02:11
我有一张表:
id company telphoe
1 a1 123
2 a1 456
3 a2 678
4 a2 897

我想用一条查询语句得到下列结果:
id a1 a2
1 123 678
2 456 897

请问我的select 怎么写?
...全文
46 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
duanduan1122 2003-12-30
  • 打赏
  • 举报
回复
当我的表中company列有多条记录时怎么办呢?
上面的做法,不是太复杂了吗?
有简单的方法吗?

能实现的话马上给分!
Jianli2004 2003-12-30
  • 打赏
  • 举报
回复
上面的办法不够通用

select identity(int,1,1) id, telphone as a1 into #2 from #1 where company = 'a1'
select identity(int,1,1) id, telphone as a2 into #3 from #1 where company = 'a2'

select a.id,a.a1,b.a2
from #2 a full join #3 b on a.id=b.id

cgsun 2003-12-30
  • 打赏
  • 举报
回复
select A.id, A.telphoe as a1, B.telphoe as a2 from table1 as A, table1 as B
where A.company = 'a1' and B.company = 'a2' and A.id = B.id - 2
愉快的登山者 2003-12-30
  • 打赏
  • 举报
回复
select A.id, A.telphoe as a1, B.telphoe as a2 from table1 as A, table1 as B
where A.company = 'a1' and B.company = 'a2' and A.id = B.id - 2

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧