title: Strange MySQL query results with aggregate function inside coalesce (ifnull also)
body: <p>What we have table</p>
  
  <pre>
  id  where_id    price
  1      1          1
  2      2          2
  </pre>
  
  <p>and sql query:</p>
  
  <pre><code>select
  t_o.*,
  coalesce(
    (
        select 
               t_o2.price 
        from test_objects t_o2 where t_o2.where_id = t_o.where_id 
        order by t_o2.price asc limit 1
    )
    ,min(t_o.price)
  ) as WrongColumn
  from test_objects t_o
  where t_o.where_id in (1,2)
  </code></pre>
  
  <p>it returns:</p>
  
  <pre>
  id  where_id    price   WrongColumn
  1      1          1         2
  </pre>
  
  <p>Why this query returns ID = 1 and WrongColumn = 2, if second and third (see right next below) queries return same row with ID = 1 and WrongColumn = 1 (and not 2)?</p>
  
  <p>second query:</p>
  
  <pre><code>select
  t_o.*,
  coalesce(
    (
        select 
               t_o2.price 
        from test_objects t_o2 where t_o2.where_id = t_o.where_id 
        order by t_o2.price asc limit 1
    ),
    t_o.price
  ) as WrongColumn
  from test_objects t_o
  where t_o.where_id in (1,2)
  </code></pre>
  
  <p>returns:</p>
  
  <pre>
  id  where_id    price   WrongColumn
  1      1          1          1
  2      2          2          2
  </pre>
  
  <p>third query:</p>
  
  <pre><code>select
  t_o.*,
  coalesce(
    (
        select 
               t_o2.price 
        from test_objects t_o2 where t_o2.where_id = t_o.where_id 
        order by t_o2.price asc limit 1
    )
  ) as WrongColumn
  ,min(t_o.price)
  from test_objects t_o
  where t_o.where_id in (1,2)
  </code></pre>
  
  <p>returns:</p>
  
  <pre>
  
  id  where_id    price   WrongColumn min(t_o.price)
  1      1          1         1             1
  
  </pre>
  
  <p>Sql to create tables is here:</p>
  
  <pre><code>CREATE TABLE `test_objects` (
    `id` int(11) NOT NULL,
    `where_id` int(11) NOT NULL,
    `price` int(11) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
  INSERT INTO `test_objects` (`id`, `where_id`, `price`) VALUES
  (1, 1,  1),
  (2, 2,  2);
  </code></pre>