两表的关联查询

lutianling521 2009-04-12 12:34:28
A: id1 id2
1 2
1 3
2 2
2 3
2 4
B: id img .......
1 1.jpg .......
2 2.jpg .......
3 3.jpg .......
......

根据id1查询出B表的信息比如说id1=1就查出B表的id为2和3的所有列信息!
...全文
36 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2009-04-12
  • 打赏
  • 举报
回复
也可以用:
select b.* from a join b on a.id2=b.id where a.id1=1
select b.* from a,b where a.id2=b.id and a.id1=1
htl258_Tony 2009-04-12
  • 打赏
  • 举报
回复
if object_id('a') is not null
drop table a
go
create table a([id1] varchar(10),[id2] varchar(10))
insert a select 1,'2'
insert a select 1,'3'
insert a select 2,'2'
insert a select 2,'3'
insert a select 2,'4'
go
if object_id('b') is not null
drop table b
go
create table b([id] varchar(10),[img] varchar(10))
insert b select '1','1.jpg'
insert b select '2','2.jpg'
insert b select '3','3.jpg'

go

select b.* from a join b on a.id2=b.id and a.id1=1
/*
id img
---------- ----------
2 2.jpg
3 3.jpg

(2 行受影响)
*/
htl258_Tony 2009-04-12
  • 打赏
  • 举报
回复
select b.* from a join b on a.id2=b.id and a.id=1

27,579

社区成员

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

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