Query sql for cakephp

Asked

Viewed 255 times

1

How do I query below in cakephp?

    SELECT * 
    FROM tramitacaos
    INNER JOIN protocolos ON tramitacaos.protocolo_id = protocolos.id
    WHERE (
    protocolo_id, data_origem
    )
    IN (
    SELECT protocolo_id, MAX( data_origem ) 
    FROM tramitacaos
    WHERE unidade_origem_id =462
    GROUP BY protocolo_id
    )

1 answer

0


You can use this query using the ConnectionManager

use Cake\Datasource\ConnectionManager;

$connection = ConnectionManager::get('default');
$results = $connection->execute(
    'SELECT 
        * 
    FROM tramitacaos
    INNER JOIN protocolos 
        ON tramitacaos.protocolo_id = protocolos.id
    WHERE 
        (protocolo_id, data_origem) IN 
        (
            SELECT 
                protocolo_id, 
                MAX( data_origem ) 
            FROM 
                tramitacaos
            WHERE 
                unidade_origem_id = :unidadeOrigemId
            GROUP BY protocolo_id
        )',
        ['unidadeOrigemId' => 462]
    )->fetchAll('assoc');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.