SQL谜题(加减符号替代)

问题:将以下字符串”.1.2.3.4.5.6.7.8.9 = 1“中的符号点(.)更改为符号加(+)或符号(-),有多少种方法?请用SQL解决此问题
计算过程:

CREATE TABLE #(VAL INT)
INSERT INTO #
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9
CREATE TABLE #1(VAL CHAR(1)) INSERT INTO #1
SELECT '+' UNION ALL
SELECT '-' ;
WITH A AS(
SELECT #1.VAL VAL1,#.VAL FROM # JOIN #1 ON 1=1) SELECT
RTRIM(A1.VAL1)+RTRIM(A1.VAL)+
RTRIM(A2.VAL1)+''+RTRIM(A2.VAL)+
RTRIM(A3.VAL1)+''+RTRIM(A3.VAL)+
RTRIM(A4.VAL1)+''+RTRIM(A4.VAL)+
RTRIM(A5.VAL1)+''+RTRIM(A5.VAL)+
RTRIM(A6.VAL1)+''+RTRIM(A6.VAL)+
RTRIM(A7.VAL1)+''+RTRIM(A7.VAL)+
RTRIM(A8.VAL1)+''+RTRIM(A8.VAL)+
RTRIM(A9.VAL1)+''+RTRIM(A9.VAL) Result
FROM A A1,A A2,A A3,A A4,A A5,A A6,A A7,A A8,A A9
WHERE A1.VAL=1 AND A2.VAL=2 AND A3.VAL=3 AND A4.VAL=4 AND A5.VAL=5 AND A6.VAL=6 AND A7.VAL=7 AND A8.VAL=8 AND A9.VAL=9
AND
CASE WHEN A1.VAL1='-' THEN -A1.VAL ELSE A1.VAL END+
CASE WHEN A2.VAL1='-' THEN -A2.VAL ELSE A2.VAL END+
CASE WHEN A3.VAL1='-' THEN -A3.VAL ELSE A3.VAL END+
CASE WHEN A4.VAL1='-' THEN -A4.VAL ELSE A4.VAL END+
CASE WHEN A5.VAL1='-' THEN -A5.VAL ELSE A5.VAL END+
CASE WHEN A6.VAL1='-' THEN -A6.VAL ELSE A6.VAL END+
CASE WHEN A7.VAL1='-' THEN -A7.VAL ELSE A7.VAL END+
CASE WHEN A8.VAL1='-' THEN -A8.VAL ELSE A8.VAL END+
CASE WHEN A9.VAL1='-' THEN -A9.VAL ELSE A9.VAL END=1

执行结果:

SQL谜题(加减符号替代)

上一篇:Centos6 下安装Nginx+Mysql+PHP


下一篇:ballerina 学习十九 安全编程