案例语句 – 需要帮助

huangapple go评论63阅读模式
英文:

case statement - help required

问题

我是新手编写查询,我这里有一个表格,想要为它编写一个查询

No:     Desc                                 Name
1         apple                              fruit
2         apple raw                          fruit
3         apple ripe                         fruit
4         APPLE                              fruit
5         tomato                             veg
6         tomato 2                           veg

所以这里我正在寻找一个case语句,无论描述(Desc)是什么,它都应该被分类为上面的(Name)

注意:有时"Apple"可以是全大写,全小写,还可以附加数字值与名称一起
如何为此编写一个case语句
期待回应。

英文:

I am new to write queries , i have a table here and wanted to write a query for that

案例语句 – 需要帮助

No:     Desc                                 Name
1         apple                              fruit
2         apple raw                          fruit
3         apple ripe                         fruit
4         APPLE                              fruit
5         tomato                             veg
6         tomato 2                           veg

so here i am looking for a case statement , whatever the description(Desc) is it should be categorised as above (Name)

NOTE : sometimes Apple can be all upper case , all lower case , numerical value also added along with name
how do i write a case statement for that
looking forward for response

looking for case statement

答案1

得分: 1

不需要使用 case expression 来实现您的请求 - 您可能需要像这样使用 column alias

SELECT No, Name AS Description
FROM YOURTABLE

或者:如果您要求将 Name 和 Description 进行 concatenation,则解决方案可能因您使用的数据库而异,例如:

SELECT NO, Name || ' ' || Desc AS Description
FROM YOURTABLE

如果在您的数据库中使用 || 不起作用,那么可以尝试使用 +(如果使用 MS SQL Server),或者尝试:

SELECT No, CONCAT(Name, ' ', Desc) AS Description
FROM YOURTABLE

仅当您需要根据行中的某些值执行不同操作时,才需要使用 case expression,作为参考,case expression 并不复杂,例如:

SELECT No
    , CASE WHEN Desc LIKE '% %' THEN Name ELSE Desc END AS name2
FROM YOURTABLE

只有在需要根据行中的某些值执行不同操作时才需要使用 case expression

英文:

You do not need a case expression to achieve your request - you MIGHT need a column alias like this:

SELECT No, Name AS Description
FROM YOURTABLE

OR: if you are asking for a concatenation of Name and Description then the solution can differ depending on which database you use: e.g.

SELECT NO, Name|| ' ' || Desc AS Description
FROM YOURTABLE

IF using || won't work in your database then instead use + if using MS SQL Server or perhaps try:

SELECT No, CONCAT(Name,' ',Desc) AS Description
FROM YOURTABLE

For reference a case expression isn't difficult e.g.

SELECT No
    , CASE WHEN Desc LIKE '% %' THEN Name ELSE Desc END AS name2
FROM YOURTABLE

You only need a case expression if you need to do different things depending on some value(s) in the row.

huangapple
  • 本文由 发表于 2023年3月1日 08:16:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定