简体   繁体   English

MySQL - 两个语句之间的区别

[英]MySQL - Difference between two statements

I noticed this SQL query in a documentation:我在文档中注意到了这个 SQL 查询:

UPDATE pages page
LEFT JOIN pages parentpage ON page.pid = parentpage.uid
LEFT JOIN pages grandparentpage ON parentpage.pid = grandparentpage.uid
LEFT JOIN pages grandgrandparentpage ON grandparentpage.pid = grandgrandparentpage.uid
SET page.is_hidden = 0
WHERE page.uid = 5
OR page.pid = 5
OR parentpage.pid = 5
OR grandparentpage.pid = 5
OR grandgrandparentpage.pid = 5;

Isn't this exactly the same as:这不是完全一样的:

UPDATE pages page
SET page.is_hidden = 0
WHERE page.uid = 5
OR page.pid = 5;

PID and UID are filtered by the where clause to equal 5. Therefore I don't see, why this JOINs would be important. PID 和 UID 由 where 子句过滤为等于 5。因此我不明白为什么这个 JOIN 会很重要。

But maybe I am missing the point?但也许我没有抓住重点?

Any help is appreciated.任何帮助表示赞赏。

Thanks in advance.提前致谢。

You are missing something.你错过了一些东西。 See db-fiddle .db-fiddle The first SQL will up date not only the row with uid 5 but also its child, grandchild and great-grandchild if they exist.第一条 SQL 不仅会更新uid 5 的行,还会更新其子、孙和曾孙(如果存在)。

They are different, because the first query updates all children, grandchildren and grandgrandchildren rows of a pid=5 or uid=5 row.它们是不同的,因为第一个查询更新pid=5uid=5行的所有子行、孙子行和孙子行。 The second will update only the rows themselves.第二个将只更新行本身。

Here's an example how the queries are different:以下是查询如何不同的示例:

|uid|pid| first query                 | second query     |
| 1 | 5 | Yes, page.pid=5             | Yes. page.pid=5  |
| 5 | 6 | Yes, page.uid=5             | Yes. page.uid=5  |
| 6 | 7 | Yes, parentpage.pid=5       | No               |
| 7 | 8 | Yes, grapdppage.pid=5       | No               |
| 8 | 9 | Yes, grandgrandppage.pid=5  | No               |
| 9 | 10| No                          | No               |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM