Note that there are some explanatory texts on larger screens.

plurals
  1. POdata from few MySQL tables sorted by ASC
    primarykey
    data
    text
    <p>In the dbase I 've few tables named as <code>aaa_9xxx, aaa_9yyy, aaa_9zzz</code>. I want to find all data with a specified <code>DATE</code> and show it with the <code>TIME ASC</code>.</p> <p>First, I must find a tables in the dbase:</p> <pre><code>$STH_1a = $DBH-&gt;query("SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'aaa\_9%' "); foreach($STH_1a as $row) { $table_name_s1[] = $row['table_name']; } </code></pre> <p>Second, I must find a data wit a concrete date and show it with <code>TIME ASC</code>:</p> <pre><code>foreach($table_name_s1 as $table_name_1) { $STH_1a2 = $DBH-&gt;query("SELECT * FROM `$table_name_1` WHERE date = '2011-11-11' ORDER BY time ASC "); while ($row = $STH_1a2-&gt;fetch(PDO::FETCH_ASSOC)) { echo " ".$table_name_1."-".$row['time']."-".$row['ei_name']." &lt;br&gt;"; } } </code></pre> <p>.. but it shows the data sorted by tables name, then by <code>TIME ASC</code>. I must to have all this data (from all tables) sorted by <code>TIME ASC</code>.</p> <hr> <p>Thank You dev-null-dweller, Andrew Stubbs and Jaison Erick for your help. I test the Erick solution :</p> <pre><code>foreach($STH_1a as $row) { $stmts[] = sprintf('SELECT * FROM %s WHERE date="%s"', $row['table_name'], '2011-11-11'); } $stmt = implode("\nUNION\n", $stmts); $stmt .= "\nORDER BY time ASC"; $STH_1a2 = $DBH-&gt;query($stmt); while ($row_1a2 = $STH_1a2-&gt;fetch(PDO::FETCH_ASSOC)) { echo " ".$row['table_name']."-".$row_1a2['time']."-".$row_1a2['ei_name']." &lt;br&gt;"; } </code></pre> <p>it's working but I've problem with '<code>table_name</code>' - it's always the <code>LAST</code> table name.</p> <p>//----------------------------------------------------------------------</p> <p>...and the ending solution with all fixes, thanks all for your help, :))</p> <pre><code>foreach($STH_1a as $row) { $stmts[] = sprintf("SELECT *, '%s' AS table_name FROM %s WHERE date='%s'", $row['table_name'], $row['table_name'], '2011-11- 11'); } $stmt = implode("\nUNION\n", $stmts); $stmt .= "\nORDER BY time ASC"; $STH_1a2 = $DBH-&gt;query($stmt); while ($row_1a2 = $STH_1a2-&gt;fetch(PDO::FETCH_ASSOC)) { echo " ".$row_1a2['table_name']."-".$row_1a2['time']."-".$row_1a2['ei_name']." &lt;br&gt;"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload