答案 0 :(得分:2)
我并不认为我理解你要做的事情,但听起来你可能真的想要这样的事情:
SELECT SKU_SIZE_PART1 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS SKU_SIZE_PART
FROM SKU_DATA
ORDER BY SKU_SIZE_PART DESC
将返回单列中所有不同的SKU_SIZE_PART1 / 2/3值,而不是三列中的所有不同(SKU_SIZE_PART1,SKU_SIZE_PART2,SKU_SIZE_PART3)三元组。
答案 1 :(得分:1)
在多次阅读您的问题之后,我认为这可能是您正在寻找的内容:
SELECT SKU_SIZE_PART1 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS ssp
FROM SKU_DATA
ORDER BY ssp DESC
答案 2 :(得分:1)
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1)
ORDER BY d.sku_size_part1 DESC
请注意,这会随机选择或多或少的行
虽然所有sku_size_parts都来自同一行,但会隐藏很多值
如果要使查询稳定,则需要在内部子选择中添加having
子句。
这样的事情:
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1
HAVING s.sku_size_part2 = MIN(s.sku_size_part2)
AND s.sku_size_part3 = MIN(s.sku_size_part3))
ORDER BY d.sku_size_part1 DESC
或者你想要@ bfavaretto的UNION变体。
答案 3 :(得分:-1)
DISTINCT选择一组不同的行,而不是列......这里的假设/问题是如何压缩多列。如果你有下表
sku1 | sku2 | sku3
---------------------
a | a | b
b | b | b
告诉它选择destinct将返回两行,因为它们都不是不同的,你不能只删除第三列,因为那时行数据会不一致。如果您希望一个表中的所有内容都可以使用子查询执行此操作。
SELECT (SELECT DISTINCT SKU_SIZE_PART1 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part1, (SELECT DISTINCT SKU_SIZE_PART2 FROM SKU_DATA ORDER BY SKU_SIZE_PART2 DESC)
as part2, (SELECT DISTINCT SKU_SIZE_PART3 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part3 FROM SKU_DATA
你可以阅读一下DISTINCT的工作原理,了解为什么你不能只做SELECT DISTINCT SKU_SIZE_PART1,PART2,PART3。像This Link
这样的地方