Model::having
HAVINGの条件を追加します
説明
public self Model::having( string $query, [ mixed $data = [], string $andOr = 'AND' ] )
HAVINGの条件を追加します。groupと一緒に使います。$dataはwhereと同じく、1つの値、またはプレースホルダーの順に並べた値の配列です。having()を使うと、countは条件に合うグループの数を数えます。
引数
| 名前 | データ型 | デフォルト | 備考 |
|---|---|---|---|
| $query | string | ?プレースホルダーを使った条件(ユーザーの入力を入れないこと) | |
| $data | mixed | [] | 1つの値、またはプレースホルダーの順に並べた値の配列 |
| $andOr | string | 'AND' | 前の条件とのつなぎ方(ANDまたはOR) |
例
Controller
$model = new Model(false, array(
'table' => 'cms_news',
'id' => 'news_id',
'visible' => 'visible',
));
// Categories with more than 5 news
// Select category, count(*) AS n From "cms_news" Where ... Group By category Having n > 5
$records = $model
->select('category, count(*) AS n')
->group('category')
->having('n > ?', 5)
->all();
複雑なHAVINGの条件
Controller
$model = new Model(false, array(
'table' => 'cms_news',
'id' => 'news_id',
'visible' => 'visible',
));
$model->select('category, count(*) AS n, max(post_date) AS latest')->group('category');
$model->having('n > ?', 5);
$model->havingOpen();
$model->having('category = ?', 1);
$model->having('latest >= ?', '2026-01-01', 'OR');
$model->havingClose();
// ... Group By category
// Having n > 5 AND (category = 1 OR latest >= '2026-01-01')
$records = $model->all();