gpt4 book ai didi

mysql - 联合与内连接,mysql错误

转载 作者:行者123 更新时间:2023-11-29 13:14:58 25 4
gpt4 key购买 nike

(SELECT * FROM app_detailsvvv as dtable INNER JOIN new_apps ON 
new_apps.trackId=dtable.trackId WHERE primaryGenreName='Games'
AND composed='1' AND new_apps.top>0)
UNION (SELECT * FROM app_detailsvvv as dtable WHERE primaryGenreName='Games')
LIMIT 12

错误:

#1222 - The used SELECT statements have a different number of columns

在 new_apps 上,有些字段不在 app_detailsvvv 中,我如何以某种方式屏蔽联合中的第二个查询。

编辑:

(SELECT dtable.* FROM app_detailsvvv as dtable INNER JOIN new_apps ON new_apps.trackId=dtable.trackId WHERE primaryGenreName='Games' AND composed='1' AND new_apps.top>0) UNION (SELECT * FROM app_detailsvvv as dtable WHERE primaryGenreName='Games') LIMIT 12

当我添加 ORDER BY new_apps.top ASC 时,我收到了这个新错误:

#1250 - Table 'new_apps' from one of the SELECTs cannot be used in global ORDER clause

最佳答案

我认为您不需要联合,只需一个左外连接:

SELECT dtable.*
FROM app_detailsvvv as dtable LEFT OUTER JOIN
new_apps
ON new_apps.trackId = dtable.trackId and
composed = '1' AND new_apps.top > 0
WHERE dtable.primaryGenreName = 'Games'
LIMIT 12;

嗯,这可能会返回重复项,您可以使用 select different dtable.* 删除重复项。

但是等等。除了返回第一个表中的所有行之外,该查询实际上没有执行任何操作。我怀疑您想要返回 12 行,并优先考虑匹配的行。如果是这种情况,那么您想要的查询是:

SELECT dtable.*
FROM app_detailsvvv as dtable LEFT OUTER JOIN
new_apps
ON new_apps.trackId = dtable.trackId
WHERE dtable.primaryGenreName = 'Games'
GROUP BY dtable.trackId <-- or whatever the unique id is on the table
ORDER BY (composed = '1' AND new_apps.top > 0) desc
LIMIT 12;

关于mysql - 联合与内连接,mysql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21590813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com