how to return two distinct arrays using union all

1
        $stmt = $this->db->query("
SELECT COUNT(*) AS total, status as tipo FROM tabela1 WHERE status=1 GROUP BY SEXO UNION ALL
SELECT COUNT(*) AS total, status as tipo FROM tabela2 WHERE status=1 GROUP BY FAIXA_ETARIA
");
    $total = $stmt->fetchAll();

how to return an array like this (two arrays with all values referring to the select):

$total['sexo'] = "todos os valores referentes a este select dentro desta array"

$total['FAIXA_ETARIA'] = "todos os valores referentes a este select dentro desta array"

Is there a way to do this without doing 2 separate queries?

    
asked by anonymous 08.05.2016 / 23:05

1 answer

1

Try this:

$stmt = $this->db->query("
    select count(*) as total
       from tabela1 where status=1
    union all
    select count(*) as total
       from tabela2 where status=1
");

$total = $stmt->fetchAll(PDO::FETCH_ASSOC);

$sexo = $total[0];

echo 'Sexo: '.$sexo['total'].'<br>';

$faixa_etaria = $total[1];

echo 'Faixa etária: '.$faixa_etaria['total'];

NOTE: Always try to keep your codes at most 80 characters per line.

    
08.05.2016 / 23:33