简体   繁体   English

Union 与内连接,mysql 错误

[英]Union with inner join, mysql error

(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

error:错误:

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

on new_apps there are fields that are not in app_detailsvvv, how can I mask for the second query in the union somehow.在 new_apps 上,有些字段不在 app_detailsvvv 中,我如何才能以某种方式屏蔽联合中的第二个查询。

edit:编辑:

(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

worked yet when I add ORDER BY new_apps.top ASC I get this new error:当我添加ORDER BY new_apps.top ASC我收到这个新错误:

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

I don't think you need a union , just a left outer join :我不认为你需要一个union ,只需要一个left outer join

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;

Hmmm, this might return duplicates, which you can get rid of using select distinct dtable.* .嗯,这可能会返回重复项,您可以使用select distinct dtable.*摆脱这些重复项。

But wait.可是等等。 This query really isn't doing anything other than returning all the rows in the first table.除了返回第一个表中的所有行之外,这个查询实际上没有做任何事情。 I suspect that you want to return 12 rows, with a priority given to the ones with a match.我怀疑您想返回 12 行,优先考虑匹配的行。 If that is the case, then the query you want is:如果是这种情况,那么您想要的查询是:

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;

Try this:尝试这个:

(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM