为什么给定的语法在mysql中有效? [英] Why the given syntax is valid in mysql?

查看:54
本文介绍了为什么给定的语法在mysql中有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个答案中,我发现了一种奇怪的语法:

In another answer I've spotted a weird syntax:

(SELECT * FROM `articles` 
 WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 30 DAY))
 ORDER BY `views` DESC 
 LIMIT 20
) ORDER by `views` ASC

虽然由mysql执行得很好.

which was executed by mysql well though.

为什么我认为它应该失败:

Why I think it should fail:

  1. 子查询没有别名
  2. 整个查询缺少SELECT子句

我发现它无法运行,也没有解释为什么会起作用.

I find it unexpected to run and don't have an explanation why it works.

它不符合 https://dev上定义的语法.mysql.com/doc/refman/5.5/en/select.html

那么,为什么有效呢?有参考吗?

So, why is it valid? Any references?

推荐答案

这是带有最终ORDER BY的替代UNION语法.

It's the alternative UNION syntax with a final ORDER BY.

这就是两个选择之间的联合:

This is what such a union between two selects looks like:

(SELECT ...)
UNION
(SELECT ...) ORDER BY ... LIMIT ...

这就是一个选择之间的这种联合看起来是这样的:

And this is what such a union between one select looks like:

(SELECT ...) ORDER BY ... LIMIT ...

与子查询完全无关.

这没有在MySQL中记录,但是从语法:

This isn't documented in MySQL, but is obvious from the grammar:

top_level_select_init:
        SELECT_SYM
        {
            Lex->sql_command= SQLCOM_SELECT;
        }
        select_init2
        | '(' select_paren ')' union_opt
        ;


/* Need select_init2 for subselects. */
union_select_init:
        SELECT_SYM select_init2
        | '(' select_paren ')' union_opt
        ;

...

union_opt:
        /* Empty */ { $$= 0; }
        | union_list { $$= 1; }
        | union_order_or_limit { $$= 1; }
        ;

这篇关于为什么给定的语法在mysql中有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