A B
id name mid id name mname
1 刘备 1 1 刘备 刘备
2 关羽 1 2 关羽 刘备
3 张飞 1 3 张飞 刘备
4 曹操 2 4 曹操 曹操
5 张辽 2 5 张辽 曹操
在数据库中已经有表A,查询后的结果是B,请问各位,这个SQL查询语句该怎么写啊?还有一个问题!我的eclipse连接数据库的时候 出现 error establishiing socket 错误!有人知道怎么回事吗?
id name mid id name mname
1 刘备 1 1 刘备 刘备
2 关羽 1 2 关羽 刘备
3 张飞 1 3 张飞 刘备
4 曹操 2 4 曹操 曹操
5 张辽 2 5 张辽 曹操
在数据库中已经有表A,查询后的结果是B,请问各位,这个SQL查询语句该怎么写啊?还有一个问题!我的eclipse连接数据库的时候 出现 error establishiing socket 错误!有人知道怎么回事吗?
解决方案 »
- 非常棘手的三表连接查询,喜欢挑战难题的进来看看。
- 请教两张表的联合查询问题
- 我想DROP所有以“temp”开头的视图,语句怎么写呀?
- trigger 和 linked server 的问题
- 关于 SQL Server 2000 使用大于4G的内存
- 200分,SQL语句。
- 往数据表里添数据后,为什么有时不是添到最后一行?
- SQL2000 我的作业为什么没有定时执行
- 这样的存储过程为什么会错误?
- PB怎么在添加修改密码一项,怎么调用BMP文件?(急!!!)
- Log Explorer支持sqlserver2005吗?哪个版本?
- 在线等,菜鸟问题,sql server 中bak文件如何变成mdf,ldf文件
id name mid id name mname
1 刘备 1 1 刘备 刘备
2 关羽 1 2 关羽 刘备
3 张飞 1 3 张飞 刘备
4 曹操 2 4 曹操 曹操
5 张辽 2 5 张辽 曹操select a.id , a.name , b.name as mname
from a,(select name , mid from a) b
wherw a.mid = b.mid
insert @ta select 1, '刘备', 1
union all select 2, '关羽', 1
union all select 3, '张飞', 1
union all select 4, '曹操', 2
union all select 5, '张辽', 2 select id,name,
[mname]=(select name from @ta b
where not exists(select name from @ta where mid=b.mid and id<b.id)
and b.mid=a.mid)
from @ta a
(5 行受影响)
id name mname
----------- ---------- ----------
1 刘备 刘备
2 关羽 刘备
3 张飞 刘备
4 曹操 曹操
5 张辽 曹操(5 行受影响)
drop table tb
gocreate table tb(id int,name varchar(10),mid int)
insert into tb(id,name,mid) values(1, '刘备', 1)
insert into tb(id,name,mid) values(2, '关羽', 1)
insert into tb(id,name,mid) values(3, '张飞', 1)
insert into tb(id,name,mid) values(4, '曹操', 2)
insert into tb(id,name,mid) values(5, '张辽', 2)
goselect tb.id , tb.name , t.name as mname from tb
left join
(
select a.mid , a.name from tb a,
(select mid,min(id) id from tb group by mid) b
where a.mid = b.mid and a.id = b.id
) t
on tb.mid = t.middrop table tb/*
id name mname
----------- ---------- ----------
1 刘备 刘备
2 关羽 刘备
3 张飞 刘备
4 曹操 曹操
5 张辽 曹操(所影响的行数为 5 行)*/
select a.*,b.*
from table1 a inner join table1 b on (a.id=b.id)
drop table tb
gocreate table tb(id int,name varchar(10),mid int)
insert into tb(id,name,mid) values(1, '刘备', 1)
insert into tb(id,name,mid) values(2, '关羽', 1)
insert into tb(id,name,mid) values(3, '张飞', 1)
insert into tb(id,name,mid) values(4, '曹操', 2)
insert into tb(id,name,mid) values(5, '张辽', 2)
goselect tb.id , tb.name , t.name as mname from tb
left join
(
select a.mid , a.name from tb a,
(select mid,min(id) id from tb group by mid) b
where a.mid = b.mid and a.id = b.id
) t
on tb.mid = t.middrop table tb这个写法更好
select A.ID,A.name,B.id, B.name AS manage_name
from employees as A ,employees as BWHERE B.id=A.manage_id
create table t_ThreeKing
(
fid int identity(1,1),
fname varchar(20),
fmid int,
)insert into t_ThreeKing(fname,fmid) values('刘备',1)
insert into t_ThreeKing(fname,fmid) values('关羽',1)
insert into t_ThreeKing(fname,fmid) values('张飞',1)
insert into t_ThreeKing(fname,fmid) values('曹操',2)
insert into t_ThreeKing(fname,fmid) values('张辽',2) select t1.fid,t2.Fname,t1.fname from t_ThreeKing t1 inner join t_ThreeKing t2 on t1.fid=t2.fmid