how many different drivers there are, use COUNT(DISTINCT) : Distinct « Select Clause « SQL / MySQL






how many different drivers there are, use COUNT(DISTINCT)

       
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  rec_id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  name            VARCHAR(20) NOT NULL,
    ->  trav_date       DATE NOT NULL,
    ->  miles           INT NOT NULL,
    ->  PRIMARY KEY (rec_id)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO mytable (name,trav_date,miles)
    ->  VALUES
    ->          ('Ben','2010-11-30',152),
    ->          ('Suzi','2010-11-29',391),
    ->          ('Henry','2010-11-29',300),
    ->          ('Henry','2010-11-27',96),
    ->          ('Ben','2010-11-29',131),
    ->          ('Henry','2010-11-26',115),
    ->          ('Suzi','2010-12-02',502),
    ->          ('Henry','2010-12-01',197),
    ->          ('Ben','2010-12-02',79),
    ->          ('Henry','2010-11-30',203)
    -> ;
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT COUNT(DISTINCT name) FROM mytable;
+----------------------+
| COUNT(DISTINCT name) |
+----------------------+
|                    3 |
+----------------------+
1 row in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Use DISTINCT to get non-dupliate records
2.Use DISTICNT to get unique value
3.Eliminating Duplicate Data Using DISTINCT 1
4.Eliminating Duplicate Data Using DISTINCT 2
5.Select distinct records using JOIN
6.The SQL key word DISTINCT has the effect that equivalent data records are output only once.
7.DISTINCT works with multiple-column output too.
8.DISTINCT with two columns
9.DISTINCT works with expressions, not just column values.
10.Inserting name values into the multisequence table generates separate sequences for each distinct name:
11.Count distinct
12.Distinct sub string
13.Average distinct value
14.Return a list of surnames, with each surname appearing only once?
15.The fraction of the records that contain unique or non-unique names: