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
which was executed by mysql well though.
Why I think it should fail:
SELECT
clauseI find it unexpected to run and don't have an explanation why it works.
It does not fit the grammar defined on https://dev.mysql.com/doc/refman/5.5/en/select.html
So, why is it valid? Any references?
Other statements are not supported. For compliance with the SQL standard, which states that diagnostics statements are not preparable, MySQL does not support the following as prepared statements: Statements containing any reference to the warning_count or error_count system variable.
The data type is a guideline for SQL to understand what type of data is expected inside of each column, and it also identifies how SQL will interact with the stored data. In MySQL there are three main data types: string, numeric, and date and time. A FIXED length string (can contain letters, numbers, and special characters).
For example, you cannot use the mysql_stmt_prepare () C API function to prepare a PREPARE , EXECUTE, or DEALLOCATE PREPARE statement. SQL syntax for prepared statements can be used within stored procedures, but not in stored functions or triggers.
For definitions of binary string columns ( BINARY , VARBINARY, and the BLOB types), MySQL interprets length specifications in byte units. Column definitions for character string data types CHAR , VARCHAR, the TEXT types, ENUM , SET, and any synonyms) can specify the column character set and collation: CHARACTER SET specifies the character set.
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 ...
Not related to subqueries at all.
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; }
;
The syntax is useful when you want to sort the end result of the UNION.
The following would sort only the last SELECT:
SELECT …
UNION
SELECT …
UNION
SELECT … ORDER BY views
But this would sort the whole result:
(SELECT …)
UNION
(SELECT …)
UNION
(SELECT …) ORDER BY views
You're doing something like this query, but you've got just one SELECT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With