UNION 和 ORDER BY 的使用不正确?

发布于 2024-11-24 05:49:35 字数 544 浏览 0 评论 0原文

我如何在mysql中使用unionorder 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

笛声青案梦长安 2024-12-01 05:50:01

正确的是:

(SELECT *
   FROM _member_facebook
   INNER JOIN _member_pts ON _member_facebook._fb_owner=_member_pts._username
   WHERE _member_facebook._promote_point = 9 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)
ORDER BY 1

The correct is:

(SELECT *
   FROM _member_facebook
   INNER JOIN _member_pts ON _member_facebook._fb_owner=_member_pts._username
   WHERE _member_facebook._promote_point = 9 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)
ORDER BY 1
靑春怀旧 2024-12-01 05:50:01

尝试()我想像

(SELECT  CITY,LENGTH(CITY) FROM STATION WHERE LENGTH(CITY)=(SELECT MIN(LENGTH(CITY)) FROM STATION) ORDER BY CITY LIMIT 1) 
UNION ALL
(SELECT  CITY,LENGTH(CITY) FROM STATION WHERE LENGTH(CITY)=(SELECT MAX(LENGTH(CITY)) FROM STATION) ORDER BY CITY LIMIT 1);

try () i think like

(SELECT  CITY,LENGTH(CITY) FROM STATION WHERE LENGTH(CITY)=(SELECT MIN(LENGTH(CITY)) FROM STATION) ORDER BY CITY LIMIT 1) 
UNION ALL
(SELECT  CITY,LENGTH(CITY) FROM STATION WHERE LENGTH(CITY)=(SELECT MAX(LENGTH(CITY)) FROM STATION) ORDER BY CITY LIMIT 1);
仅一夜美梦 2024-12-01 05:50:01

我认为如果您使用 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.

高冷爸爸 2024-12-01 05:50:00

说明:

了解其工作原理对于避免类似用例中的“陷阱”非常重要。请注意,union 的语法有些“特殊”:

子语句 union all 子语句 union all 子语句 [order by-子句] [limit-子句]

其中“子语句”可以可选地被 ( 和<代码>)。一些工作示例:

  • select 1 union all (select 2);
    选择 1 并集全部选择 2 并集全部(选择 3);
    选择 1 并集所有(选择 2) 并集所有选择 3;
    选择 1 并集全部(选择 2) 并集全部(选择 3);
    选择1 联合所有(选择2) 联合所有(选择3) 联合所有选择4;
    选择 1 并集全部(选择 2) 并集全部 选择 3 并集全部(选择 4);
    

但是,如果您用大括号将第一个“子语句”括起来,则必须将所有其他“< em>子语句“带大括号:

  • (select 1) union all (select 2) union all (select 3);
    

(注意,上述一点在

如果不这样做,就会出现语法错误:

  • mysql> (选择1)并集所有选择2; -- 错误,因为并非所有“子语句”都用大括号括起来
    ERROR 1064 (42000):您的 SQL 语法有错误;检查...
    mysql> (选择 1) 并集全部 (选择 2) 并集全部 选择 3; -- 错误,因为并非所有“子语句”都用大括号括起来
    错误 1064 (42000):您遇到错误...
    mysql> (选择 1) 并集全部 选择 2 并集全部 (选择 3); -- 错误,因为并非所有“子语句”都用大括号括起来
    错误 1064 (42000):您有错误...
    

接下来,每个“子语句”可以包含 wheregroup byhavingjoinlimit,但不order by

如果您想使用 order by,则包含 order by 的“子语句”必须用大括号括起来。 (这意味着它们不再是可选的。)

现在,如果我们再次查看语法:

子语句 union all 子语句 union all 子语句 [order by-子句] [limit-子句]

我们可以看到整个union 语句以可选的 order by / 结尾限制。这两个关键字适用于整个 union 语句,而不仅仅是最后一个“子语句”:

  • mysql>;选择1
        ->联合所有
        ->选择2限制1;
    +---+
    | 1 |
    +---+
    | 1 |
    +---+
    1 行一组(0.00 秒)
    
    mysql>
    

我们之前提到过 limit 关键字也可以应用于单个“子语句”:

  • mysql>选择 1 限制 1
        ->联合所有
        ->选择2;
    +---+
    | 1 |
    +---+
    | 1 |
    | 2 |
    +---+
    2 行一组(0.00 秒)
    
    mysql>
    

如果您想将limit应用于最后一个“子语句”(而不是整个union语句) ,您必须用大括号括住最后一个“子语句”:

  • mysql>;选择1
        ->联合所有
        -> (选择2限制1);
    +---+
    | 1 |
    +---+
    | 1 |
    | 2 |
    +---+
    2 行一组(0.00 秒)
    
    mysql>
    

limit应用于最后一个“子语句以及整个联合 语句,使用:

  • mysql>选择1
        ->联合所有
        -> (选择2限制1)限制1;
    +---+
    | 1 |
    +---+
    | 1 |
    +---+
    1 行一组(0.00 秒)
    
    mysql>
    

order by相同:

  • mysql>选择1
        ->联合所有
        -> (选择2按1排序)按1排序;
    +---+
    | 1 |
    +---+
    | 1 |
    | 2 |
    +---+
    2 行一组(0.00 秒)
    
    mysql>
    

但请注意,将 order by 应用于“子语句”是毫无意义,因为文档已明确声明仅保证order bycf.)在应用时有效到整个union 语句:

