19
16

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 5 years have passed since last update.

Criteriaで複雑な条件のクエリを書く

Last updated at Posted at 2014-02-28

括弧を使ったセレクトをしたい

SELECT * FROM table_name
WHERE clumn_A = 1
AND (
(clumn_B = 'hoge' OR clumn_C = "piyo") OR
(clumn_C = 'foo' OR clumn_C = "bar")
);

(;^ω^)ああん!Propelでこういう条件でセレクトしたいけど条件がネストしててO/Rマッパじゃ難しいお〜!
(;^ω^)生のクエリ書くしかないか…!?

そんなことはない、Criterionを使えばね。

$c = new Criteria();

$c->add(TableNamePeer::CLUMN_A, 1);
$criterion1 = $c->getNewCriterion(TableNamePeer::CLUMN_B, 'hoge');
$criterion1->addOr($c->getNewCriterion(TableNamePeer::CLUMN_B, 'piyo'));

$criterion2 = $c->getNewCriterion(TableNamePeer::CLUMN_C, 'foo');
$criterion2->addOr($c->getNewCriterion(TableNamePeer::CLUMN_C, 'bar'));

$criterion1->addOr($criterion2);
// ここがポイント!
// 条件1 OR 条件2 の ORの部分。

$criteria->add($criterion1);

余談

クライテリオンってなんか戦隊ものっぽい。

19
16
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
19
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?