A表字段如下:
id b c
1 2 3
2 2 2
3 3 2
4 1 4B表字段如下:
id name
1 aa
2 bb
3 cc
4 ddA表中的字段 b和c与B表中的id相对应,也就是A表字段b的2对应B表name中的bb,也就是A表字段c的3对应B表name中的cc,
现在我想得到的是A表中字段b和c在B表中name相同的记录,怎么做到,也就是说按上面的表来说应该得到
A表
id b c
1 2 3
2 2 2
3 3 2
id b c
1 2 3
2 2 2
3 3 2
4 1 4B表字段如下:
id name
1 aa
2 bb
3 cc
4 ddA表中的字段 b和c与B表中的id相对应,也就是A表字段b的2对应B表name中的bb,也就是A表字段c的3对应B表name中的cc,
现在我想得到的是A表中字段b和c在B表中name相同的记录,怎么做到,也就是说按上面的表来说应该得到
A表
id b c
1 2 3
2 2 2
3 3 2
解决方案 »
- c# 请教如何把DateTime转换为float
- 怎么根据ID 删选一个数据集文件中的一行数据,再把它放入一个dataset中
- C# Winfomr美化ListView的问题
- 急!!!
- 请问如何判断post的来源地址是否来自本站
- c#写的webservice其他的语言能调用么?例如vc、vb、pb、delphi等
- 在Web页面中如何动态时实获知文本框内容发生改变?在线急等变!!!
- C# 中使用DrawString实现字符间距缩放50%
- C# SerialPort 传输速率问题
- 如何一次获取oracle库中所有字典表的内容?????????????
- 有關C#上傳文件到MSSQL數據存儲問題
- 数据绑定控件的写法问题!
上表中得出的结果应该是
id b c
1 2 3
2 2 2
3 3 2
1 aa
2 bb
3 bb
4 dd不知道我理解得是否正确~
select * from A where id in(select id from A t1,At2 where t1.b=t2.c)
得到的不是我想要的结果
我想得到最后的结果是
A表
id b c
1 2 3
2 2 2
3 3 2
id b c
1 2 3
2 2 2
3 3 2
4 1 4 B表字段如下:
id name
1 aa
2 bb
3 bb
4 dd A表中的字段 b和c与B表中的id相对应,也就是A表字段b的2对应B表name中的bb,也就是A表字段c的3对应B表name中的cc,
现在我想得到的是A表中字段b和c在B表中name相同的记录,怎么做到,也就是说按上面的表来说应该得到
A表
id b c
1 2 3
2 2 2
3 3 2 CREATE TABLE tb (id int,b int,c int)
INSERT INTO tb(id,b,c) VALUES (1,2,3)
INSERT INTO tb(id,b,c) VALUES (2,2,2)
INSERT INTO tb(id,b,c) VALUES (3,3,2)
INSERT INTO tb(id,b,c) VALUES (4,1,4)
CREATE TABLE tb2(id int,[name] varchar(200))
INSERT INTO tb2(id,[name]) VALUES (1,'aa')
INSERT INTO tb2(id,[name]) VALUES (2,'bb')
INSERT INTO tb2(id,[name]) VALUES (3,'bb')
INSERT INTO tb2(id,[name]) VALUES (4,'dd')
SELECT tmp.id,tmp.b,tmp.c FROM
(SELECT tb.id AS id,b,c,[name] FROM tb LEFT JOIN tb2 ON tb.b=tb2.id) AS tmp JOIN (
SELECT * FROM (SELECT tb.id,c,[name] FROM tb LEFT JOIN tb2 ON tb.c=tb2.id) AS tm) tmp2 ON tmp.id=tmp2.id AND tmp.[name]=tmp2.[name]
ORDER BY id
DROP table tb
DROP TABLE tb2
得到的结果id b c
1 2 3
2 2 2
3 3 2
注意我标注颜色的地方,不知道是不是楼主有写错的~。
只是现在,你看我的这条语句是这样写的
A表字段如下:
id b c
1 2 3
2 2 2
3 3 2
4 1 4
select * from A where id in(select id from A t1,At2 where t1.b=t2.c) 我想得到最后的结果是
A表
id b c
1 2 3
2 2 2
3 3 2
declare @ta table (id int,b int, c int)
declare @tb table (id int,name varchar(10))insert @ta
select 1,2,3 union all
select 2,2,2 union all
select 3,3,2 union all
select 4,1,4insert @tb
select 1,'aa' union all
select 2,'bb' union all
select 3,'bb' union all
select 4,'dd'select d.id,b,c from
(select a.id,b,b.id as id1,name from @ta a,@tb b where a.b=b.id )d
join
(select a.id,c,c.id as id1,name from @ta a,@tb c where a.c=c.id)e
on d.id=e.id and d.name=e.name
order by d.id
id b c
----------------
1 2 3
2 2 2
3 3 2
你又如何得到你后面想要的结果呢???
B表中的NAME 你是打算如何处理的呢?
在你的表A中增加了2个字段,这样 SQL 语句就变成:
select id,b,c from a where nameb=namec
t1.id<>t2.id and (t1.b=t2.c or t2.b=t1.c))
只用A表一个表,这样就可以得出我要的结果了
你可以去试试,呵呵,谢谢你了~~