–§– ..使用各个 SELECT 语句的 ORDER BY 并不意味着行在最终结果中出现的顺序。

order by 在“substatement" 是如果你将它与限制

–§– ..ORDER BY 的使用在这种情况下,通常与LIMIT结合使用,因此它用于确定要为SELECT检索的选定行的子集,即使它确实不一定影响这些行的顺序最终的UNION结果。


另外,如果您想将 select 合并到< /em> 使用union,将会有更多需要注意的“陷阱”。有关此问题,请参阅问题 32858

Explanation:

It's important to understand how this works to avoid "gotchas" in similar use cases. Note thatunion's syntax is somewhat "special":

substatement union all substatement union all substatement [order by-clause] [limit-clause]

where "substatement" can optionally be surrounded by ( and ). Some working examples:

  • select 1 union all (select 2);
    select 1 union all  select 2  union all (select 3);
    select 1 union all (select 2) union all  select 3;
    select 1 union all (select 2) union all (select 3);
    select 1 union all (select 2) union all (select 3) union all  select 4;
    select 1 union all (select 2) union all  select 3  union all (select 4);
    

However, if you surround the first "substatement" with braces, you must surround all the other "substatement"s with braces:

  • (select 1) union all (select 2) union all (select 3);
    

(Note that the above point is not mentioned in the official docs.)

Failing to do that is a syntax error:

  • mysql> (select 1) union all select 2; -- error because not all "substatement"s are braced
    ERROR 1064 (42000): You have an error in your SQL syntax; check the...
    mysql> (select 1) union all (select 2) union all  select 3; -- error because not all "substatement"s are braced
    ERROR 1064 (42000): You have an error...
    mysql> (select 1) union all  select 2  union all (select 3); -- error because not all "substatement"s are braced
    ERROR 1064 (42000): You have an error...
    

Next, each "substatement" can contain where, group by, having, join, limit, but not order by.

If you'd like to use order by, the "substatement" that contains order by must be surrounded by braces. (Which means they are no longer optional.)

Now, if we'd look at the syntax again:

substatement union all substatement union all substatement [order by-clause] [limit-clause]

we can see that the entire union statement ends with an optional order by / limit. These two keywords apply to the entire union statement, not just the last "substatement":

  • mysql> select 1
        -> union all
        -> select 2 limit 1;
    +---+
    | 1 |
    +---+
    | 1 |
    +---+
    1 row in set (0.00 sec)
    
    mysql>
    

We've mentioned previously that the limit keyword can also be applied to individual "substatement"s:

  • mysql> select 1 limit 1
        -> union all
        -> select 2;
    +---+
    | 1 |
    +---+
    | 1 |
    | 2 |
    +---+
    2 rows in set (0.00 sec)
    
    mysql>
    

If you want to apply limit to the last "substatement" (as opposed to the entire union statement), you must surround the last "substatement" with braces:

  • mysql> select 1
        -> union all
        -> (select 2 limit 1);
    +---+
    | 1 |
    +---+
    | 1 |
    | 2 |
    +---+
    2 rows in set (0.00 sec)
    
    mysql>
    

To apply limit to the the last "substatement" and also to the entire union statement, use:

  • mysql> select 1
        -> union all
        -> (select 2 limit 1)limit 1;
    +---+
    | 1 |
    +---+
    | 1 |
    +---+
    1 row in set (0.00 sec)
    
    mysql>
    

It's the same with order by:

  • mysql> select 1
        -> union all
        -> (select 2 order by 1)order by 1;
    +---+
    | 1 |
    +---+
    | 1 |
    | 2 |
    +---+
    2 rows in set (0.00 sec)
    
    mysql>
    

But note that applying order by to "substatement"s is meaningless because the docs have explicitly stated that order by is only guaranteed (cf.) to work when applied to the entire union statement:

–§–  ..use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result..

The only way order by would make sense in a "substatement" is if you combine it with limit:

–§–  ..the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result.

Also, if you want to combine select into with union, there'll be more "gotchas" to watch out for. See issue 32858 regarding this.

睫毛溺水了 2024-12-01 05:50:00

在查询中使用 Order by 和 limit 子句时,使用括号解决了我的问题。我的要求是在特定条件下获取表格中的顶部和底部行,以下代码对我有用:

(SELECT column1, column2
FROM table1
ORDER BY column1, column2
LIMIT 1)

UNION

(SELECT column1, column2
FROM table2
ORDER BY column1, column2 
LIMIT 1)

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:

(SELECT column1, column2
FROM table1
ORDER BY column1, column2
LIMIT 1)

UNION

(SELECT column1, column2
FROM table2
ORDER BY column1, column2 
LIMIT 1)
心头的小情儿 2024-12-01 05:49:59

带括号:

(
    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
)

也就是说,MySQL 并不强制在外部子句中保留内部排序,尽管它可能会这样做,因为无论如何它都需要对行进行排序来计算相应的 LIMIT 子句。

With parenthesis:

(
    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
)

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.

因为看清所以看轻 2024-12-01 05:49:56

尝试使用:

(
  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
)

尽管如此,我认为您应该将 ORDER BY 子句放在第二个查询的末尾

Try with:

(
  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
)

Although, I think you should put the ORDER BY clause at the end of the second query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文