表1
UserID(唯一) 姓名 年纪 城市
123 张3 21 北京
124 李4 22 上海表2
UserID 收入 ID(关联表3ID)
124 5221 1
123 4622 2表3
ID 出事
1 计算机
2 金融表1userid字段关联表2的UserID,表2的ID字段关联表3ID字段,根据表1的Userid=123时,查询3个表相关联的数据
SQL语句怎么写?
结果是:
123 张3 北京 5221 1 计算机
UserID(唯一) 姓名 年纪 城市
123 张3 21 北京
124 李4 22 上海表2
UserID 收入 ID(关联表3ID)
124 5221 1
123 4622 2表3
ID 出事
1 计算机
2 金融表1userid字段关联表2的UserID,表2的ID字段关联表3ID字段,根据表1的Userid=123时,查询3个表相关联的数据
SQL语句怎么写?
结果是:
123 张3 北京 5221 1 计算机
b.UserID,
b.收入,
c.出事
from 表1 a,表2 b,表3 c
where a.UserID=b.UserID and b.ID=c.ID
select
*--指定顯示列
from t1,t2,t3
where t1.UserID=t2.UserID and t2.ID=t3.ID
inner join 表3 on b.id=c.id
from 表1 a
inner join 表2 b
on a.UserID=b.UserID
inner join 表3 c
on b.id=c.ID
go
if OBJECT_ID('tb_a') is not null
drop table tb_a
go
create table tb_a ( userid int , 姓名 varchar(5),年纪 int , 城市 varchar(5))
insert into tb_a
select 123, '张3', 21, '北京' union all
select 124, '李4', 22, '上海'
go
if OBJECT_ID('tb_b') is not null
drop table tb_b
go
create table tb_b(userid int, 收入 int , id int)
insert into tb_b
select 124, 5221, 1 union all
select 123, 4622, 2
go
if OBJECT_ID('tb_c') is not null
drop table tb_c
go
create table tb_c(id int ,出事 varchar (10))
insert into tb_c
select 1, '计算机' union all
select 2, '金融'
select * from tb_a
select * from tb_b
select * from tb_cselect a.userid,姓名,城市,b.收入,c.出事
from tb_a a join tb_b b on a.userid=b.userid
join tb_c c on b.id=c.id
where a.userid=123
use PracticeDB
go
if OBJECT_ID('tb_a') is not null
drop table tb_a
go
create table tb_a ( userid int , 姓名 varchar(5),年纪 int , 城市 varchar(5))
insert into tb_a
select 123, '张3', 21, '北京' union all
select 124, '李4', 22, '上海'
go
if OBJECT_ID('tb_b') is not null
drop table tb_b
go
create table tb_b(userid int, 收入 int , id int)
insert into tb_b
select 124, 5221, 1 union all
select 123, 4622, 2
go
if OBJECT_ID('tb_c') is not null
drop table tb_c
go
create table tb_c(id int ,出事 varchar (10))
insert into tb_c
select 1, '计算机' union all
select 2, '金融'
select * from tb_a
select * from tb_b
select * from tb_cselect a.userid,姓名,城市,b.收入,c.出事
from tb_a a join tb_b b on a.userid=b.userid
join tb_c c on b.id=c.id
where a.userid=123userid 姓名 城市 收入 出事
123 张3 北京 4622 金融
from tb1 a
left join tb2 b on a.UserID = b.UserID
left join tb3 c on b.ID = c.ID
where a.UserID = 123