0

为满足特定条件的元组提供别名的 SQL 语句?

不要更改数据库的内容。只是查询当(在此示例中)城市字段为波特兰时,将其别名设置为 A。但是这个表和这个字段的底层数据库,仍然是波特兰。

在此处输入图像描述

4

2 回答 2

0

通常这是通过链接查找表完成的。例如,如果您有一个包含“original_name”和“new_name”列的表,那么您的 sql 将如下所示:

Select a.address, a.addressline1, b.new_name
from atable a
join lookuptable b on a.original_name = b.original_name

您将所有查找值放在查找表中,并且可以在需要时更改(或添加)它们。与 case 子句不同,您不必更改 sql。

于 2017-06-29T03:59:56.757 回答
0

我在那种情况下使用案例IE

select 
id, 
name, 
case city 
    when 'Portland' then 
        'anotherthing'
    else city 
 end as mycolumn   -- can be the same column CITY
 from mydata;

另一种方法是使用like say another person iif

select 
id, 
name, 
iif(city = 'Portland',
    'anotherthing',
     city 
) as mycolumn   -- can be the same column CITY
 from mydata;

我希望有帮助

于 2017-06-29T03:40:26.773 回答