[英]MySQL SELECT DISTINCT multiple columns
Let's say I have columns a, b c, d
in a table in a MySQL database.假设我在 MySQL 数据库的表中有列
a, b c, d
。 What I'm trying to do is to select the distinct values of ALL of these 4 columns in my table (only the distinct values).我想要做的是 select 我表中所有这 4 列的不同值(只有不同的值)。 I tried stuff like:
我尝试过类似的东西:
SELECT DISTINCT a,b,c,d FROM my_table;
SELECT DISTINCT a,b,c,d FROM my_table GROUP BY a,b,c,d;
None of those worked.这些都没有奏效。 Can anybody help out here?
有人可以在这里帮忙吗?
Thank you谢谢
NOTE I want the distinct values of the columns a, b, c d
separately.注意我想要
a, b, c d
列的不同值。 Not the distinct combination of values不是价值的独特组合
I know that the question is too old, anyway:我知道这个问题太老了,无论如何:
SELECT a, b
FROM mytable
GROUP BY a, b;
This will give your all the combinations.这将为您提供所有组合。
can this help?这有帮助吗?
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
Taking a guess at the results you want so maybe this is the query you want then猜测你想要的结果,所以也许这就是你想要的查询
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
另一种简单的方法是使用concat()
SELECT DISTINCT(CONCAT(a,b)) AS cc FROM my_table GROUP BY (cc);
Both your queries are correct and should give you the right answer.您的两个查询都是正确的,应该会给您正确的答案。
I would suggest the following query to troubleshoot your problem.我建议使用以下查询来解决您的问题。
SELECT DISTINCT a,b,c,d,count(*) Count FROM my_table GROUP BY a,b,c,d
order by count(*) desc
That is add count(*) field.即添加 count(*) 字段。 This will give you idea how many rows were eliminated using the group command.
这将使您了解使用 group 命令消除了多少行。
This will give DISTINCT values across all the columns:这将在所有列中给出 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
You can simply use this to get back one row with four comma separated lists with all values.您可以简单地使用它来取回一行,其中包含四个逗号分隔的列表,其中包含所有值。
SELECT
GROUP_CONCAT(DISTINCT `a`) AS `as`,
GROUP_CONCAT(DISTINCT `b`) AS `bs`,
GROUP_CONCAT(DISTINCT `c`) AS `cs`,
GROUP_CONCAT(DISTINCT `d`) AS `ds`
FROM
`my_table`;
If you need an other separator than the comma, add the SEPARATOR
option to GROUP_CONCAT
.如果您需要逗号以外的其他分隔符,请将
SEPARATOR
选项添加到GROUP_CONCAT
。
select distinct concat( colA, ' ', colB, ' ', colC ) from tableName;
Based on your answers I made the same but for extracting a json column vars
with arrays of 8 values all merged in 1 column with quotes trimed distinct values.根据您的回答,我做了同样的事情,但是为了提取 json 列
vars
,其中 arrays 的 8 个值全部合并在 1 列中,引号修剪了不同的值。
SELECT DISTINCT TRIM(BOTH '"' FROM T1.`vars`) FROM (
SELECT JSON_EXTRACT(`vars`, '$[1]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[2]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[3]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[4]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[5]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[6]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[7]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[8]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
) T1
WHERE `vars` IS NOT NULL
AND `vars` != 'false';
Hope it can helps...希望它可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.