Include a WHERE clause-but only to select rows, not to test summary values. : HAVING « 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 » HAVING 
Include a WHERE clause-but only to select rows, not to test summary values.
       
mysql>
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  rec_id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  name            VARCHAR(20NOT NULL,
    ->  trav_date       DATE NOT NULL,
    ->  miles           INT NOT NULL,
    ->  PRIMARY KEY (rec_id)
    -> );
Query OK, rows affected (0.00 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(*AS count, name
    -> FROM mytable
    -> GROUP BY name
    -> HAVING count > 3;
+-------+-------+
| count | name  |
+-------+-------+
|     | Henry |
+-------+-------+
row in set (0.00 sec)

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

   
    
    
    
    
    
    
  
Related examples in the same category
1.SELECT statement includes a HAVING clause that contains one condition:
2.Shows those hours of the day during which no messages were sent by using a HAVING clause that selects only sum
3.HAVING COUNT(*) > 1
4.HAVING SUM(AMOUNT) > 150
5.Determining Whether Values are Unique
6.To find message sender/recipient pairs between whom only one message was sent
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.