0
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 1 year has passed since last update.

CakePHPのサブクエリでunionAll()を実行するとSyntax Errorになる

Posted at

事象

サブクエリにてunionAll()を使用するとSyntax Erorになる。

元になったORM

$query->where(['id in' => TableRegistry::getTableLocator()
				->get('テーブル2')
				->find()
                ->select('id')
                ->unionAll(
                    TableRegistry::getTableLocator()
                        ->get('テーブル3')
        				->find()
                        ->select('id')
                )

生成されたクエリ

SELECT * FROM テーブル 
WHERE id in ((SELECT id FROM テーブル2 )
UNION ALL (SELECT id FROM テーブル3);
(SELECT id FROM テーブル2 )

この()がなくなるとSyntax Errorではなくなる

対策

OR句を使用すること。
パフォーマンスもそんなに変わらんし、その方が素直な実装になるでしょう。
それにしてもORMとの格闘は大変だ。

0
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
0
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?