English 中文(简体)
使用SQL从多个字段中选择不同的内容。
原标题:
  • 时间:2009-02-13 17:13:51
  •  标签:

我的数据库里有5个对应于小知识问答游戏的答案的列 - 正确,错误1,错误2,错误3,错误4。

我想返回所有可能的答案,且不带重复项。我希望能够在不使用临时表的情况下完成这个任务。是否可以使用类似于这样的东西?

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

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

感谢您的时间。

最佳回答

这应该给你所有表中不同的值。我假设你想要添加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
问题回答
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)

I provided one answer 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;

你可以使用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

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

“正确、错误1、错误2、错误3、错误4”这些列意味着您的数据库设计存在问题。通常情况下,在列名称上加上数字或字母后缀应该引起重新考虑问题的警示。

正如您所观察的那样,您的设计需要您绕过来解决一个典型的数据降维问题。

这是你想要的吗?

选择去重c1从t。

MySQLMS 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

这是确切的答案。

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 




相关问题
热门标签