title: Why isn't the user-define variable working
body: <p>I am trying to use a subquery in an if statement.
  If I do the following it works</p>
  
  <pre><code>SELECT `Currency`, IF((SELECT `id` FROM `upload_currency` WHERE `CurrencyText`=`Currency`) &gt; 0,(SELECT `id` FROM `upload_currency` WHERE `CurrencyText`=`Currency`),1) FROM `upload_salesinvoice`
  </code></pre>
  
  <p>and I get the output
  1,1,USD 3,1,EUR 2,1,1,1 which is correct</p>
  
  <p>However if I try to make my query more efficient like this, it gives me all 1's instead.</p>
  
  <pre><code>SELECT `Currency`, IF(@val:=(SELECT `id` FROM `upload_currency` WHERE `CurrencyText`=`Currency`) &gt; 0,@val,1) FROM `upload_salesinvoice`
  </code></pre>
  
  <p>I will explain a bit more I have a table called upload_currency and in this table I have this layout</p>
  
  <p>id 1, CurrencyText GBP</p>
  
  <p>id 2, CurrencyText EUR</p>
  
  <p>id 3, CurrencyText USD</p>
  
  <p>So my subselect should return 1 for GBP, or 1 for an empty string, 2 for EUR and 3 for USD, but the second query is failing and giving me all 1's</p>
  
  <p>So it looks like my user-defined variable is failing for some reason.</p>