如何根据其他列的值查看字符串或更新数据库表上的列?

发布于 2024-09-30 22:39:06 字数 919 浏览 3 评论 0原文

我有一个像这样的数据库表:-

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不可一世的女人 2024-10-07 22:39:06

使用单个 CASE 子句:

SELECT  Column1,
        Column2,
        Column3,
        Column4,       
        Column5,
        Column6,
        CASE WHEN Column1 IS NOT NULL AND Column2 IS NOT NULL THEN 'status4'
             WHEN Column2 IS NOT NULL AND Column3 IS NULL THEN 'status5'
             WHEN Column1 IS NOT NULL THEN 'status1'
             WHEN Column2 IS NOT NULL THEN 'status2'
             WHEN Column3 IS NOT NULL THEN 'status3'
             WHEN Column2 IS NOT NULL OR Column3 IS NOT NULL THEN 'status6'
        END  AS Dummy_Column
FROM    Table1

注意。 Status6 永远不会被返回,因为它将被 Status1 或 Status2 覆盖,即使 Status4 和 Status5 不适用。

Using a single CASE clause:

SELECT  Column1,
        Column2,
        Column3,
        Column4,       
        Column5,
        Column6,
        CASE WHEN Column1 IS NOT NULL AND Column2 IS NOT NULL THEN 'status4'
             WHEN Column2 IS NOT NULL AND Column3 IS NULL THEN 'status5'
             WHEN Column1 IS NOT NULL THEN 'status1'
             WHEN Column2 IS NOT NULL THEN 'status2'
             WHEN Column3 IS NOT NULL THEN 'status3'
             WHEN Column2 IS NOT NULL OR Column3 IS NOT NULL THEN 'status6'
        END  AS Dummy_Column
FROM    Table1

NB. Status6 will never be returned because it will be overridden by Status1 or Status2, even where Status4 and Status5 are not applicable.

左岸枫 2024-10-07 22:39:06

剥猫皮的方法不止一种,这就是其中之一。

SELECT  t.Column1
        , t.Column2
        , t.Column3
        , t.Column4        
        , t.Column5
        , t.Column6
        , COALESCE(Status1, Status2, Status3, Status4, Status5, Status6) AS DummyColumn
FROM    (
          SELECT  *
                  , CASE WHEN Column1 IS NOT NULL THEN 'status1' ELSE NULL END AS Status1
                  , CASE WHEN Column2 IS NOT NULL THEN 'status2' ELSE NULL END AS Status2
                  , CASE WHEN Column3 IS NOT NULL THEN 'status3' ELSE NULL END AS Status3
                  , CASE WHEN Column1 IS NOT NULL AND Column2 IS NOT NULL THEN 'status4' ELSE NULL END AS Status4
                  , CASE WHEN Column2 IS NOT NULL AND Column3 IS NULL THEN 'status5' ELSE NULL END AS Status5
                  , CASE WHEN Column2 IS NOT NULL OR Column3 IS NOT NULL THEN 'status6' ELSE NULL END AS Status6
          FROM    Table1
        ) t

请注意,这可以使用 SQL Server 进行,但由于不涉及任何神秘的结构,因此将其转换为 SyBase 应该相对容易。

There's more than one way to skin a cat, this is one of them.

SELECT  t.Column1
        , t.Column2
        , t.Column3
        , t.Column4        
        , t.Column5
        , t.Column6
        , COALESCE(Status1, Status2, Status3, Status4, Status5, Status6) AS DummyColumn
FROM    (
          SELECT  *
                  , CASE WHEN Column1 IS NOT NULL THEN 'status1' ELSE NULL END AS Status1
                  , CASE WHEN Column2 IS NOT NULL THEN 'status2' ELSE NULL END AS Status2
                  , CASE WHEN Column3 IS NOT NULL THEN 'status3' ELSE NULL END AS Status3
                  , CASE WHEN Column1 IS NOT NULL AND Column2 IS NOT NULL THEN 'status4' ELSE NULL END AS Status4
                  , CASE WHEN Column2 IS NOT NULL AND Column3 IS NULL THEN 'status5' ELSE NULL END AS Status5
                  , CASE WHEN Column2 IS NOT NULL OR Column3 IS NOT NULL THEN 'status6' ELSE NULL END AS Status6
          FROM    Table1
        ) t

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文