SQL 根据查询结果返回自定义值 [英] SQL returning custom values based on query results

查看:31
本文介绍了SQL 根据查询结果返回自定义值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据查询进行返回不同自定义值的查询.很难解释,这里有一个例子应该会更清楚.

is it possible to make a query that returns a different custom value based on a query. its hard to explain, here is an example that should make it more clear.

我在表中有什么:

  • 1
  • 2
  • 3

这是我想要返回的:

  • 一个
  • 两个
  • 三个

类似于 if 语句...

something like an if statement...

推荐答案

您需要一个 case 语句.根据您的 SQL 风格,这样的事情应该可以工作:

You want a case statement. Depending on your flavor of SQL, something like this should work:

select 
    bar = case 
               when foo = 1 then 'one'
               when foo = 2 then 'two'
               else 'baz' 
          end
from myTable 

这篇关于SQL 根据查询结果返回自定义值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