从案例陈述

时间:2016-10-25 13:33:21

标签: mysql sql

我的变量myvalue是一个字符串参数,其值为"TRUE""TRUE,FALSE"

myvalue = "TRUE"我只想选择N/A时。当myvalue = "TRUE,FALSE"时,我想选择0到23之间的值列表(表示当天的离散小时数),每个值都有自己的行。

这就是我所拥有的 - 我在这里缺少什么?我希望能够在不将任何内容存储在表(临时或真实)中的情况下执行此操作。我收到了语法错误。

SELECT
CASE myvalue
WHEN "TRUE"
THEN "N/A"
WHEN "TRUE,FALSE" THEN (SELECT "0" FROM DUAL 
                        UNION SELECT "1" FROM DUAL
                        UNION SELECT "2" FROM DUAL
                        UNION SELECT "3" FROM DUAL
                        UNION SELECT "4" FROM DUAL
                        UNION SELECT "5" FROM DUAL
                        UNION SELECT "6" FROM DUAL 
                        UNION SELECT "7" FROM DUAL 
                        UNION SELECT "8" FROM DUAL 
                        UNION SELECT "9" FROM DUAL 
                        UNION SELECT "10" FROM DUAL 
                        UNION SELECT "11" FROM DUAL 
                        UNION SELECT "12" FROM DUAL 
                        UNION SELECT "13" FROM DUAL 
                        UNION SELECT "14" FROM DUAL 
                        UNION SELECT "15" FROM DUAL 
                        UNION SELECT "16" FROM DUAL 
                        UNION SELECT "17" FROM DUAL 
                        UNION SELECT "18" FROM DUAL 
                        UNION SELECT "19" FROM DUAL 
                        UNION SELECT "20" FROM DUAL 
                        UNION SELECT "21" FROM DUAL 
                        UNION SELECT "22" FROM DUAL 
                        UNION SELECT "23" FROM DUAL
                        )
END as optionz
FROM
DUAL

错误讯息:Error Code: 1242. Subquery returns more than 1 row

6 个答案:

答案 0 :(得分:2)

CASE返回的表述不是SELECT cant 返回UNION

这是确定

SELECT CASE WHEN 1=2
            THEN '2'
            ELSE (SELECT 1 as d FROM DUAL)
       END as res
FROM DUAL;

ISNT OK

SELECT CASE WHEN 1=2
            THEN '2'
            ELSE (SELECT 1 as d FROM DUAL UNION
                  SELECT 2 as d FROM DUAL 
                  )
       END as res
FROM DUAL;

您需要 DEMO

--SELECT @myValue := 'TRUE';
SELECT @myValue := 'TRUE,FALSE';

SELECT val
FROM (SELECT "TRUE" as parameter, "N/A" as val FROM DUAL UNION
      SELECT "TRUE,FALSE", "1" FROM DUAL UNION
      SELECT "TRUE,FALSE", "2" FROM DUAL UNION
      SELECT "TRUE,FALSE", "3" FROM DUAL UNION
      SELECT "TRUE,FALSE", "4" FROM DUAL UNION
      SELECT "TRUE,FALSE", "5" FROM DUAL UNION
      SELECT "TRUE,FALSE", "6" FROM DUAL UNION
      SELECT "TRUE,FALSE", "7" FROM DUAL UNION
      SELECT "TRUE,FALSE", "8" FROM DUAL UNION
      SELECT "TRUE,FALSE", "9" FROM DUAL UNION
      SELECT "TRUE,FALSE", "10" FROM DUAL 
     ) t
WHERE t.parameter = @myValue ;

答案 1 :(得分:1)

以下是我的尝试:

select x from  (select myValue as y) t join
    (select 'N/A' as x, 'TRUE' as y 
    union
    select '1' as x, "TRUE,FALSE" as y
    union
    select '2' as x, "TRUE,FALSE" as y
    /* keep adding your values here */
    ) t2
on t.y = t2.y;

