MorkaLork Development

Interesting stuff I've picked up over the years...

Mysql 11: HAVING

2009-04-16 21:18:51 | 161 views | sql mysql table having select from group by limit

HAVING:



If you want to limit the output to a certain sum. HAVING let's you set that limit.


This will be our table 'stores'

|--------------------+--------------------+--------------------|
|store_name |store_sales |store_updates |
|____________________+____________________+____________________|
|Hanks Hardware 2500 2009-01-01 00:00:00 |
|Santos SuperStore 750 2008-12-01 00:00:00 |
|Bobs Boutique 1250 2009-01-01 00:00:00 |
|Pattyïs Pantstore 1050 2008-12-15 00:00:00 |
|Hanks Hardware 2250 2008-01-01 00:00:00 |
+--------------------------------------------------------------+


Syntax:

SELECT column, SUM(column)
FROM table_name
GROUP BY column
HAVING(condition)


Example:


SELECT store_name, SUM(store_sales)
FROM stores
GROUP BY store_name
HAVING SUM(store_sales > 1200)


|--------------------+--------------------|
|store_name |SUM(store_sales) |
|____________________+____________________|
|Bobs Boutique 1250 |
|Hanks Hardware 4750 |
+------------------------------------------+



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.