Skip to content

Commit 264e3a2

Browse files
committed
update to DB docs (refs #4617)
1 parent cda8c90 commit 264e3a2

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

guide/database/query/builder.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ This query would generate the following SQL:
148148

149149
### Database Functions
150150

151-
Eventually you will probably run into a situation where you need to call `COUNT` or some other database function within your query. The query builder supports these functions in two ways. The first is by using quotes within aliases:
151+
Eventually you will probably run into a situation where you need to call `COUNT` or some other database function within your query. The query builder supports these functions using the `Database_Expression` class:
152152

153-
$query = DB::select(array('COUNT("username")', 'total_users'))->from('users');
153+
$query = DB::select(array(DB::expr('COUNT(`username`)'), 'total_users'))->from('users');
154154

155-
This looks almost exactly the same as a standard `AS` alias, but note how the column name is wrapped in double quotes. Any time a double-quoted value appears inside of a column name, **only** the part inside the double quotes will be escaped. This query would generate the following SQL:
155+
This looks almost exactly the same as a standard `AS` alias, but note how the column name is put in a call to `DB::expr()`. Any time `DB::expr()` is used, the column name will **not** be escaped. This query would generate the following SQL:
156156

157157
SELECT COUNT(`username`) AS `total_users` FROM `users`
158158

@@ -164,14 +164,14 @@ This looks almost exactly the same as a standard `AS` alias, but note how the co
164164
->where('posts.created', '>=', $yesterday);
165165

166166
$total = clone $query;
167-
$total->select(array('COUNT( DISTINCT "username")', 'unique_users'));
167+
$total->select(array(DB::expr('COUNT( DISTINCT `username`)'), 'unique_users'));
168168
$query->select('posts.username')->distinct();
169169

170170
### Aggregate Functions
171171

172172
Aggregate functions like `COUNT()`, `SUM()`, `AVG()`, etc. will most likely be used with the `group_by()` and possibly the `having()` methods in order to group and filter the results on a set of columns.
173173

174-
$query = DB::select('username', array('COUNT("id")', 'total_posts')
174+
$query = DB::select('username', array(DB::expr('COUNT(`id`)'), 'total_posts')
175175
->from('posts')->group_by('username')->having('total_posts', '>=', 10);
176176

177177
This will generate the following query:
@@ -182,7 +182,7 @@ This will generate the following query:
182182

183183
Query Builder objects can be passed as parameters to many of the methods to create subqueries. Let's take the previous example query and pass it to a new query.
184184

185-
$sub = DB::select('username', array('COUNT("id")', 'total_posts')
185+
$sub = DB::select('username', array(DB::expr('COUNT(`id`)'), 'total_posts')
186186
->from('posts')->group_by('username')->having('total_posts', '>=', 10);
187187

188188
$query = DB::select('profiles.*', 'posts.total_posts')->from('profiles')
@@ -196,7 +196,7 @@ This will generate the following query:
196196

197197
Insert queries can also use a select query for the input values
198198

199-
$sub = DB::select('username', array('COUNT("id")', 'total_posts')
199+
$sub = DB::select('username', array(DB::expr('COUNT(`id`)'), 'total_posts')
200200
->from('posts')->group_by('username')->having('total_posts', '>=', 10);
201201

202202
$query = DB::insert('post_totals', array('username', 'posts'))->select($sub);

0 commit comments

Comments
 (0)