假设我a, b c, d
在MySQL数据库的表中有列。我想做的是选择表中所有这4列的所有唯一值(只有唯一值)。我尝试了类似的东西:
SELECT DISTINCT a,b,c,d FROM my_table;
SELECT DISTINCT a,b,c,d FROM my_table GROUP BY a,b,c,d;
这些都不起作用。有人可以帮忙吗?
谢谢
注意我想要分别显示各列的不同值a, b, c d
。不是价值的独特组合
假设我a, b c, d
在MySQL数据库的表中有列。我想做的是选择表中所有这4列的所有唯一值(只有唯一值)。我尝试了类似的东西:
SELECT DISTINCT a,b,c,d FROM my_table;
SELECT DISTINCT a,b,c,d FROM my_table GROUP BY a,b,c,d;
这些都不起作用。有人可以帮忙吗?
谢谢
注意我想要分别显示各列的不同值a, b, c d
。不是价值的独特组合
Answers:
能帮上忙吗?
select
(SELECT group_concat(DISTINCT a) FROM my_table) as a,
(SELECT group_concat(DISTINCT b) FROM my_table) as b,
(SELECT group_concat(DISTINCT c) FROM my_table) as c,
(SELECT group_concat(DISTINCT d) FROM my_table) as d
REPLACE(group_concat(DISTINCT d),',',' ')
我知道这个问题太老了:
select a, b from mytable group by a, b
将给您所有的组合。
对您想要的结果进行猜测,所以也许这是您想要的查询,然后
SELECT DISTINCT a FROM my_table
UNION
SELECT DISTINCT b FROM my_table
UNION
SELECT DISTINCT c FROM my_table
UNION
SELECT DISTINCT d FROM my_table
SELECT DISTINCT value FROM ( SELECT DISTINCT a AS value FROM my_table UNION SELECT DISTINCT b AS value FROM my_table UNION SELECT DISTINCT c AS value FROM my_table ) AS derived
SELECT e,f FROM ( SELECT DISTINCT zdjh AS e FROM
tj_xhqd` UNION SELECT DISTINCT sjsj AS f FROM tj_xhqd
UNION SELECT DISTINCT xhqd AS g FROM tj_xhqd
)Error Code: 1054 Unknown column 'f' in 'field list'
您的两个查询都是正确的,应该给您正确的答案。
我建议使用以下查询来解决您的问题。
SELECT DISTINCT a,b,c,d,count(*) Count FROM my_table GROUP BY a,b,c,d
order by count(*) desc
那就是添加count(*)字段。这将使您了解使用group命令消除了多少行。
where
条件的方法?我只需要计数大于1的行。尝试过select distinct a,b,c,count(*) from MyTempTable group by a,b,c order by count(*) desc where count(*)>1;
但没有用。
having count(*) > 1
没有where
因为这是汇总查询。顺便说一句,我的回答不能完全解决上述OP的问题。反正很乐意提供帮助。
distinct a,b,c,count(*) from MyTable group by a,b,c having count(*) > 1 order by count(*) desc
这将在所有列中提供DISTINCT值:
SELECT DISTINCT value
FROM (
SELECT DISTINCT a AS value FROM my_table
UNION SELECT DISTINCT b AS value FROM my_table
UNION SELECT DISTINCT c AS value FROM my_table
) AS derived