连接两列并且只有一个回车符

时间:2017-02-09 20:15:53

标签: sqlite

我正在尝试将两列连接成一个并设法完成它。但是我不想要两列的回车,而只想要两列之间有一个空格。

SELECT t1.save_line || t2.save_line as save_line 
FROM (
    SELECT * 
    FROM save_output_table 
    WHERE save_type = 'R' and seq_id = '0' and execution_id = '292' 
) t1 
    JOIN (
        SELECT * 
        FROM save_output_table 
        WHERE save_type = 'R' and seq_id = '0' and execution_id = '286'
) t2 ON t1.line_id = t2.line_id

输出:

+------------+
| Save_line  |
+------------+
| 18         |
| 12         |
|------------|
| 23         |
| 22         |
+------------+

预期:

+------------+
| Save_line  |
+------------+
| 18 12      |
| 23 22      |
+------------+

1 个答案:

答案 0 :(得分:2)

首先,查询更简单地写为:

SELECT (sov.save_line || sov2.save_line) as save_line
FROM save_output_table sov JOIN 
     save_output_table sov2
     on sov.line_id = sov2.line_id
WHERE sov.save_type = 'R' and sov.seq_id = '0' and sov.execution_id = '292' and
      sov2.save_type = 'R' and sov2.seq_id = '0' and sov2.execution_id = '286' ;

然后,您可以用空格替换换行符。这可能取决于您的操作系统,但类似这样:

SELECT replace(sov.save_line || sov2.save_line0, char(13), ' ') as save_line

在Windows中,您可能需要替换CR和LF:

SELECT replace(replace(sov.save_line || sov2.save_line0, char(13), ' '), char(10), '') as save_line