Home  >  Article  >  Database  >  sql having用法与实例教程

sql having用法与实例教程

WBOY
WBOYOriginal
2016-06-07 17:47:042574browse

sql having用法与实例教程

HAVING子句是可选的,结合使用group by子句。它类似于Where子句,但经条款规定的限制,确定哪些记录后,将显示他们已进行分组。它通常都处于SQL语句的结束,一个HAVING子句的SQL语句可能会也可能不包括group by子句。

对于拥有语法如下:

SELECT column1, ... column_n, aggregate_function (expression)
FROM table_name
[WHERE condition]
[GROUP BY column1, ... column_n]
HAVING condition
实例:

Select Item, Sum(Price) as TotalSum
From Antiques
Group by Item
Having Sum(Price)>57

查询返回不同的项目和所谓'字段列表TotalSum'算数的古董结果总和供应每个项目。 HAVING子句将只返回那些超过57美元的金额与项目的结果。

范例#2

Select SellerID, Count(*) as Number_of_Sellers
From Antiques
Where BuyerID=21
Group by SellerID
Having Count(*)>1

个例子显示了卖方的ID列表,以及它们的数量,但只有在有一个以上的BuyerID等于15卖方。

例如#3

SELECT Item, COUNT(Item) AS Total, MAX(Price) AS MaxPrice
FROM Antiques
GROUP BY Item
HAVING COUNT(Item) > 1 AND MAX(Price)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn