在sql中将一列折叠成一列?

时间:2010-01-27 05:02:17

标签: sql

说我有一张这样的表:

|x1|y1|

|0 |1 |

|2 |4 |

|3 |2 |

我知道我可以这样做:

SELECT a.x1 AS .., a.y1 AS .., 
       b.x1 AS .., b.y1 AS .., 
       c.x1 AS .., c.y1 AS .., 
       d.x1 AS .., d.y1 AS .. 
FROM xytable a, xytable b, xytable c, xytable d 
WHERE ...

但是,在我的结果表中,我想将a.x1和a.y1一起输出,作为 one 列下的元组。换句话说,我希望:

|c1          |c2            |c3             |c4           |

(a.x1, a.y1) | (a.x2, a.y2) |  (a.x3, a.y3) | (a.x4, a.y4)

有可能吗?

2 个答案:

答案 0 :(得分:3)

如果您使用的是MySQL,则可以使用CONCAT功能:

SELECT 
     CONCAT('(', a.x1, ', ', a.y1, ')') AS c1
FROM 
    xytable a, xytable b, xytable c, xytable d 
WHERE 
    ...

在SQL Server中,您可以与+运算符连接以连接字符串:

SELECT 
    '(' + a.x1 + ', ' + a.y1 + ')' AS c1
FROM 
    xytable a, xytable b, xytable c, xytable d 
WHERE 
    ...

答案 1 :(得分:2)

作为字符串?

您可以执行类似

的操作
SELECT '(' + convert(varchar, x1) + ',' + convert(varchar, y1) + ')' as "col"