当前位置:网站首页>如何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
边栏推荐
- 可视化常见问题解决方案(九)背景颜色问题
- Meishe helps Baidu "Duka editing" to make knowledge creation easier
- van-uploader上传图片实现过程、使用原生input实现上传图片
- 组合数求解与(扩展)卢卡斯定理
- PyTorch 22. Pytorch common code snippet collection
- 社区版阿里MQ普通消息发送订阅Demo
- kaggle-房价预测实战
- remote: Support for password authentication was removed on August 13, 2021.
- Discussion on frame construction and technology selection of short video platform
- Pycharm
猜你喜欢
菜菜的刷题日记 | 蓝桥杯 — 十六进制转八进制(纯手撕版)附进制转换笔记
Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
可视化常见问题解决方案(八)数学公式
记录阿里云服务器挖矿程序处理
可视化常见绘图(一)堆叠图
Source Insight 4.0常见问题
可视化常见绘图(四)柱状图
不需要破解markdown编辑工具Typora
Jiangning hospital DMR system solution
Beifeng communication helps Zhanjiang fire brigade build PDT wireless communication system
随机推荐
Jupyter Notebook 安装
JDBC连接池
PC端一次启动多个微信
学习笔记5-梯度爆炸和梯度消失(K折交叉验证)
Solution of wireless intercom system in Commercial Plaza
Emergency communication system for flood control and disaster relief
The difference between null and undefined
Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference
Applet Wx Previewmedia related problem solving - Daily stepping on the pit
利用mysql-binlog恢复数据
manjaro安装与配置(vscode,微信,美化,输入法)
pytorch:关于GradReverseLayer实现的一个坑
USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference
anaconda3安装
PyTorch 20. Pytorch tips (continuously updated)
[ACM-ICPC 2018 沈阳赛区网络预赛] J.Ka Chang (分块+dfs序)
安装tui-editor失败,快速解决方案
可视化常见问题解决方案(八)数学公式
Mysql的存储引擎
HQL语句的调优