title: #1327 - Undeclared variable: table_name
body: <p>I was rolling some queries into a stored procedure and I hit the <code>#1327 - Undeclared variable</code> error ... the odd thing though is that <strong>the variable it claims is undeclared is actually a table name</strong>.
  <br><br></p>
  
  <hr>
  
  <p><strong>Working through the problem</strong></p>
  
  <p>So I extracted the bit of the procedure where it was falling over and tried to run it as a normal SQL query directly on the database through PHPMyAdmin... same thing. After much tinkering it seems to be where I'm joining another table.</p>
  
  <p>If I run the query on a single table, it's fine, like this:</p>
  
  <pre><code>SET @i_channel_id = 3;
  SET @i_product_id = 90;
  
  SELECT 
  `product_status_to_channel`.`status_code` INTO @s_status_code
  
  FROM `product_status_to_channel`
  
  WHERE `product_status_to_channel`.`channel_id` = @i_channel_id
  AND `product_status_to_channel`.`product_id` = @i_product_id
  
  ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, 
      `product_status_to_channel`.`date` DESC
  
  LIMIT 0, 1;
  
  SELECT @s_status_code AS status_code;
  </code></pre>
  
  <p><br>
  Which outputs <em>'LIVE'</em> as the <code>status_code</code> in PHPMyAdmin - which is fine.
  <br><br></p>
  
  <hr>
  
  <p>However, when I try and <code>JOIN</code> to the message table to find the associated status message, I get the error: <code>#1327 - Undeclared variable: product_status_to_channel_lang</code> ... but <code>product_status_to_channel_lang</code> is a table?!</p>
  
  <pre><code>SET @i_channel_id = 3;
  SET @i_language_id = 3;
  SET @i_product_id = 90;
  
  SELECT 
  `product_status_to_channel`.`status_code` INTO @s_status_code,
  `product_status_to_channel_lang`.`string` INTO @s_status_message
  
  FROM `product_status_to_channel`
  
  LEFT JOIN `product_status_to_channel_lang`
      ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
      AND `product_status_to_channel_lang`.`language_id` = @i_language_id
  
  WHERE `product_status_to_channel`.`channel_id` = @i_channel_id
  AND `product_status_to_channel`.`product_id` = @i_product_id
  
  ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, `product_status_to_channel`.`date` DESC
  
  LIMIT 0, 1;
  
  SELECT @s_status_code AS status_code, @s_status_message AS status_message;
  </code></pre>
  
  <p><br>
  Is it trying to evaluate <code>product_status_to_channel_lang.product_status_to_channel_id</code> as a variable on the JOIN?</p>
  
  <pre><code>LEFT JOIN `product_status_to_channel_lang`
      ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
  </code></pre>
  
  <p><br>
  I assume I'm overlooking something obvious?</p>
  
  <p>I've tried this on both:</p>
  
  <ul>
  <li>a Win7 box running xampp with MySQL 5.5.27 - MySQL Community Server (GPL) </li>
  <li>a Debian box running MySQL 5.1.73-1-log - (Debian)</li>
  </ul>