根据不同的条件查询不同的表 sql
/*
a表 a.user_id a.name
b表 b.user_id b.name
c表 c.user_id c.user_type
当c表的 c.user_type = "a" 时 它显示 a表的 a.name
当c表的 c.user_type = "b" 时 它显示 b表的 b.name
a,b,c表中的 a.user_id = c.user_id,b.user_id = c.user_id
*/
if object_id('ta')is not null drop table ta
go
create TABLE ta([user_ID] int,name varchar(50))
INSERT INTO ta select
1,'a1' union all select
2,'a2'
if object_id('tb')is not null drop table tb
go
create TABLE tb([user_ID] int,name varchar(50))
INSERT INTO tb select
1,'b1' union all select
2,'b2'
if object_id('tc')is not null drop table tc
go
create TABLE tc([user_ID] int,user_type varchar(50))
INSERT INTO tc select
1,'a' union all select
2,'b'
select * from ta
select * from tb
select * from tc
--方法一
select c.[user_id],name=case user_type when 'a' then a.name when 'b' then b.name end
from tc c,ta a,tb b
where a.[user_id]=c.[user_id] and b.[user_id]=c.[user_id]
--方法二
SELECT tc.user_ID, CASE user_type WHEN 'a' THEN ta.name WHEN 'b' THEN tb.name END AS 输出
FROM tc INNER JOIN
ta ON tc.user_ID = ta.user_ID INNER JOIN
tb ON tc.user_ID = tb.user_ID
/*
user_ID name
----------- --------------------------------------------------
1 a1
2 a2
user_ID name
----------- --------------------------------------------------
1 b1
2 b2
user_ID user_type
----------- --------------------------------------------------
1 a
2 b
user_id 输出
----------- --------------------------------------------------
1 a1
2 b2
*/
a表 a.user_id a.name
b表 b.user_id b.name
c表 c.user_id c.user_type
当c表的 c.user_type = "a" 时 它显示 a表的 a.name
当c表的 c.user_type = "b" 时 它显示 b表的 b.name
a,b,c表中的 a.user_id = c.user_id,b.user_id = c.user_id
*/
if object_id('ta')is not null drop table ta
go
create TABLE ta([user_ID] int,name varchar(50))
INSERT INTO ta select
1,'a1' union all select
2,'a2'
if object_id('tb')is not null drop table tb
go
create TABLE tb([user_ID] int,name varchar(50))
INSERT INTO tb select
1,'b1' union all select
2,'b2'
if object_id('tc')is not null drop table tc
go
create TABLE tc([user_ID] int,user_type varchar(50))
INSERT INTO tc select
1,'a' union all select
2,'b'
select * from ta
select * from tb
select * from tc
--方法一
select c.[user_id],name=case user_type when 'a' then a.name when 'b' then b.name end
from tc c,ta a,tb b
where a.[user_id]=c.[user_id] and b.[user_id]=c.[user_id]
--方法二
SELECT tc.user_ID, CASE user_type WHEN 'a' THEN ta.name WHEN 'b' THEN tb.name END AS 输出
FROM tc INNER JOIN
ta ON tc.user_ID = ta.user_ID INNER JOIN
tb ON tc.user_ID = tb.user_ID
/*
user_ID name
----------- --------------------------------------------------
1 a1
2 a2
user_ID name
----------- --------------------------------------------------
1 b1
2 b2
user_ID user_type
----------- --------------------------------------------------
1 a
2 b
user_id 输出
----------- --------------------------------------------------
1 a1
2 b2
*/
分类:
SQL Server
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战