【问题标题】:How can i merge two columns of a table in sql as a sentence?如何将sql中表的两列合并为一个句子?
【发布时间】:2019-05-09 23:43:41
【问题描述】:

我是数据库编程的初学者。我正在做我的作业,我得到了一个关于 sql 查询的任务。我的任务是在一个句子中合并一个表(人)的 3 列(名字、姓氏、性别)。

例如:

"John Anderson is a male."

"Julia Smith is a female."

我写了以下查询:

SELECT first_name, last_name, ||' is a '|| sex ||'.'||    
FROM person    
LIMIT 10

我得到这个错误:

ERROR: Operator does not exist: text ||

LINE 1:select first_name, last_name, ||' is a '|| sex ||'.'||
                                                           ^ 
HINT: No operator matches the specified name and argument type. You may have to add explicit type conversions.

如果有人能给我一个想法或解决方案,那对我来说会很棒。

【问题讨论】:

  • 您使用的是什么 DBMS? MySQL、PostgreSQL、SQL Server、甲骨文???
  • 我认为类似于select first_name + ' ' + last_name + ' is a ' + sex from person limit 10,至少在sql server中。
  • 我在 pgAdmin 平台中使用 PostgreSQL
  • 也许这个错误是因为你不需要最后一个 ||在你的句子中。

标签: sql database string select selection


【解决方案1】:

你写SELECT表达式的方式有错误,应该是

SELECT first_name || ' ' || last_name || ' is a ' || sex || '.'    
FROM person    
LIMIT 10

Demo on dbfiddle

【讨论】:

  • 非常感谢您的回答。我的问题已经解决了。
  • @d.kai 不用担心。我很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多