Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> $stmts_1[] = sprintf(" SELECT * FROM ( SELECT * FROM $table_name tn WHERE date_time = $today_date UNION ALL SELECT * FROM $table_name_2 tn2 WHERE date_time = $today_date )tmp;" } </code></pre> <p>That should do what you want - I havn't tested it as depends on the variables being passed and you haven't provided SQL or code. This is a sub-select query. The UNION ALL part concatenates the results of both queries into one result. However both tables MUST have the same amount of columns in the same order otherwise this will throw a incompatible type or wrong amount of column error.</p> <p>Its much better instead to define the columns and the order e.g.</p> <pre><code> $stmts_1[] = sprintf(" SELECT * FROM ( SELECT col1, col2, col3, col4...... coln FROM $table_name tn WHERE date_time = $today_date UNION ALL SELECT col1, col2, col3, col4...... coln FROM $table_name_2 tn2 WHERE date_time = $today_date )tmp;" </code></pre> <p>}</p> <p>EDIT:</p> <p>Applying the same logic I said above you can handle extra attributes that are in one table but not another like this:</p> <pre><code>SELECT * FROM( SELECT id as id, ei_code as ei_code, date_time as dt, '' as country_name, index_year as iy, index_period as ip1, index_period_2 as ip2, index_unit as iu, index_comment as ic, '' as et, '' as er, '' as ec FROM `v_c_ei_9001` vce1 UNION ALL SELECT id as id, country_code as country_code, date_time as date_time, country_name as country_name, '' as iy, '' as ip1, '' as ip2, '' as iu, '' as ic, event_title as et, event_released as er, event_comment as ec FROM `v_c_e` as vce2 )tmp </code></pre> <p>I've made a couple of assumptions based upon the create table you posted seeing as the input masks were very similar. If these are wrong in your context just create blank fields as i've done for ip1, ip2 etc. I've tested the above with your CREATE tables in MySQL using some sample data and its fine.</p> <p>EDIT 2:</p> <pre><code>$start2 = "SELECT id as id, country_code as country_code, date_time as date_time, country_name as country_name, '' as iy, '' as ip1, '' as ip2, '' as iu, '' as ic, event_title as et, event_released as er, event_comment as ec FROM "; $count = 0; $start = "SELECT id as id, ei_code as ei_code, date_time as date_time, '' as country_name, index_year as iy, index_period as ip1, index_period_2 as ip2, index_unit as iu, index_comment as ic, '' as et, '' as er, '' as ec FROM "; //Loop through all table names using the variable $start concatenated with $table_name to create the SQL statement you require while (($row_1 = $STH_1 -&gt; fetch_assoc()) !== null){ $table_name = $row_1['table_name']; $stmts_1[] = sprintf(" $start $table_name WHERE date_time = $today_date "); $count = $count + 1; } //This statements adds an extra String to the above stmt_1 - the $count variable controls where the extra string is placed, should be at the end of the array. Shouldn't matter though. //NOTE this is your second query the part 'b' of your above question: variable $table_name_2 $stmt_1[count] sprintf(" $start2 $table_name_2 WHERE date_time = $today_date "); //Implode all arrays into one long array adding 'Carriage Return (\n) UNION Carriage Return (\n)' between each statement. $stmt_1 = implode("\nUNION\n", $stmts_1); //Add another line at the end for the order by $stmt_1 .= "\nORDER BY date_time ASC"; $STH_5_2 = $DBH_R-&gt;query($stmt_1); while (($row_5_2 = $STH_5_2 -&gt; fetch_assoc()) !== null) { } </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. VO
      singulars
      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