如何使用ORDER BY连接行

时间:2019-11-27 16:13:11

标签: sql concatenation

已编辑:在SQL中,我将带有文本列的表连接到单独的表(后者在下面的示例中未描述)。我想显示来自每一行的文本,这些文本串联成一行。我想使用一个单独的列(“行”)来确定文本的连接顺序。

>>> rows[0].xpath('.//*[@id="horizontal-list"]')
[]
>>> rows[0].xpath('.')
[<Selector xpath='.' data='<ul id="horizontal-list">\n\t\t\n<li><a href'>]
>>> rows[0].xpath('.//li[1]/a/@href')
[<Selector xpath='.//li[1]/a/@href' data='http://www.lawncaredirectory.com/statedi'>]
>>> rows[0].xpath('.//li[1]/a/@href').get()
'http://www.lawncaredirectory.com/statedirectory.php?state=Alabama'

单列/行输出:

li[1]

2 个答案:

答案 0 :(得分:1)

在postgresql中尝试一下:

select  string_agg(Str,' ' order by Line ) as Sentence FROM Table1
group by Col;

输出:

sentence
This is a sentence

http://sqlfiddle.com/#!17/e84f9/4

答案 1 :(得分:1)

在MySql中尝试:

select  group_concat(Str order by Line separator ' ' ) as Sentence FROM Table1
group by Col;

结果:

sentence
This is a sentence

http://sqlfiddle.com/#!9/e84f9e/5