$sql = 'SELECT COUNT(*) FROM ';
$cola = ' UNION '; // Deixar os espaços em branco "em volta".
$colaWhere = ' WHERE ';
$first = true;
$wheres = "";
foreach($arrayNomeBDs as $nomeBD){
$where = ' status = 2'. ' AND nomePessoa="O aqui vai variar" ';
if($first == true){
$wheres .= $sql . $nomeBD .$colaWhere . $where. $cola;
$first = false;
} else {
$wheres .= $sql . $nomeBD .$colaWhere . $where. $cola;
}
}
It is generating like this:
SELECT COUNT(*) FROM tab1 WHERE status = 2 AND nomePessoa="O aqui vai variar" UNION SELECT COUNT(*) FROM tab2 WHERE status = 2 AND nomePessoa="O aqui vai variar" UNION SELECT COUNT(*) FROM tab3 WHERE status = 2 AND nomePessoa="O aqui vai variar" UNION
How do I remove the last union? What implementation tip could you give me?
a good way is to check the last index of the array, if I do not put UNION
? the same thing if I only have a database name in the array, I should get the UNION