Use COUNT, GROUP and HAVING : Count « Select Clause « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Select Clause » Count 
Use COUNT, GROUP and HAVING

/*

mysql> select * from Item;
+----+-----------+---------+---------+----------+------------+----------+---------------------+
| ID | Name      | InStock | OnOrder | Reserved | Department | Category | RowUpdate           |
+----+-----------+---------+---------+----------+------------+----------+---------------------+
|  1 | Bloodshot |      10 |       5 |        1 | Popular    | Rock     | 2005-10-09 09:19:45 |
|  2 | Most      |      10 |       5 |        2 | Classical  | Opera    | 2005-10-09 09:19:45 |
|  3 | Jazz      |      17 |       4 |        3 | Popular    | Jazz     | 2005-10-09 09:19:45 |
|  4 | Class     |       9 |       4 |        4 | Classical  | Dance    | 2005-10-09 09:19:45 |
|  5 | Violin    |      24 |       2 |        5 | Classical  | General  | 2005-10-09 09:19:45 |
|  6 | Cha Cha   |      16 |       6 |        6 | Classical  | Vocal    | 2005-10-09 09:19:45 |
|  7 | Blues     |       2 |      25 |        7 | Popular    | Blues    | 2005-10-09 09:19:45 |
|  8 | Pure      |      32 |       3 |       18 | Popular    | Jazz     | 2005-10-09 09:19:45 |
|  9 | Mud       |      12 |      15 |       19 | Popular    | Country  | 2005-10-09 09:19:45 |
| 10 | The       |       5 |      20 |       11 | Popular    | New Age  | 2005-10-09 09:19:45 |
| 11 | Embrace   |      24 |      11 |       12 | Popular    | New Age  | 2005-10-09 09:19:45 |
| 12 | Magic     |      42 |      17 |       13 | Classical  | General  | 2005-10-09 09:19:45 |
| 13 | Lake      |      25 |      44 |       24 | Classical  | Dance    | 2005-10-09 09:19:45 |
| 14 | LaLala    |      20 |      10 |        5 | Classical  | Opera    | 2005-10-09 09:19:45 |
| 15 | Soul      |      15 |      30 |       16 | Popular    | Blues    | 2005-10-09 09:19:45 |
| 16 | Stages    |      42 |       0 |        7 | Popular    | Blues    | 2005-10-09 09:19:45 |
| 17 | Six       |      16 |       8 |        6 | Classical  | General  | 2005-10-09 09:19:45 |
+----+-----------+---------+---------+----------+------------+----------+---------------------+
17 rows in set (0.00 sec)

mysql> SELECT Category, COUNT(*) AS Total
    -> FROM Item
    -> WHERE Department='Popular'
    -> GROUP BY Category
    -> HAVING Total < 3;
+----------+-------+
| Category | Total |
+----------+-------+
| Country  |     1 |
| Jazz     |     2 |
| New Age  |     2 |
| Rock     |     1 |
+----------+-------+
4 rows in set (0.00 sec)

*/

Drop table Item;

CREATE TABLE Item
(
   ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name VARCHAR(50NOT NULL,
   InStock SMALLINT UNSIGNED NOT NULL,
   OnOrder SMALLINT UNSIGNED NOT NULL,
   Reserved SMALLINT UNSIGNED NOT NULL,
   Department ENUM('Classical', 'Popular') NOT NULL,
   Category VARCHAR(20NOT NULL,
   RowUpdate TIMESTAMP NOT NULL
);


INSERT INTO Item (Name, InStock, OnOrder, Reserved, Department, Category)
          VALUES ('Bloodshot',      10,      5,       1'Popular',      'Rock'),
                 ('Most',           10,      5,       2'Classical',    'Opera'),
                 ('Jazz',           17,      4,       3'Popular',      'Jazz'),
                 ('Class',           9,      4,       4'Classical',    'Dance'),
                 ('Violin',         24,      2,       5'Classical',    'General'),
                 ('Cha Cha',        16,      6,       6'Classical',    'Vocal'),
                 ('Blues',           2,     25,       7'Popular',      'Blues'),
                 ('Pure',           32,      3,      18'Popular',      'Jazz'),
                 ('Mud',            12,     15,      19'Popular',      'Country'),
                 ('The',             5,     20,      11'Popular',      'New Age'),
                 ('Embrace',        24,     11,      12'Popular',      'New Age'),
                 ('Magic',          42,     17,      13'Classical',    'General'),
                 ('Lake',           25,     44,      24'Classical',    'Dance'),
                 ('LaLala',         20,     10,       5'Classical',    'Opera'),
                 ('Soul',           15,     30,      16'Popular',      'Blues'),
                 ('Stages',         42,      0,       7'Popular',      'Blues'),
                 ('Six',            16,      8,       6'Classical',    'General');

select from Item;

SELECT Category, COUNT(*AS Total
FROM Item
WHERE Department='Popular'
GROUP BY Category
HAVING Total < 3;



           
       
Related examples in the same category
1.Counting Rows: Counting the total number of animals
2.Count and group
3.Use COUNT in select command
4.COUNT() and GROUP BY
5.Another Count and Group BY
6.Count and group by two columns
7.COUNT command with condition
8.COUNT with condition and group
9.Get GROUP BY for COUNT
10.Use COUNT with condition
11.Simple COUNT
12.Performing Row and Column Counting
13.Use COUNT and GROUP
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.