使用sql从多个字段中选择不同的

时间:2009-02-13 17:13:52

标签: sql select distinct

我有5个列对应于琐事游戏数据库中的答案 - 右,错1,错2,错3,错4

我想返回所有可能的答案而不重复。我希望在不使用临时表的情况下完成此任务。是否可以使用类似的东西?:

select c1, c2, count(*)
from t
group by c1, c2

但这会返回3列。我想要一列不同的答案。

感谢您的时间

8 个答案:

答案 0 :(得分:15)

这应该为您提供表中的所有不同值。我认为你想要添加where子句来仅为特定问题选择。但是,此解决方案需要5个子查询,如果您的表很大,则可能会很慢。

SELECT DISTINCT(ans) FROM (
    SELECT right AS ans FROM answers
    UNION
    SELECT wrong1 AS ans FROM answers
    UNION
    SELECT wrong2 AS ans FROM answers
    UNION
    SELECT wrong3 AS ans FROM answers
    UNION
    SELECT wrong4 AS ans FROM answers
) AS Temp

答案 1 :(得分:8)

SELECT DISTINCT(ans) FROM (
    SELECT right AS ans FROM answers
    UNION
    SELECT wrong1 AS ans FROM answers
    UNION
    SELECT wrong2 AS ans FROM answers
    UNION
    SELECT wrong3 AS ans FROM answers
    UNION
    SELECT wrong4 AS ans FROM answers
) AS Temp

DISTINCT是多余的,因为UNION不会返回对所有列都相同的行。 (如果您想要重复,或者如果您知道不存在重复项,请使用UNION ALL以获得更快的性能)

  
    

这将为您提供包含所有答案的单个列表。如果您在一列中有多个相同答案的副本,您仍然会有重复项。

  

如果您使用UNION,则不应该是这种情况,仅当您使用UNION ALL

SELECT [value] INTO #TEMP
FROM
(
SELECT  [value] = 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 1
) AS X

(4 row(s) affected)

SELECT [value] 
FROM    #TEMP

value       
----------- 
1
2
3
1

(4 row(s) affected)

SELECT [value] 
FROM    #TEMP
UNION 
SELECT [value]
FROM    #TEMP

value       
----------- 
1
2
3

(3 row(s) affected)

答案 2 :(得分:5)

我提供了一个答案above

然而,我认为使用UNPIVOT更好的方法。

SELECT DISTINCT(ans)
FROM (
    SELECT [Name], ANS 
    FROM (
        SELECT right, wrong1, wrong2, wrong3, wrong4 
        FROM answers
    ) AS PVT
    UNPIVOT 
    (ans FOR [Name] IN (right, wrong1, wrong2, wrong3, wrong4)) AS UNPVT
) AS OUTPUT;

您可以在内部子查询中提供任何WHERE子句:

SELECT DISTINCT(ans)
FROM (
    SELECT [Name], ANS 
    FROM (
        SELECT right, wrong1, wrong2, wrong3, wrong4 
        FROM answers
        WHERE (...)
    ) AS PVT
    UNPIVOT 
    (ans FOR [Name] IN (right, wrong1, wrong2, wrong3, wrong4)) AS UNPVT
) AS OUTPUT;

答案 3 :(得分:2)

您可以使用UNION并运行5个select语句,每个语句对应一个表。它看起来像这样:

SELECT right FROM answers
UNION
SELECT wrong1 FROM answers
UNION
SELECT wrong2 FROM answers
UNION
SELECT wrong3 FROM answers
UNION
SELECT wrong4 FROM answers

这将为您提供包含所有答案的单个列表。如果您在一列中有多个相同答案的副本,您仍然会有重复项。

答案 4 :(得分:2)

“right,wrong1,wrong2,wrong3,wrong4”的列表示您的数据库设计错误。通常,列名称上的数字或字母后缀应该是一个红色标记,以重新考虑问题。

正如您所观察到的,您的设计需要您到处寻找解决典型数据缩减问题的方法。

答案 5 :(得分:1)

这是你想要的吗?

select distinct c1 from t

答案 6 :(得分:0)

MySQL AND MS SQL

SELECT
      CASE
        WHEN which = 1 THEN c1
        WHEN which = 2 THEN c2
        WHEN which = 3 THEN c3
        WHEN which = 4 THEN c4
        WHEN which = 5 THEN c5
      END AS answer,
      which
FROM mytable, (
     SELECT 1 AS which
     UNION ALL 
     SELECT 2
     UNION ALL 
     SELECT 3
     UNION ALL 
     SELECT 4
     UNION ALL 
     SELECT 5
) w

对于Oracle,请为每个号码选择添加FROM DUAL

答案 7 :(得分:0)

这是确切的答案。

SELECT (ans) FROM (
    SELECT correct AS ans FROM tGeoQuiz 
    UNION
    SELECT wrong1 AS ans FROM tGeoQuiz 
    UNION
    SELECT wrong2 AS ans FROM tGeoQuiz
    UNION
    SELECT wrong3 AS ans FROM tGeoQuiz
    UNION
    SELECT wrong4 AS ans FROM tGeoQuiz 
) AS Temp