UNION 和 ORDER BY 的使用不正确?
我如何在mysql中使用union和order by?
select * from _member_facebook
inner join _member_pts
ON _member_facebook._fb_owner=_member_pts._username
where _member_facebook._promote_point = 9
ORDER BY RAND() limit 2
UNION ALL
select * from _member_facebook
inner join _member_pts
ON _member_facebook._fb_owner=_member_pts._username
where _member_facebook._promote_point = 8 limit 3
给我错误
#1221 - Incorrect usage of UNION and ORDER BY
任何人都可以帮忙吗?
how can i use union and order by in mysql ?
select * from _member_facebook
inner join _member_pts
ON _member_facebook._fb_owner=_member_pts._username
where _member_facebook._promote_point = 9
ORDER BY RAND() limit 2
UNION ALL
select * from _member_facebook
inner join _member_pts
ON _member_facebook._fb_owner=_member_pts._username
where _member_facebook._promote_point = 8 limit 3
give me error
#1221 - Incorrect usage of UNION and ORDER BY
any one can help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正确的是:
The correct is:
尝试()我想像
try () i think like
我认为如果您使用 order by 或 limit 或两者都使用,则必须使用括号。我尝试通过交替使用 limit 和 order by 而不使用括号来处理查询,但查询不起作用。只有在添加括号后才起作用。
I think use of parenthesis is mandatory if you use order by or limit or both. I tried working on a query by using limit and order by interchangeably without parenthesis and the query did not work. It only worked after adding parenthesis.
说明:
了解其工作原理对于避免类似用例中的“陷阱”非常重要。请注意,
union
的语法有些“特殊”:其中“子语句”可以可选地被
(
和<代码>)。一些工作示例:但是,如果您用大括号将第一个“子语句”括起来,则必须将所有其他“< em>子语句“带大括号:
(注意,上述一点在
如果不这样做,就会出现语法错误:
接下来,每个“子语句”可以包含
where
、group by
、having
、join
、limit
,但不order by
。如果您想使用
order by
,则包含order by
的“子语句”必须用大括号括起来。 (这意味着它们不再是可选的。)现在,如果我们再次查看语法:
我们可以看到整个
union 语句以可选的
order by
/ 结尾限制
。这两个关键字适用于整个union
语句,而不仅仅是最后一个“子语句”:我们之前提到过
limit
关键字也可以应用于单个“子语句”:如果您想将
limit
应用于最后一个“子语句”(而不是整个union
语句) ,您必须用大括号括住最后一个“子语句”:将
limit
应用于最后一个“子语句”以及整个联合 语句,使用:
与
order by
相同:但请注意,将
order by
应用于“子语句”是毫无意义,因为文档已明确声明仅保证order by
(cf.)在应用时有效到整个union
语句:order by
在“substatement" 是如果你将它与限制
:另外,如果您想将
select 合并到< /em>
使用union
,将会有更多需要注意的“陷阱”。有关此问题,请参阅问题 32858。Explanation:
It's important to understand how this works to avoid "gotchas" in similar use cases. Note that
union
's syntax is somewhat "special":where "substatement" can optionally be surrounded by
(
and)
. Some working examples:However, if you surround the first "substatement" with braces, you must surround all the other "substatement"s with braces:
(Note that the above point is not mentioned in the official docs.)
Failing to do that is a syntax error:
Next, each "substatement" can contain
where
,group by
,having
,join
,limit
, but notorder by
.If you'd like to use
order by
, the "substatement" that containsorder by
must be surrounded by braces. (Which means they are no longer optional.)Now, if we'd look at the syntax again:
we can see that the entire
union
statement ends with an optionalorder by
/limit
. These two keywords apply to the entireunion
statement, not just the last "substatement":We've mentioned previously that the
limit
keyword can also be applied to individual "substatement"s:If you want to apply
limit
to the last "substatement" (as opposed to the entireunion
statement), you must surround the last "substatement" with braces:To apply
limit
to the the last "substatement" and also to the entireunion
statement, use:It's the same with
order by
:But note that applying
order by
to "substatement"s is meaningless because the docs have explicitly stated thatorder by
is only guaranteed (cf.) to work when applied to the entireunion
statement:The only way
order by
would make sense in a "substatement" is if you combine it withlimit
:Also, if you want to combine
select into
withunion
, there'll be more "gotchas" to watch out for. See issue 32858 regarding this.在查询中使用 Order by 和 limit 子句时,使用括号解决了我的问题。我的要求是在特定条件下获取表格中的顶部和底部行,以下代码对我有用:
Using parentheses fixed my problem while using Order by and limit clauses in the query. My requirement was to get the top and the bottom row in the table with a certain condition and the following code worked for me:
带括号:
也就是说,MySQL 并不强制在外部子句中保留内部排序,尽管它可能会这样做,因为无论如何它都需要对行进行排序来计算相应的
LIMIT
子句。With parenthesis:
Said that, it isn't mandatory for MySQL to keep the inner sorting in the outer clause—though it'll probably do so since it needs to sort rows anyway to calculate the corresponding
LIMIT
clauses.尝试使用:
尽管如此,我认为您应该将
ORDER BY
子句放在第二个查询的末尾Try with:
Although, I think you should put the
ORDER BY
clause at the end of the second query