【问题标题】:PSQL: string concatenation and output stringPSQL:字符串连接和输出字符串
【发布时间】:2017-07-14 22:21:11
【问题描述】:

我是 psql 的新手。 我有一张“宠物”桌,如下所示。

    name     | species |       owner        | gender |     color
-------------+---------+--------------------+--------+---------------
 Nagini      | snake   | Lord Voldemort     | female | green
 Hedwig      | owl     | Harry Potter       | female | snow white
 Scabbers    | rat     | Ron Weasley        | male   | unspecified
 Pigwidgeon  | owl     | Ron Weasley        | male   | grey
 Crookshanks | cat     | Herminone Granger  | male   | ginger
 Mrs Norris  | cat     | Argus Filch        | female | dust-coloured
 Trevor      | toad    | Neville Longbottom | male   | brown

如何连接字符串并以给出“Ron Weasley 有 X 个宠物”(其中 X = 2)的方式输出字符串?

我知道 X=2 部分的 'select count(name) from pets where owner = 'Ron Weasley';',但不确定如何在 PSQL 中连接和输出字符串。

【问题讨论】:

    标签: string psql string-concatenation


    【解决方案1】:
    SELECT p.owner||' has ' count(p.name)||' pets' as conc_output
      FROM pets p
     WHERE owner = 'Ron Weasley';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-29
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 2021-08-10
      相关资源
      最近更新 更多