转:drupal sql查询count

Count queries

Count queries

Any query may have a corresponding "count query". The count query returns the number of rows in the original query. To obtain a count query, use the countQuery() method.

$count_query = $query->countQuery();

$count_query is now a new Dynamic Select query with no ordering restrictions that when executed will return a result set with only one value, the number of records that would be matched by the original query. Because PHP supports chaining methods on returned objects, the following idiom is a common approach:

$num_rows = $query->countQuery()->execute()->fetchField();
posted @ 2017-07-11 16:04  jiafeng  阅读(285)  评论(0编辑  收藏  举报