how to return two distinct arrays using Union all

Asked

Viewed 29 times

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 for 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"

there is a way to do this without making 2 separate querys?

1 answer

1


Try it this way:

$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'];

OBSERVING: always try to keep your codes up to 80 characters per line.

Browser other questions tagged

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