1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CodeIgniter4 のクエリビルダ `$builder->selectXXX()` は第2引数にAS句を指定できる

Posted at

マニュアルにこっそり記載があるがわかりにくいのでメモ。
クエリビルダのグルーピング系 $builder->selectXXX() メソッドには第2引数を設定することでAS句として使える。

// 例
$builder->selectMax('age', 'member_age');
$query = $builder->get();
// Produces: SELECT MAX(age) as member_age FROM mytable

対象となるメソッドは以下

  • $builder->selectMax()
  • $builder->selectMin()
  • $builder->selectAvg()
  • $builder->selectSum()
  • $builder->selectCount()

実は $builder->selectMax() にだけ例示(というか引用だが)のように第2引数を設定している例があるのだが、他のメソッドは省略されてしまっている模様。
実際は他のメソッドでも利用可能(予測はつくと思うが)

恐らく使用頻度が高いであろう $builder->selectCount() だけを見ていると一瞬あれ?となるので備忘録として残す。
(というか、自分が引っかかっただけの失敗談)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?