原文:http://blog.csdn.net/showshore/article/details/7263115
---------------------------------------------------------
在做項目的過程中,使用sql語句時,很多時候會用到where或having。
看到國外一個論壇上有人提到兩者性能比較的這個問題時,有人是這樣回答的:
The theory (by theory I mean?SQL Standard) says that WHERE restricts the result set before returning rows and HAVING restricts the result set after bringing all the rows. So WHERE is faster. On SQL Standard compliant DBMSs in this regard, only use HAVING where you cannot put the condition on a WHERE (like computed columns in some RDBMSs.)
也就是說(從理論上講),where是在查詢前做條件限制,having是在查詢后的結果集上做條件限制。查詢前做限制的話,返回的件數會少;而在查詢后的結果集上做限制的話,之前的查詢結果可能會很多,這樣就會影響性能。
所以,一般是用where,having是在where不方便或者不能使用的情況下才使用。