当前位置:网站首页>如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
2022-04-23 06:23:00 【CSDN问答】
问题遇到的现象和发生背景



问题相关代码,请勿粘贴截图
以下代码为SQL server 中生成数据表内容的代码,供测试使用
create table customer
(
custid int primary key,
cfirstname char(30),
csurname char(30),
billingaddress char(30),
cgender char(1) check(cgender in('M','F'))
);
create table shoporder
(
ordid int primary key,
deliveryaddress char(30),
spid int,
custid int,
foreign key(spid) references salesperson(spid),
foreign key(custid) references customer(custid)
);
use "3120004023";
INSERT
INTO customer(custid,cfirstname,csurname,cgender,billingaddress)
VALUES
('1','Casey','Cartwright','F','1 Roma Ave Cranbourne'),
('2','Evan','Chambers','M','8 David St Dandenong'),
('3','Calvin','Owens','M','7 Long Rd Lara'),
('4','Frannie','Morgan','F','9 Down Pde Upwey'),
('5','Cappie','Jones','M','6 Mist St Toorak'),
('6','Dana','Stockwell','F','2 Tree St Epping'),
('7','Ash','Howard','F','4 Elm Ave Elwood');
use "3120004023";
INSERT
INTO shoporder(ordid, deliveryaddress,spid,custid)
VALUES
(41,NULL,23,1),
(51,NULL,23,1),
(42,NULL,21,2),
(43,NULL,23,2),
(49,NULL,24,2),
(44,'1 John St Hawthorn',26,3),
(48,NULL,26,3),
(45,'1254 Dunstall Rd Coorparoo',22,4),
(47,'727 Hudson Rd Glenorchy',26,4),
(50,'517 Franklin St Dowerin',22,4),
(46,NULL,21,6);
我的解答思路和尝试过的方法
尝试过使用case when 达到相同的效果,但题目要求是使用 union子句
select ordid,case when deliveryaddress is NULL then billingaddress else deliveryaddress end as address from shoporder,customer where shoporder.custid=customer.custid;
我想要达到的结果

采纳答案:
union怎么写呀
我看到这个题,第一想到的就是case when。然后看到你下面也说了你用case when 。
本来以为我能解答,结果我也不会用union
版权声明
本文为[CSDN问答]所创,转载请带上原文链接,感谢
https://ask.csdn.net/questions/7693005
边栏推荐
- LATEX使用
- Solution of wireless intercom system in Commercial Plaza
- How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?
- What is a closure?
- Tensorflow安装后ImportError: DLL load failed: 找不到指定的模块,且国内安装缓慢
- Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference
- 可视化常见问题解决方案(七)画图刻度设置解决方案
- [CodeForces - 208E] Blood Cousins(k代兄弟问题)
- Urban emergency management - urban emergency communication command and dispatching system
- 小程序换行符\n失效问题解决-日常踩坑
猜你喜欢

PC端一次启动多个微信

Solution of emergency communication system for major security incidents

使用compressorjs压缩图片,优化功能,压缩所有格式的图片

el-date-picker中自定义快捷选项picker-options,动态设置禁用日期

el-select 中v-model绑定值,数据回显只显示value,不显示label

Take you to travel in space, and American photography technology provides comprehensive technical support for aerospace creative applet

Tensorflow安装后ImportError: DLL load failed: 找不到指定的模块,且国内安装缓慢

javscript获取文件真实后缀名

可视化之路(九)Arrow类详解

How does the public and Private Integrated walkie talkie realize cooperative work under multi-mode communication?
随机推荐
可视化之路(十二)Collection类详解
理解补码的要点
[ACM-ICPC 2018 沈阳赛区网络预赛] J.Ka Chang (分块+dfs序)
在项目中的定时作用
技术小白的第一篇(表达自己)
开发板如何ping通百度
青龙面板拉库命令更新【2022/4/20】收藏不走丢
如何将进程绑定到指定的CPU上
菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
LATEX使用
Meishe helps Baidu "Duka editing" to make knowledge creation easier
# 可视化常见绘图(二)折线图
Emergency medical communication solution | mesh wireless ad hoc network system
可视化常见绘图(四)柱状图
VScode
golang实现MD5,SHA256,bcrypt加密
How does the public and Private Integrated walkie talkie realize cooperative work under multi-mode communication?
枫桥学院开元名庭酒店DMR系统解决方案
go语言:在函数间传递切片
HQL语句的调优