single-table 和 multiple-table 语法之间的区别?
Difference between single-table and multiple-table syntax?
我试图了解 MYSQL 如何确定单 table 和多 table 语法,但我找不到此信息。 documentation 解释了 MYSQL 如何处理这两者,但没有解释是什么决定了它们。
文档说:
You can specify multiple tables in a DELETE statement to delete rows
from one or more tables depending on the particular condition in the
WHERE clause. However, you cannot use ORDER BY or LIMIT in a
multiple-table DELETE.
以这个查询为例:
DELETE table FROM table
INNER JOIN other ON other.column = table.column
WHERE other.column2 = ?
LIMIT 1
这是单个还是多个 table 语法?有一个 JOIN
,因此您可以倾向于多个,但它只是从一个 table 中删除。我的另一个怀疑是,它是在多个 WHERE
子句用于多个 table 时确定的。如果您能在回答中包含这两个示例,我们将不胜感激!
编辑:
我问这个问题是因为当使用 LIMIT
执行某些 DELETE
查询时,我得到一个错误,你不能将 LIMIT
与 multiple-table 语法一起使用。
编辑#2:
简而言之,如果要在 DELETE
查询中加入 table,则不能使用 ORDER BY
或 LIMIT
。
如果您指定 DELETE
来自 1 个 table,并且您的 DELETE
语句中没有涉及 JOIN
,那么它是一个单一的 table语法。否则,它是一个 multi-table 语法。我很确定这就是文档页面的意思。
我认为这种解释的证据是下面的页面说:
The table_references clause lists the tables involved in the join. Its syntax is described in Section 13.2.8.2, "JOIN Syntax".
请注意,在单 table 语法中没有 table_references
元素。
MySQL's documentation states the following
关于 "single-table" 语法:
If the ORDER BY clause is specified, the rows are deleted in the order that is specified. The LIMIT clause places a limit on the number of rows that can be deleted.
关于 "multi-table" 语法:
For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.
一些测试揭示了 ORDER BY
限制。
这是一个有效的单table DELETE
语句,带有 ORDER BY
:
DELETE FROM table
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
具有针对另一个 table 的显式连接条件的类似查询会导致 ORDER BY
出现语法错误,尽管 table 中只有一个被删除:
DELETE table1
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
用隐式连接指定相同的查询以同样的方式失败
DELETE table1
FROM table1, table2
WHERE
table1.id = table2.id
AND somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
在连接的 (multi-table) 语句中,在 DELETE
之后没有指定 table 以进行删除,因此它看起来更像是一个单一的 table 语法也是一个语法错误
DELETE /* no table named here */
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE somecol
这是 DELETE
子句中命名的 table:
最后,使用 only one table,但使用 mult-table 语法(在 [=16 之后命名 table =] 关键字) 不允许 允许 ORDER BY
,因此 此处真正的识别差异似乎是 table 在 [=16] 中命名=] 子句 区分多table 和单table:
此查询仅涉及一个 table(没有连接),但会产生语法错误:
/* name table1 in the DELETE clause */
DELETE table1
/* but not other table is joined */
FROM table1
WHERE somecol = 'someval'
ORDER BY id LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY id LIMIT 1
我试图了解 MYSQL 如何确定单 table 和多 table 语法,但我找不到此信息。 documentation 解释了 MYSQL 如何处理这两者,但没有解释是什么决定了它们。
文档说:
You can specify multiple tables in a DELETE statement to delete rows from one or more tables depending on the particular condition in the WHERE clause. However, you cannot use ORDER BY or LIMIT in a multiple-table DELETE.
以这个查询为例:
DELETE table FROM table
INNER JOIN other ON other.column = table.column
WHERE other.column2 = ?
LIMIT 1
这是单个还是多个 table 语法?有一个 JOIN
,因此您可以倾向于多个,但它只是从一个 table 中删除。我的另一个怀疑是,它是在多个 WHERE
子句用于多个 table 时确定的。如果您能在回答中包含这两个示例,我们将不胜感激!
编辑:
我问这个问题是因为当使用 LIMIT
执行某些 DELETE
查询时,我得到一个错误,你不能将 LIMIT
与 multiple-table 语法一起使用。
编辑#2:
简而言之,如果要在 DELETE
查询中加入 table,则不能使用 ORDER BY
或 LIMIT
。
如果您指定 DELETE
来自 1 个 table,并且您的 DELETE
语句中没有涉及 JOIN
,那么它是一个单一的 table语法。否则,它是一个 multi-table 语法。我很确定这就是文档页面的意思。
我认为这种解释的证据是下面的页面说:
The table_references clause lists the tables involved in the join. Its syntax is described in Section 13.2.8.2, "JOIN Syntax".
请注意,在单 table 语法中没有 table_references
元素。
MySQL's documentation states the following
关于 "single-table" 语法:
If the ORDER BY clause is specified, the rows are deleted in the order that is specified. The LIMIT clause places a limit on the number of rows that can be deleted.
关于 "multi-table" 语法:
For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.
一些测试揭示了 ORDER BY
限制。
这是一个有效的单table DELETE
语句,带有 ORDER BY
:
DELETE FROM table
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
具有针对另一个 table 的显式连接条件的类似查询会导致 ORDER BY
出现语法错误,尽管 table 中只有一个被删除:
DELETE table1
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
用隐式连接指定相同的查询以同样的方式失败
DELETE table1
FROM table1, table2
WHERE
table1.id = table2.id
AND somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
在连接的 (multi-table) 语句中,在 DELETE
之后没有指定 table 以进行删除,因此它看起来更像是一个单一的 table 语法也是一个语法错误
DELETE /* no table named here */
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE somecol
这是 DELETE
子句中命名的 table:
最后,使用 only one table,但使用 mult-table 语法(在 [=16 之后命名 table =] 关键字) 不允许 允许 ORDER BY
,因此 此处真正的识别差异似乎是 table 在 [=16] 中命名=] 子句 区分多table 和单table:
此查询仅涉及一个 table(没有连接),但会产生语法错误:
/* name table1 in the DELETE clause */
DELETE table1
/* but not other table is joined */
FROM table1
WHERE somecol = 'someval'
ORDER BY id LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY id LIMIT 1