我尝试了几种方法在表中为表中的唯一或不同数据选择多个列,包括以下查询:
SELECT
(SELECT GROUP_CONCAT(DISTINCT a) FROM TableName),
(SELECT GROUP_CONCAT(DISTINCT b) FROM TableName),
(SELECT GROUP_CONCAT(DISTINCT c) FROM TableName);
SELECT(a, b, c) FROM TableNamegroup by 'a' order by a asc;
SELECT DISTINCT a FROM TableName
UNION
SELECT DISTINCT b FROM TableName
UNION
SELECT DISTINCT c FROM TableName;
但它们要么不起作用,要么以我无法使用的格式返回信息。我需要的是这样的格式:
+--------------------+
| a | b | c |
|--------------------|
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
etc......
没有做个别查询,有没有办法做到这一点?
答案 0 :(得分:2)
如果您需要三列,则select
需要三列。如果您想要独特的组合:
select distinct a, b, c
from TableName;
这是你想要的吗?
我怀疑您需要三列中唯一ID的列表。您可以使用MySQL中的变量来执行此操作:
select rn, max(a) as a, max(b) as b, max(c) as c
from ((select @rna := @rna + 1 as rn, a, null as b, null as c
from (select distinct a from TableName) t cross join
(select @rna := 0) const
) union all
(select @rnb := @rnb + 1 as rn, null, b, null
from (select distinct b from TableName) t cross join
(select @rnb := 0) const
) union all
(select @rnc := @rnc + 1 as rn, null, null, c
from (select distinct c from TableName) t cross join
(select @rnc := 0) const
group by c
)
) abc
group by rn
order by rn;
以下是SQL Fiddle中的工作示例。