当前位置:网站首页>如何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
边栏推荐
- Machine vision series (01) -- Overview
- 学习笔记5-梯度爆炸和梯度消失(K折交叉验证)
- ESP32学习-向工程项目添加文件夹
- Javscript gets the real suffix of the file
- golang实现正则匹配:密码包含至少一位数字,字母和特殊字符,且长度8-16
- [COCI]Lampice (二分+树分治+字符串哈希)
- 免费开源智能充电桩物联网SAAS云平台
- PyTorch 13. Nested functions and closures (dog head)
- [牛客练习赛68]牛牛的粉丝(矩阵快速幂之循环矩阵优化)
- 记录一些npm 有关的问题(杂乱记录)
猜你喜欢

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

通过sparksql读取presto中的数据存到clickhouse

可视化常见问题解决方案(七)画图刻度设置解决方案

可视化之路(十二)Collection类详解

可视化常见问题解决方案(八)共享绘图区域问题解决方案

manjaro安装与配置(vscode,微信,美化,输入法)

Urban emergency management - urban emergency communication command and dispatching system

菜菜的刷题日记 | 238.除自身以外数组的乘积

江宁医院DMR系统解决方案

枫桥学院开元名庭酒店DMR系统解决方案
随机推荐
后台管理系统框架,总有你想要的
Discussion on frame construction and technology selection of short video platform
golang实现正则匹配:密码包含至少一位数字,字母和特殊字符,且长度8-16
golang实现一个带Web界面的五险一金计算器
[COCI]Lampice (二分+树分治+字符串哈希)
golang实现MD5,SHA256,bcrypt加密
可视化常见问题解决方案(七)画图刻度设置解决方案
小程序换行符\n失效问题解决-日常踩坑
嵌入式相关面经(一)
SDC intelligent communication patrol management system of Nanfang investment building
The people of Beifeng have been taking action
manjaro安装与配置(vscode,微信,美化,输入法)
免费开源智能充电桩物联网SAAS云平台
小程序wx.previewMedia相关问题解决-日常踩坑
简单易懂的子集dp
组合数求解与(扩展)卢卡斯定理
go语言:在函数间传递切片
go语言数组操作
关于'enum'枚举类型以及结构体的问题。
el-table的数据更新后,页面中数据未更新this.$forceUpdate()无效果