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

サブクエリをfrom句で利用する方法【CakePHP4】

Posted at

CakePHP4でサブクエリをFrom句で利用する

// サブクエリの作成
$subquery = $this->Model名->find()
              ->select([
                  'エイリアス名' => '値'
              ]);
        
// サブクエリを利用してデータを取得する    
$query = $this->Model名->find()
           ->select([
             'サブクエリ名.値'
           ])
           ->from([
               'サブクエリ名' => 'サブクエリを格納した変数'
           ])
          ->enableHydration(false)
          ->all()
          ->toArray();

// 使用例
$subquery = $this->Users->find()
           ->select([
             'user_name' => 'name'
           ]);

$query = $this->Users->find()
           ->select([
             'subquery.user_name'(エイリアスを付けても取得可)
           ])
           ->from([
             'subquery' => $subquery
           ])
           ->enableHydration(false)
           ->all()
           ->toArray();
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?