我有多行需要不同的concat函数。 该表看起来像这样
A B C D E F
-----------
1 H E Y
2 T H E E R
3 W O R D L
我想添加另一个单词concatenated,我已经这样做了
SELECT *, CONCAT(B, C, D) AS G FROM table
显然它会返回
A B C D E F G
-------------
1 H E Y HEY
2 T H E E R THE
3 W O R D L WOR
有没有办法让结果
A B C D E F G
-------------
1 H E Y HEY
2 T H E E R THERE
3 W O R D L WORLD
答案解决了简化问题:
如何将这3个选择语句合并为1?
SELECT *, CONCAT(C, B, E, D, F) AS G FROM table WHERE A = 1;
SELECT *, CONCAT(G, C, D, B, E, F) AS G FROM table WHERE A = 2;
SELECT *, CONCAT(D, E, F, C, B) AS G FROM table WHERE A = 3;
答案 0 :(得分:0)
SELECT *,
CASE A
WHEN 1 THEN CONCAT(C, B, E, D, F)
WHEN 2 THEN CONCAT(C, D, B, E, F)
WHEN 3 THEN CONCAT(D, E, F, C, B)
END AS G
FROM table
WHERE A IN (1, 2, 3)
或者您可以使用UNION
组合单独的查询:
SELECT *, CONCAT(C, B, E, D, F) AS G
FROM table
WHERE A = 1
UNION
SELECT *, CONCAT(C, D, B, E, F) AS G
FROM table
WHERE A = 2
UNION
SELECT *, CONCAT(D, E, F, C, B) AS G
FROM table
WHERE A = 3