title: incorrect usage of union and limit
body: <p>I have a massive SQL Statement and after upgrading to MySQL 5.7 I get the following error:</p>
  
  <blockquote>
    <p>incorrect usage of union and limit</p>
  </blockquote>
  
  <p>This is the initial query: </p>
  
  <pre><code>SELECT    tx_wbfincas_domain_model_finca.*,
            helper.*
  FROM      tx_wbfincas_domain_model_finca
  LEFT JOIN tx_wbfincas_domain_model_finca_searchhelper AS helper
  ON        tx_wbfincas_domain_model_finca.uid = helper.finca_uid
  WHERE     hidden=0
  AND       deleted=0
  AND       helper.persons =
            (
                   SELECT IF(Min(shelper.persons) IS NULL, 1000, Min(shelper.persons)) as tb1
                   FROM   tx_wbfincas_domain_model_finca_searchhelper                  AS shelper
                   WHERE  shelper.finca_uid = tx_wbfincas_domain_model_finca.uid limit 1
                   UNION
                            (
                                   SELECT max(shelper.persons)                        AS tb1
                                   FROM   tx_wbfincas_domain_model_finca_searchhelper AS shelper
                                   WHERE  shelper.finca_uid = tx_wbfincas_domain_model_finca.uid limit 1 )
                   ORDER BY tb1 ASC limit 1)
  AND       uid IN
            (
                   SELECT uid_local
                   FROM   tx_wbfincas_finca_category_mm
                   WHERE  uid_foreign=4)
  GROUP BY  uid
  ORDER BY  min_price ASC ;
  
  https://pastebin.com/H2eaNzJ6
  </code></pre>
  
  <p>I already tried to insert more parentheses, like this:  </p>
  
  <pre><code>SELECT    tx_wbfincas_domain_model_finca.*,
            helper.*
  FROM      tx_wbfincas_domain_model_finca
  LEFT JOIN tx_wbfincas_domain_model_finca_searchhelper AS helper
  ON        tx_wbfincas_domain_model_finca.uid = helper.finca_uid
  WHERE     hidden=0
  AND       deleted=0
  AND       helper.persons =
            ((SELECT IF(Min(shelper.persons) IS NULL, 1000, Min(shelper.persons)) as tb1
                      FROM   tx_wbfincas_domain_model_finca_searchhelper                  AS shelper
                      WHERE  shelper.finca_uid = tx_wbfincas_domain_model_finca.uid limit 1 )
                   UNION
                   (SELECT max(shelper.persons) as tb1
                           FROM   tx_wbfincas_domain_model_finca_searchhelper AS shelper
                           WHERE  shelper.finca_uid = tx_wbfincas_domain_model_finca.uid limit 1 )
                   ORDER BY tb1 ASC limit 1)
  AND       uid IN
            (SELECT uid_local
                   FROM   tx_wbfincas_finca_category_mm
                   WHERE  uid_foreign=4)
  GROUP BY  uid
  ORDER BY  min_price ASC ;
  
  https://pastebin.com/mctaxTNL
  </code></pre>
  
  <p>But then I get another error:</p>
  
  <blockquote>
    <p>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION'</p>
  </blockquote>
  
  <p>What am I doing wrong here?</p>