title: MySQL how to find and compare min and max values in the same row
body: <p>I have searched but have been unable to find a solution that can be successfully adapted to my query.</p>
  
  <p>I have built a database that holds advertising banners and my script rotates and displays each each banner in succession, however I am trying to add in a query that will search in the table and display the banner with the highest CTR (click through ratio) based on its MIN impressions (amount of times the banner has been shown) and MAX clicks (the number of times the banner has been clicked)</p>
  
  <p>I have tried many different combinations of the search query some of them the syntax is wrong and others appear to have the correct syntax but fail to produce the expected results, this is the last query I have tried :</p>
  
  <pre><code>$query = "SELECT banner_id,banner_url,banner_alt,banner_size FROM banner_ads WHERE $banner_niche$banner_size banner_enable=1 AND banner_impressions=(SELECT MIN(banner_impressions) FROM banner_ads WHERE banner_clicks=(SELECT MAX(banner_clicks) FROM banner_ads));";
  $result = mysql_query($query) or die('Query failed: ' . mysql_error() . "\nQuery: $query");
  $row = mysql_fetch_array($result, MYSQL_NUM);
  $banner_id = $row[0];
  $banner_url = $row[1];
  $banner_alt = $row[2];
  list($banner_width, $banner_height) = split("x", $row[3]);
  </code></pre>
  
  <p>An example of the the field contents are :</p>
  
  <pre><code>banner_id / banners row ID number : 1
  banner_url / URL to the banner image : http://somesite.com/banner.jpg
  banner_alt / banner alt text : Visit SomeSite.com
  banner_size / size of the banner : 468x60
  banner_niche / the category of the banner : books
  banner_enable / is the banner set to display or not : 1
  banner_impressions / number of times banner has been shown : 1000
  banner_clicks / number if time banner has been clicked : 50
  </code></pre>
  
  <p>So simply I need query to search the table for a banner that has the least impressions with the most clicks.</p>
  
  <pre><code>----------------------------------------------
  banner_id | banner_impressions | banner_clicks
  ----------------------------------------------
  1         | 1000               | 50
  2         | 100                | 80
  3         | 2000               | 90
  ----------------------------------------------
  </code></pre>
  
  <p>So in the above example banner_id 2 would match the criteria. (I skipped the other fields in the example because they are not relevant)  </p>
  
  <p>Thanks in advance guys, I hope I have explained well enough what I am trying to achieve. </p>