mysql -- where 、having、using异同

A: using () of the difference between the use of having

1. USING()

Available in the join statement using the same field are connected, and play the same role ON, inner join, and can be used in both left join

Example:

LEFT JOIN normal writing:

SELECT t1.id,t2.name FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE ....

In fact, you can also write:

SELECT t1.id,t2.name FROM t1 LEFT JOIN t2 USING(id) WHERE ....

2. HAVING

mysql where the having clause and can achieve functional screening record, may be considered complementary HAVING where, because it can be determined packet data again, typically followed group by, and can use the aggregate functions (SUM, min, max, avg, count)

Example:

SELECT `uid`,SUM(`points`) num FROM table GROUP BY `uid` HAVING num>1000

 

Second, and where usage difference having (excerpt from another article)

having usage

having clause allows us to filter into a variety of data sets, where the words before the polymerization to filter the records, that role before the group by and having clause. Group of records having clause and screening after the polymerization.

SQL instance:

1, shows the total number of population in each region and the total area

SELECT region, SUM(population), SUM(area) FROM bbc GROUP BY region

The first region to return the records into groups, this is the literal meaning of the GROUP BY. After the divided groups, and each group polymerization function
of different fields (a record or records) for operation.

2, shows the total population and the total area of ​​each region. Display only those areas covering more than 1,000,000.

SELECT region, SUM(population), SUM(area)
FROM bbc
GROUP BY region
HAVING SUM(area)>1000000

Here, where we can not use to screen more than 1 million of the region, because such a record does not exist in the table.
Instead, having clause allows us to filter the data in each group after group

mysql determine the length of a field:

select home_page from aaa表 where char_length(trim(home_page))<10 and char_length(trim(home_page))>1;

Mysql difference in where and having clauses

mysql in where and having clauses can realize the function of filtering records, but their usage there are some differences to see an example:
with the group by and having clauses jointly found not to duplicate records, sql as follows:
the SELECT uid, email, count (*) as ct from `edm_user081217` gROUP bY email
then look at this, it is easy to understand the
select uid, email, count (* ) as ct from` edm_user081217` gROUP bY email HAVING ct> 1
first with the group by for email group, with having to filter, so find out that duplicate records is greater than 1, a.

the following is the difference between having and where a:
the Select City the FROM Weather the wHERE temp_lo = (the SELECT max (temp_lo) the FROM Weather);
the role of different objects. The WHERE clause applied to tables and views, HAVING clause, the role of the group.
WHERE Select input lines before and aggregation packet (thus, it controls which rows go into the aggregate calculation), while HAVING selected packets after the packets and aggregation line. Therefore, WHERE clause must not contain aggregate functions; because trying to use aggregate functions to determine which rows will gather input operation is meaningless. Instead, HAVING clause always contains aggregate functions. (Strictly speaking, you can not use aggregate write HAVING clause, but this is just futile. The same conditions can be more effectively used in the WHERE stage.)
In the previous example, we can apply the city name restriction in WHERE, because it does not require aggregation. This is more efficient than the increase in the limit HAVING, because we avoid the grouping and aggregation calculation for those rows that fail the WHERE check
sum up:
the HAVING generally follows the group by, the implementation of part of the working group of records selected.
where it is to perform all data to work.
Further polymerization can HAVING functions, such as having sum (qty)> 1000

Guess you like

Origin www.cnblogs.com/daijiabao/p/11295455.html