Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT all value from table only once if they're duplicated

Tags:

mysql

Lets say I have a table with the following rows/values:

+--------+----------+
|   ID   |  adspot  |
+--------+----------+
|      1 |        A |
|      2 |        B |
|      3 |        A |
|      4 |        B |
|      5 |        C |
|      6 |        A |
+--------+----------+

I need a way to select the values in adspot but only once if they're duplicated. So from this example I'd want to select A once and B once. The SQL result should look like this then:

+----------+
|  adspot  |
+----------+
|        A |
|        B |
|        C |
+----------+

I'm using mySQL and PHP, in case anyone asks.

Thanks.

like image 917
Vitaliy Isikov Avatar asked Mar 09 '11 03:03

Vitaliy Isikov


People also ask

How do you show a column value if only one time is repeated?

You can use distinct keyword to select all values from a table only once if they are repeated. select distinct yourColumnName from yourTableName; To understand the above syntax, let us create a table.

How do I select all records from a table without duplicates in SQL?

If you want the query to return only unique rows, use the keyword DISTINCT after SELECT . DISTINCT can be used to fetch unique rows from one or more columns. You need to list the columns after the DISTINCT keyword.

How do I select a single record for duplicates in SQL?

Using the GROUP BY clause to group all rows by the target column(s) – i.e. the column(s) you want to check for duplicate values on. Using the COUNT function in the HAVING clause to check if any of the groups have more than 1 entry; those would be the duplicate values.

How do you find duplicate records in a table?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.


1 Answers

SELECT DISTINCT adspot FROM your_table; ( this may not perform well at all in large tables )

like image 121
Dre Avatar answered Oct 05 '22 05:10

Dre