MySQL:选择唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34312757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: SELECT UNIQUE VALUE
提问by Bj?rn C
In my table I have several duplicates. Ineed to find unique values in mysql table column.
在我的表中,我有几个重复项。需要在 mysql 表列中找到唯一值。
SQL
SQL
SELECT column FROM table
WHERE column is unique
SELECT column FROM table
WHERE column = DISTINCT
I've been trying to Google, but almost all queries are more complex.
我一直在尝试谷歌,但几乎所有的查询都更复杂。
The result I's like is all non duplicate values.
我喜欢的结果是所有非重复值。
EDITI'd like to have UNIQUE values...
编辑我想拥有独特的价值...
Not all values one time... (Distinct)
并非所有值一次...(不同)
回答by Rahul Tripathi
回答by Marcus
Try this one:
试试这个:
SELECT COUNT(column_name) AS `counter`, column_name
FROM tablename
GROUP BY column_name
WHERE COUNT(column_name) = 1
Have a look at this fiddle: http://sqlfiddle.com/#!9/15147/2/0
看看这个小提琴:http://sqlfiddle.com/#! 9/15147/2/0
回答by Nerdi.org
Here is the query that you want!
这是您想要的查询!
SELECT column FROM table GROUP BY column HAVING COUNT(column) = 1
This query took 00.34 seconds on a data set of 1 Million rows.
该查询在 100 万行的数据集上花费了 00.34 秒。
Here is a query for you though, in the future if you DO want duplicates, but NOT non-duplicates...
不过,这是您的查询,将来如果您确实想要重复,但不是非重复...
SELECT column, COUNT(column) FROM table GROUP BY column HAVING COUNT(column) > 1
This query took 00.59 seconds on a data set of 1 Million rows. This query will give you the (column) value for every duplicate, and also the COUNT(column) result for how many duplicates. You can obviously choose not to select COUNT(column) if you don't care how many there are.
该查询在 100 万行的数据集上花费了 00.59 秒。此查询将为您提供每个重复项的(列)值,以及重复项数的 COUNT(列)结果。如果您不在乎有多少,您显然可以选择不选择 COUNT(column) 。
You can also check this out, if you need access to more than just the column with possible duplicates... Finding duplicate values in a SQL table
如果您需要访问的不仅仅是具有可能重复项的列,您也可以检查一下……在 SQL 表中查找重复值
回答by Bernes Lase
try this > select distinct (columnName) from table
试试这个 > 从表中选择不同的(列名)