2017-07-14 60 views
0

我是psql的新手。 我有一張桌子「寵物」如下。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 

我如何連接字符串和輸出的方式字符串,它賦予「羅恩·韋斯萊有X的寵物」(其中X = 2)?

我知道'select count(name) from pets where owner = 'Ron Weasley';'爲X = 2部分,但不知道如何在PSQL中連接並輸出字符串。

回答

0
SELECT p.owner||' has ' count(p.name)||' pets' as conc_output 
    FROM pets p 
WHERE owner = 'Ron Weasley';