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.
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.
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.
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.
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.
SELECT DISTINCT adspot FROM your_table;
( this may not perform well at all in large tables )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With