diff --git a/var/Typecho/Db/Adapter/Mysql.php b/var/Typecho/Db/Adapter/Mysql.php index e2cff0fb..13bf577d 100644 --- a/var/Typecho/Db/Adapter/Mysql.php +++ b/var/Typecho/Db/Adapter/Mysql.php @@ -141,7 +141,7 @@ class Typecho_Db_Adapter_Mysql implements Typecho_Db_Adapter $sql['offset'] = (0 == strlen($sql['offset'])) ? NULL : ' OFFSET ' . $sql['offset']; return 'SELECT ' . $sql['fields'] . ' FROM ' . $sql['table'] . - $sql['where'] . $sql['group'] . $sql['order'] . $sql['limit'] . $sql['offset']; + $sql['where'] . $sql['group'] . $sql['having'] . $sql['order'] . $sql['limit'] . $sql['offset']; } /** diff --git a/var/Typecho/Db/Adapter/Pdo/Mysql.php b/var/Typecho/Db/Adapter/Pdo/Mysql.php index 65376d8e..71047e2b 100644 --- a/var/Typecho/Db/Adapter/Pdo/Mysql.php +++ b/var/Typecho/Db/Adapter/Pdo/Mysql.php @@ -84,6 +84,6 @@ class Typecho_Db_Adapter_Pdo_Mysql extends Typecho_Db_Adapter_Pdo $sql['offset'] = (0 == strlen($sql['offset'])) ? NULL : ' OFFSET ' . $sql['offset']; return 'SELECT ' . $sql['fields'] . ' FROM ' . $sql['table'] . - $sql['where'] . $sql['group'] . $sql['order'] . $sql['limit'] . $sql['offset']; + $sql['where'] . $sql['group'] . $sql['having'] . $sql['order'] . $sql['limit'] . $sql['offset']; } } diff --git a/var/Typecho/Db/Adapter/Pdo/Pgsql.php b/var/Typecho/Db/Adapter/Pdo/Pgsql.php index d34a9cac..6618b140 100644 --- a/var/Typecho/Db/Adapter/Pdo/Pgsql.php +++ b/var/Typecho/Db/Adapter/Pdo/Pgsql.php @@ -71,7 +71,7 @@ class Typecho_Db_Adapter_Pdo_Pgsql extends Typecho_Db_Adapter_Pdo $sql['offset'] = (0 == strlen($sql['offset'])) ? NULL : ' OFFSET ' . $sql['offset']; return 'SELECT ' . $sql['fields'] . ' FROM ' . $sql['table'] . - $sql['where'] . $sql['group'] . $sql['order'] . $sql['limit'] . $sql['offset']; + $sql['where'] . $sql['group'] . $sql['having'] . $sql['order'] . $sql['limit'] . $sql['offset']; } /** diff --git a/var/Typecho/Db/Adapter/Pdo/SQLite.php b/var/Typecho/Db/Adapter/Pdo/SQLite.php index bcfc3554..445852a2 100644 --- a/var/Typecho/Db/Adapter/Pdo/SQLite.php +++ b/var/Typecho/Db/Adapter/Pdo/SQLite.php @@ -70,6 +70,6 @@ class Typecho_Db_Adapter_Pdo_SQLite extends Typecho_Db_Adapter_Pdo $sql['offset'] = (0 == strlen($sql['offset'])) ? NULL : ' OFFSET ' . $sql['offset']; return 'SELECT ' . $sql['fields'] . ' FROM ' . $sql['table'] . - $sql['where'] . $sql['group'] . $sql['order'] . $sql['limit'] . $sql['offset']; + $sql['where'] . $sql['group'] . $sql['having'] . $sql['order'] . $sql['limit'] . $sql['offset']; } } diff --git a/var/Typecho/Db/Adapter/Pgsql.php b/var/Typecho/Db/Adapter/Pgsql.php index b759986c..e5a394fa 100644 --- a/var/Typecho/Db/Adapter/Pgsql.php +++ b/var/Typecho/Db/Adapter/Pgsql.php @@ -149,7 +149,7 @@ class Typecho_Db_Adapter_Pgsql implements Typecho_Db_Adapter $sql['offset'] = (0 == strlen($sql['offset'])) ? NULL : ' OFFSET ' . $sql['offset']; return 'SELECT ' . $sql['fields'] . ' FROM ' . $sql['table'] . - $sql['where'] . $sql['group'] . $sql['order'] . $sql['limit'] . $sql['offset']; + $sql['where'] . $sql['group'] . $sql['having'] . $sql['order'] . $sql['limit'] . $sql['offset']; } /** diff --git a/var/Typecho/Db/Adapter/SQLite.php b/var/Typecho/Db/Adapter/SQLite.php index 9856161e..318fbc6c 100644 --- a/var/Typecho/Db/Adapter/SQLite.php +++ b/var/Typecho/Db/Adapter/SQLite.php @@ -169,7 +169,7 @@ class Typecho_Db_Adapter_SQLite implements Typecho_Db_Adapter $sql['offset'] = (0 == strlen($sql['offset'])) ? NULL : ' OFFSET ' . $sql['offset']; return 'SELECT ' . $sql['fields'] . ' FROM ' . $sql['table'] . - $sql['where'] . $sql['group'] . $sql['order'] . $sql['limit'] . $sql['offset']; + $sql['where'] . $sql['group'] . $sql['having'] . $sql['order'] . $sql['limit'] . $sql['offset']; } /** diff --git a/var/Typecho/Db/Query.php b/var/Typecho/Db/Query.php index 7fca2fe6..b09b0a52 100644 --- a/var/Typecho/Db/Query.php +++ b/var/Typecho/Db/Query.php @@ -70,6 +70,7 @@ class Typecho_Db_Query 'offset' => NULL, 'order' => NULL, 'group' => NULL, + 'having' => NULL, 'rows' => array(), ); } @@ -336,6 +337,28 @@ class Typecho_Db_Query return $this; } + /** + * HAVING (HAVING) + * + * @return Typecho_Db_Query + */ + public function having() + { + $condition = func_get_arg(0); + $condition = str_replace('?', "%s", $this->filterColumn($condition)); + $operator = empty($this->_sqlPreBuild['having']) ? ' HAVING ' : ' AND'; + + if (func_num_args() <= 1) { + $this->_sqlPreBuild['having'] .= $operator . ' (' . $condition . ')'; + } else { + $args = func_get_args(); + array_shift($args); + $this->_sqlPreBuild['having'] .= $operator . ' (' . vsprintf($condition, array_map(array($this->_adapter, 'quoteValue'), $args)) . ')'; + } + + return $this; + } + /** * 选择查询字段 *