SQL 查询将多条记录中的值组合到单个列中
Posted
技术标签:
【中文标题】SQL 查询将多条记录中的值组合到单个列中【英文标题】:SQL Query to combine values from multiple records into a single column 【发布时间】:2018-06-21 17:12:50 【问题描述】:在我的表格中,每条记录最多可以有 30 个不同的预算代码。 我需要一个将表中的所有预算代码作为单列返回的查询。 查询我有错误指出子查询返回的值超过 1 个。
SELECT
(SELECT [budgetCode1] FROM TABLE_NAME),
(SELECT [budgetCode2] FROM TABLE_NAME),
(SELECT [budgetCode3] FROM TABLE_NAME),
(SELECT [budgetCode4] FROM TABLE_NAME),
(SELECT [budgetCode5] FROM TABLE_NAME),
(SELECT [budgetCode6] FROM TABLE_NAME),
(SELECT [budgetCode7] FROM TABLE_NAME),
(SELECT [budgetCode8] FROM TABLE_NAME),
(SELECT [budgetCode9] FROM TABLE_NAME),
(SELECT [budgetCode10] FROM TABLE_NAME),
(SELECT [budgetCode11] FROM TABLE_NAME),
(SELECT [budgetCode12] FROM TABLE_NAME),
(SELECT [budgetCode13] FROM TABLE_NAME),
(SELECT [budgetCode14] FROM TABLE_NAME),
(SELECT [budgetCode15] FROM TABLE_NAME),
(SELECT [budgetCode16] FROM TABLE_NAME),
(SELECT [budgetCode17] FROM TABLE_NAME),
(SELECT [budgetCode18] FROM TABLE_NAME),
(SELECT [budgetCode19] FROM TABLE_NAME),
(SELECT [budgetCode20] FROM TABLE_NAME),
(SELECT [budgetCode21] FROM TABLE_NAME),
(SELECT [budgetCode22] FROM TABLE_NAME),
(SELECT [budgetCode23] FROM TABLE_NAME),
(SELECT [budgetCode24] FROM TABLE_NAME),
(SELECT [budgetCode25] FROM TABLE_NAME),
(SELECT [budgetCode26] FROM TABLE_NAME),
(SELECT [budgetCode27] FROM TABLE_NAME),
(SELECT [budgetCode28] FROM TABLE_NAME),
(SELECT [budgetCode29] FROM TABLE_NAME),
(SELECT [budgetCode30] FROM TABLE_NAME) AS BudgetCodes
FROM TABLE_NAME
【问题讨论】:
重新考虑您的宽数据库表,因为这是低效的存储并且无法正确扩展。创建一个 BudgetCodes 表,其中包含指向 TableName 的外键链接,并为 1-30 和 one 列维护 one 列指示符对应数字或字符串值的指示符。 正如@RolandmysqlDBA 的concluded:没有替代品或创可贴可以弥补糟糕的设计。请,为了您将来的理智,今天规范化该表!!! 我非常想规范化我的表格。不幸的是,这个表是通过另一个系统的数据转储创建的。我对表结构没有发言权。 【参考方案1】:您的查询将返回 30 列,如果 TABLE_NAME
有多个记录,则会出错。相反,您想在此处使用 UNION 查询:
SELECT [budgetCode1] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode2] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode3] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode4] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode5] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode6] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode7] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode8] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode9] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode10] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode11] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode12] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode13] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode14] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode15] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode16] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode17] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode18] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode19] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode20] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode21] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode22] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode23] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode24] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode25] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode26] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode27] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode28] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode29] FROM TABLE_NAME
UNION ALL
SELECT [budgetCode30] FROM TABLE_NAME
您还可以添加从哪个budgetcode 列派生的:
SELECT [budgetCode1],"budgetCode1" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode2],"budgetCode2" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode3],"budgetCode3" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode4],"budgetCode4" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode5],"budgetCode5" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode6],"budgetCode6" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode7],"budgetCode7" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode8],"budgetCode8" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode9],"budgetCode9" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode10],"budgetCode10" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode11],"budgetCode11" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode12],"budgetCode12" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode13],"budgetCode13" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode14],"budgetCode14" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode15],"budgetCode15" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode16],"budgetCode16" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode17],"budgetCode17" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode18],"budgetCode18" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode19],"budgetCode19" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode20],"budgetCode20" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode21],"budgetCode21" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode22],"budgetCode22" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode23],"budgetCode23" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode24],"budgetCode24" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode25],"budgetCode25" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode26],"budgetCode26" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode27],"budgetCode27" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode28],"budgetCode28" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode29],"budgetCode29" as budgetcode FROM TABLE_NAME
UNION ALL
SELECT [budgetCode30],"budgetCode30" as budgetcode FROM TABLE_NAME AS BudgetCodes
FROM TABLE_NAME
【讨论】:
我们 SQL 人员需要强调这样的次优数据库设计,这会导致繁琐、复杂的查询! @Parfait 完全同意。为每个预算代码设置列是一个糟糕的选择,并且会导致一些讨厌的(和缓慢的)联合查询以将其置于正确的格式(就像我们在这里所做的那样)。糟糕。 能否修改此查询以排除 NULL 或空字符串值? 您必须将其包装在子查询中并添加 WHERE 子句WHERE [budgetcode1] IS NULL
或.. 将 WHERE 子句添加到每个单独的 SELECT 语句中。以上是关于SQL 查询将多条记录中的值组合到单个列中的主要内容,如果未能解决你的问题,请参考以下文章