我有一个庞大的SQL语句,升级到MySQL 5.7后,我收到以下错误:
不正确使用union和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
但后来又出现了另一个错误:
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'UNION'附近使用正确的语法
我在这里做错了什么?
答案 0 :(得分:1)
如果您使用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
)
答案 1 :(得分:1)
我有解决方案: 我必须在Union之前删除嵌套Query中的第一个限制。
查询:
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
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
答案 2 :(得分:0)
是的,我的第二次尝试是用更多括号。 使用您的查询我
Unknown column 'tx_wbfincas_domain_model_finca.uid' in 'where clause'
你的第一个选择应该是" SELECT tb1"而不是tbl1 - 我是对的吗?
我是否应该加入子查询中的tx_wbfincas_domain_model_finca?