这使得连接有点像临时表,并且会为“TRUE / FALSE”情况提供大量行,而对于“TRUE”情况只有一行,这就是我想你想要的

答案 2 :(得分:0)

我认为你的问题有点奇怪..但也许这会有用(如果你正在使用Oracle SQL):

SELECT HOURS_OF_DAY
from
(SELECT myvalue as myvalue FROM dual) a, 
(SELECT '0' hours_of_day  FROM DUAL 
                        UNION SELECT '1' FROM DUAL
                        UNION SELECT '2' FROM DUAL
                        UNION SELECT '3' FROM DUAL
                        UNION SELECT '4' FROM DUAL
                        UNION SELECT '5' FROM DUAL
                        UNION SELECT '6' FROM DUAL 
                        UNION SELECT '7' FROM DUAL 
                        UNION SELECT '8' FROM DUAL 
                        UNION SELECT '9' FROM DUAL 
                        UNION SELECT '10' FROM DUAL 
                        UNION SELECT '11' FROM DUAL 
                        UNION SELECT '12' FROM DUAL 
                        UNION SELECT '13' FROM DUAL 
                        UNION SELECT '14' FROM DUAL 
                        UNION SELECT '15' FROM DUAL 
                        UNION SELECT '16' FROM DUAL 
                        UNION SELECT '17' FROM DUAL 
                        UNION SELECT '18' FROM DUAL 
                        UNION SELECT '19' FROM DUAL 
                        UNION SELECT '20' FROM DUAL 
                        UNION SELECT '21' FROM DUAL 
                        UNION SELECT '22' FROM DUAL 
                        UNION SELECT '23' FROM DUAL
                        ) b
WHERE myvalue = 'TRUE,FALSE'

答案 3 :(得分:0)

你有没有试过这样做:

SELECT * 
FROM DUAL
WHERE
    [YourDataColumn] IN (SELECT
                        CASE
                            WHEN myvalue = 'TRUE' THEN 'N/A'
                            ELSE (SELECT 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
                        END AS X)

答案 4 :(得分:0)

使用union all

SELECT 'N/A' as optionz
FROM dual
WHERE myvalue = 'TRUE'
UNION ALL
SELECT *
FROM (SELECT "0" as val FROM DUAL 
                        UNION SELECT "1" FROM DUAL
                        UNION SELECT "2" FROM DUAL
                        UNION SELECT "3" FROM DUAL
                        UNION SELECT "4" FROM DUAL
                        UNION SELECT "5" FROM DUAL
                        UNION SELECT "6" FROM DUAL 
                        UNION SELECT "7" FROM DUAL 
                        UNION SELECT "8" FROM DUAL 
                        UNION SELECT "9" FROM DUAL 
                        UNION SELECT "10" FROM DUAL 
                        UNION SELECT "11" FROM DUAL 
                        UNION SELECT "12" FROM DUAL 
                        UNION SELECT "13" FROM DUAL 
                        UNION SELECT "14" FROM DUAL 
                        UNION SELECT "15" FROM DUAL 
                        UNION SELECT "16" FROM DUAL 
                        UNION SELECT "17" FROM DUAL 
                        UNION SELECT "18" FROM DUAL 
                        UNION SELECT "19" FROM DUAL 
                        UNION SELECT "20" FROM DUAL 
                        UNION SELECT "21" FROM DUAL 
                        UNION SELECT "22" FROM DUAL 
                        UNION SELECT "23" FROM DUAL
                        ) t
FROM DUAL
WHERE myvalue <> 'TRUE';

答案 5 :(得分:0)

编辑: 而不是UNION链接,请执行:

SELECT a FROM (
  SELECT 'N/A' AS a, 'TRUE' AS b, @a := -1
  UNION
  SELECT @a := @a +1 AS a, 'TRUE,FALSE' AS b, NULL FROM information_schema.PARAMETERS LIMIT 24
) t
WHERE t.b = @myValue;

使用数据库中的任何表保证至少有24条记录。

使用MySQL,您可以删除FROM DUAL