title: mysql - find table name from SELECT statement over multiple tables
body: <p>A query is made to select rows from multiple tables... Then necessary info (i.e <code>headline</code> is retrieved from there )</p>
  
    
  
  <pre><code>$sql="(SELECT `headline`,`date` FROM `arts`)
  
  UNION
  
  (SELECT `headline`,`date` FROM `lead_news`)
  
  UNION
  
  (SELECT `headline`,`date` FROM `opinion_analysis`)
  
  UNION
  
  (SELECT `headline`,`date` FROM `politics`)
  
  UNION
  
  (SELECT `headline`,`date` FROM `top_news`)
  
  UNION
  
  (SELECT `headline`,`date` FROM `lifestyle`)
  
  ORDER BY `date` DESC LIMIT $limit  ";
  </code></pre>
  
  
  
  <pre><code>$result=mysql_query($sql);
  while($row=mysql_fetch_assoc($result)){
      echo trim(htmlspecialchars_decode($row['headline']));
  }
  </code></pre>
  
  <p>I also need to  find out the <code>table</code> name for each <code>$row</code>.  I found a solution <a href="https://stackoverflow.com/questions/12544758/how-to-find-the-table-name-which-the-image-randomly-selected-from">here</a> that suggests the query to be :</p>
  
  <p><code>"SELECT *, table_name as source FROM table_name"</code> so that <code>$row['source']</code> can return the table name.</p>
  
  <p>But I am trying to know whether there is  any associative array key to retrieve the name from <code>$row</code> or any built-in function in mysql ?</p>
  
  <p><strong>EDIT</strong> : I wrote the <code>SELECT</code> commands in the following way :</p>
  
  <pre><code>SELECT `headline`,`date`, `arts` as `source` FROM `arts`
  </code></pre>
  
  <p>and got the error :Unknown column 'arts' in 'field list'.
  What way to adopt here ?</p>