当前位置:网站首页>如何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
边栏推荐
- Hanlp分词器(通过spark)
- PC端一次启动多个微信
- pytorch:关于GradReverseLayer实现的一个坑
- 可视化常见问题解决方案(七)画图刻度设置解决方案
- The difference between null and undefined
- 快速下载vscode的方法
- Discussion on frame construction and technology selection of short video platform
- Us photo cloud editing helps BiliBili upgrade its experience
- 学习笔记6-几种深度学习卷积神经网络的总结
- 小程序wx.previewMedia相关问题解决-日常踩坑
猜你喜欢
随机推荐
[2020WC Day2]F.采蘑菇的克拉莉丝(子树和查询、轻重儿子思想)
Object.create()原理,Object.create()规范,手写Object.create(),Object.create()用法
Emergency medical communication solution | mesh wireless ad hoc network system
[CodeForces - 208E] Blood Cousins(k代兄弟问题)
如何将进程绑定到指定的CPU上
使用el-popconfirm和el-backtop不生效
安装tui-editor失败,快速解决方案
VIM使用
Javscript gets the real suffix of the file
go语言数组操作
小程序换行符\n失效问题解决-日常踩坑
可视化之路(十一)matplotlib颜色详解
数论之阶与原根讲解
基于可视化结构的身份证号码校验系统-树莓派实现
记录一个查询兼容性的网站,String.replaceAll()兼容性报错
两个线程交互打印奇偶数字
江宁医院DMR系统解决方案
技术小白的第一篇(表达自己)
H5案例开发
jvm知识点汇总-持续更新









