Using SQL's DISTINCT Keyword to Retrieve Unique Data
Dealing with duplicate entries in SQL queries can be problematic when you need unique results. The DISTINCT
keyword provides a simple solution for filtering out these duplicates and returning only unique values.
To utilize DISTINCT
, incorporate it into your SELECT
statement before the column name(s) you want unique results from. For example, if you need unique entries from the "item" column in a table:
<code class="language-sql">SELECT DISTINCT item FROM table_name;</code>
This query would yield the following unique items:
ID | Item |
---|---|
1 | item1 |
2 | item2 |
3 | item3 |
You can also specify multiple columns to retrieve unique combinations based on all selected columns:
<code class="language-sql">SELECT DISTINCT item, data FROM table_name;</code>
This would return unique pairings of "item" and "data":
ID | Item | Data |
---|---|---|
1 | item1 | data1 |
2 | item2 | data3 |
3 | item3 | data4 |
The above is the detailed content of How Can I Retrieve Only Unique Records Using SQL's DISTINCT Keyword?. For more information, please follow other related articles on the PHP Chinese website!