sql - 如何编写动态Sql查询
问题描述
我有一个有 2 列的表。如果行仅包含第 1 列的值,那么我需要执行操作 a。如果它对两列都有值,我需要执行操作 b,如果行只有第 2 列,我需要执行操作操作 3。如何为此编写 SQL
解决方案
如果通过“操作”您的意思是“在结果集中的列中设置一个值”,那么您需要一个case
表达式:
select (case when col1 is not null and col2 is null then 'a'
when col1 is not null and col2 is not null then 'b'
when col1 is null and col2 is not null then 'c'
end)
from t;
推荐阅读
- python - Visual Studio Code:如何在新选项卡而不是新窗口中打开新的 Python 文件
- java - 如何使用调试外壳创建新变量
- r - 从 R 中下载和打开 xlsx 文件的问题
- python - 如何在子类中的方法之间引用
- angular - 离子限制 API 调用
- c# - 为什么 xamarin 表单列表视图中的组标题在发布模式下不可见?
- python-3.x - 在终端中安装 pytables 失败
- python - Python - while 循环 - 初学者
- php - 包装 darryldecode/cart laravel Cart::add() 功能不起作用
- javascript - Array.prototype.reduce 方法回调中的累加器上的“get”方法指的是什么?