如何根据其他列的值查看字符串或更新数据库表上的列?
我有一个像这样的数据库表:-
Table1
Column1 : Varchar(50)
Column2 : Varchar(50)
Column3 : Boolean
Column4 : Varchar(50)
Column5 : Varchar(50)
我想在运行 SQL 语句时创建一个虚拟列 - 或向表中添加一个新列 - 以便该列的值将包含一个指示行状态的字符串项目取决于这些列中的多个。我的意思是这些列是否有空值。
表示这一点的逻辑方式:-
Select switch DummyColumn
"status1" Case column1 IS NOT NULL
"status2" Case column2 IS NOT NULL
"status3" Case column3 IS NOT NULL
"status4" Case column1 IS NOT NULL AND column2 IS NOT NULL
"status5" Case column2 IS NOT NULL AND column3 IS NULL
"status6" Case column2 IS NOT NULL OR column3 IS NOT NULL
From Table1;
在业务逻辑中,我可以说我想显示一个字符串,指示数据库中项目的状态,具体取决于许多其他状态列,这些状态列可能是布尔值或字符串,并且根据这些表中存储的值,该状态可以有许多值列。
我的数据库引擎是Sybase。
我如何构建 SQL 查询来做到这一点?这件事可以叫什么?我怎样才能找到或搜索它?我不知道我可以在什么主题中找到这个?我搜索有关合并、串联、替换、NVL 和联合的内容,但没有找到我想要的内容。
提前致谢,我希望我能很好地表达我的问题,并且它是重复的,我希望找到一个好的答案为了尽快..
I have a database table like that :-
Table1
Column1 : Varchar(50)
Column2 : Varchar(50)
Column3 : Boolean
Column4 : Varchar(50)
Column5 : Varchar(50)
I would like to create a dummy column -or add a new column to the table- when running a SQL statement so that the value is that column will hold a string indicating the satus of the row item depeing on more than one of these columns. I mean wether these columns have Null values or not.
A logical way to represent that:-
Select switch DummyColumn
"status1" Case column1 IS NOT NULL
"status2" Case column2 IS NOT NULL
"status3" Case column3 IS NOT NULL
"status4" Case column1 IS NOT NULL AND column2 IS NOT NULL
"status5" Case column2 IS NOT NULL AND column3 IS NULL
"status6" Case column2 IS NOT NULL OR column3 IS NOT NULL
From Table1;
In Business Logic, I can say I want to make display a string indicating the status of an Item in the database depeding on many other status columns which may be boolen or string and this status can have many values according to the values stored in these table columns.
My database engine is Sybase.
How I can build my SQL query to do that ? what may be this matter called ? How I can find or search for that ? I do not know in what topic I may find this ? I search about Merging, Concatening, Replacing, NVL, and Union but i do not find what I seek for..
Thanks in Advance and I hope that I express my question well and that it is a duplicate and I hope to find a good answer for it as soon as possible ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用单个 CASE 子句:
注意。 Status6 永远不会被返回,因为它将被 Status1 或 Status2 覆盖,即使 Status4 和 Status5 不适用。
Using a single CASE clause:
NB. Status6 will never be returned because it will be overridden by Status1 or Status2, even where Status4 and Status5 are not applicable.
剥猫皮的方法不止一种,这就是其中之一。
请注意,这可以使用 SQL Server 进行,但由于不涉及任何神秘的结构,因此将其转换为 SyBase 应该相对容易。
There's more than one way to skin a cat, this is one of them.
Note that this works using SQL Server but as there are no arcane constructions involved, it should be relatively easy to convert this to SyBase.