Sql基础之多表联查

本文介绍了一个关于数据库中多表联合查询的具体案例,通过连接四个不同的数据表(表a、表b、表c和表d),展示了如何使用SQL语句获取指定ID的所有相关信息。示例包括了使用INNER JOIN和LEFT JOIN的不同方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现构造多个数据表以供查询表

表a
id name 
1  张三
2  李四
3  王五
4  小赵
表b
ID userid jqnumber
1  1       2张
2  2       4张
表c
ID  userid  wcj
1   1       2人
2   3       4人
表d
ID  userid  ht
1   2       1包
2   4       2包

查询ID=1的所有数据,例如:

1 张三  2张  2人   

 

 

答案:

select a.id,a.name,b.jqnumber,c.wcj 

from 表a a 

join  表b b on a.id=b.userid 

join  表c c on a.id=c.userid 

where a.id=1



select a.id,a.name,b.jqnumber,c.wcj,d.ht

from 表a a inner join 表b b on a.id=b.userid

inner join 表c c on a.id=b.userid

inner join 表d d on a.id=d.userid

--也可以考虑用 left join

 

 

下为标准select 

select a.id,a.[name],b.jqnumber,c.wcj,d.ht

from a,b,c,d

where a.id = b.userid

  and a.id = c.userid

  and a.id = d.userid
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值