mysql - union 和 limit 的不正确使用

标签 mysql sql union

我有一个大量的 SQL 语句,在升级到 MySQL 5.7 后我得到以下错误:

incorrect usage of union and limit

这是初始查询:

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

我已经尝试插入更多的括号,像这样:

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

但是我又遇到了另一个错误:

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'

我在这里做错了什么?

最佳答案

如果您使用limit,您需要在子查询周围加上括号。而且,UNION 不是 SELECT 语法的一部分。 . .所以你需要 SELECT 。 . . FROM 带有子查询。所以这可能有效:

 helper.persons = (SELECT tbl1
                   FROM ((SELECT IF(Min(shelper.persons) IS NULL, 1000, Min(shelper.persons)) as tb1
                          FROM tx_wbfincas_domain_model_finca_searchhelper 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 shelper
                          WHERE shelper.finca_uid = tx_wbfincas_domain_model_finca.uid 
                          LIMIT 1
                         )
                        ) x
                 ORDER BY tb1 ASC 
                 LIMIT 1
                )

关于mysql - union 和 limit 的不正确使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46930814/

相关文章:

MySQL:如何组合从另一个表多次引用的列并分配适当的别名?

java - 在 Hibernate 中为 Informix 数据库中的列进行类型转换

SQL 查询 : how to select records, 但如果存在父记录,请选择它的最新子记录

mysql - 如何使用 group by 'a' field by max of 'b' field 合并两个生成的表?

php - codeigniter mysql 时不时更新惩罚

PHP/SQL - 查询数组加载时间

java - 使用java将大量数据存储到mysql的最佳方法

sql - PostgreSQL:更新前 2 行语法错误?

sql - 为什么合并后只有一条记录?

sql - Hive:无法执行带限制的联合查询