PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: distinct
distinct - phponwebsites.com
Showing posts with label distinct. Show all posts

3 Feb 2014

Select distinct values in mysql

                       If your column in mysql table contains duplicate values, then you need to display distinct values by mysql command 'DISTINCT' and 'GROUP BY'.


Select unique values in Mysql by distinct:


                       The mysql command 'DISTINCT' is used to select distinct values. The mysql query for select distinct values as follows as:

                 SELECT DISTINCT column_name FROM table_name

Consider the following table. table1 is name of this table.

select distinct values in mysql

                              Now we are going to get unique values in column 'id' by 'DISTINCT' mysql command like this:

                      SELECT DISTINCT id FROM table1

Now you'll get output like this:

select unique values using distinct in mysql

                    You can see the table 'table1' which have unique values.

Select unique values in Mysql by 'Group by':

                   
                              You can also display unique values using 'GROUP BY' mysql command. If you want to select distinct values from one column and also other values from table, then you can use mysql command 'GROUP BY'.  The mysql query is:

                       SELECT * FROM table_name GROUP BY column_name 

 Now we are going to get unique values in column 'id' by 'GROUP BY' mysql command like this:

                       SELECT * FROM table1 GROUP BY id 

Now you'll get output like this:

display unique values using group by in mysql


                           You can see the column 'id' values in mysql table 'table1' are unique.

Related Post:

1 Jan 2014

Distinct in mysql

                       The 'DISTINCT' mysql cmmand is used to eliminate duplicate values in a column. The mysql query as follows as:
                   
                      SELECT DISTINCT field_name FROM table_name


Distinct in Mysql:


                                     Consider the following table. table1 is name of this table.

distinct in mysql

                              Now we are going to get unique values in column 'id' by 'DISTINCT' mysql command like this:

                      SELECT DISTINCT id FROM table1

Now you'll get output like this:

unique values using distinct in mysql

                    You can see the table 'table1' which have unique values.


Distinct multiple column in Mysql:


                    You can use multiple column for 'DISTINCT' in a query. But you can't get answer as you want.
The mysql query as follows as:

                      SELECT DISTINCT id, count FROM table1

You'll get output like this:

distinct multiple columns in mysql