我可以在SQL SELECT结果集中包含硬编码数据吗?

时间:2022-12-06 11:43:29

I have the following code: (pseudo code)

我有以下代码:(伪代码)

Select id, name, code, address from users

I json encode the data from the database like so:

我json编码数据库中的数据,如下所示:

{"id":"228","name":"Jimmy","code":"12345","address":"abc street"},      {"id":"229","name":"John","code":"22345","address":"10 1st street"}

But I actually need the data to look like this:

但我实际上需要数据看起来像这样:

{"id":"228","name":"Jimmy","code":"12345","address":"abc street", "DT_RowId": "row_228"},       {"id":"229","name":"John","code":"22345","address":"10 1st street","DT_RowId": "row_229"}

where DT_RowId is a field that has the record id, prefixed with the text "row_". Is there a way to force the sql statement to include this as a part of the result set? It might not be the best way... but the question popped into my head and so now I'm curious.

其中DT_RowId是具有记录ID的字段,前缀为文本“row_”。有没有办法强制sql语句将其作为结果集的一部分包含在内?这可能不是最好的方式......但问题突然出现在我脑海中,所以现在我很好奇。

1 个解决方案

#1


Sure, just use the concatenation operator || with a string literal:

当然,只需使用连接运算符||用字符串文字:

Select 
  id, 
  'row_' || id as DT_RowId, 
  name, 
  code, 
  address 
from users

#1


Sure, just use the concatenation operator || with a string literal:

当然,只需使用连接运算符||用字符串文字:

Select 
  id, 
  'row_' || id as DT_RowId, 
  name, 
  code, 
  address 
from users