$STH_1 = $DBH_R->query("SELECT table_name
FROM information_schema.tables
WHERE table_name
LIKE 'v_c_ei\_9%'
");
$stmts_1 = array();
while (($row_1 = $STH_1 -> fetch_assoc()) !== null){
$table_name = $row_1['table_name'];
$stmts_1[] = sprintf("SELECT *
FROM $table_name
WHERE (ei_code = '1117') AND (DAY(date_time) != '00')
");
}
$stmt_1 = implode("\nUNION\n", $stmts_1);
$stmt_1 .= "\nORDER BY date_time ASC";
$STH_5_2 = $DBH_R->query($stmt_1);
while (($row_5_2 = $STH_5_2 -> fetch_assoc()) !== null){
OK, the script above is working ok. But, I want (must) change its functionality. I must do selection before UNION (something like ORDER BY … LIMIT) but I know – this is not working. The solution is to change sprintif into UNION & UNION & UNION … But the problem is – we don’t know how much tables we have.
The table names from the first query looks like
v_c_ei_9001
v_c_ei_9002
..........
v_c_ei_9030
..........
ect.
Than the q: how change the sprintif (and maybe array $stmts_1 too) from abowe into the UNION in situation when we don’t know how much tables we have?
thnks
When you add
ORDER BYandLIMITclauses to each constituent of theUNION, as follows:And then you do the following:
The resulting string in
$stmt_1will look something like this:As you can see, the final constituent of your
UNIONhas twoORDER BYclauses, which is a syntax error. Either remove the line commentedremember this bitabove, or else (if you need to reorder the unified queries) you must make theUNIONa subquery to an outer query that performs the reordering: