{"id":60336,"date":"2024-02-13T02:00:18","date_gmt":"2024-02-12T18:00:18","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=60336"},"modified":"2024-02-13T02:00:18","modified_gmt":"2024-02-12T18:00:18","slug":"mysql-in-subquery","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-in-subquery.html","title":{"rendered":"\u8bb0\u8e29\u5230 MySQL in \u5b50\u67e5\u8be2\u7684\u201c\u5751\u201d"},"content":{"rendered":"
\u524d\u8a00<\/strong><\/span><\/div>\n

MySQL\u662f\u9879\u76ee\u4e2d\u5e38\u7528\u7684\u6570\u636e\u5e93\uff0c\u5176\u4e2din\u67e5\u8be2\u4e5f\u662f\u5f88\u5e38\u7528\u3002\u6700\u8fd1\u9879\u76ee\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u4e00\u4e2a\u51fa\u4e4e\u610f\u6599\u7684select\u67e5\u8be2\uff0c\u7adf\u7136\u7528\u4e8633\u79d2\uff01<\/p>\n

\u4e00\u3001\u8868\u7ed3\u6784<\/strong><\/span><\/div>\n

1. userinfo \u8868<\/strong><\/p>\n

\"\"<\/p>\n

2. article \u8868<\/strong><\/p>\n

\"\"<\/p>\n

select*fromuserinfowhereidin(selectauthor_idfromartilcewheretype=1);<\/pre>\n

\u5927\u5bb6\u7b2c\u4e00\u773c\u770b\u5230\u4e0a\u9762\u7684SQL\u65f6\uff0c\u53ef\u80fd\u90fd\u4f1a\u89c9\u5f97\u8fd9\u662f\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u5b50\u67e5\u8be2\u3002\u5148\u628aauthor_id\u67e5\u51fa\u6765\uff0c\u518d\u7528in\u67e5\u8be2\u4e00\u4e0b\u3002<\/p>\n

\u5982\u679c\u6709\u76f8\u5173\u7d22\u5f15\u4f1a\u975e\u5e38\u5feb\u7684\uff0c\u62c6\u89e3\u6765\u8bb2\u5c31\u662f\u4ee5\u4e0b\u8fd9\u6837\u7684\uff1a<\/p>\n

1.selectauthor_idfromartilcewheretype=1;\u3000\u30002.select*fromuserinfowhereidin(1,2,3);<\/pre>\n

\u4f46\u662f\u4e8b\u5b9e\u662f\u8fd9\u6837\u7684\uff1a<\/strong><\/p>\n

mysql> select count(*) from userinfo;<\/pre>\n

\"\"<\/p>\n

mysql> select count(*) from article;<\/pre>\n

\"\"<\/p>\n

mysql>\u00a0select id,username from userinfo where id in (select author_id from article where type = 1);<\/pre>\n

\"\"<\/p>\n

33 \u79d2<\/strong>\uff01<\/strong>\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u4e48\u6162\u5462\uff1f<\/strong><\/p>\n

\u4e09\u3001\u95ee\u9898\u539f\u56e0<\/strong><\/span><\/div>\n

\u5b98\u65b9\u6587\u6863\u89e3\u91ca\uff1ain \u5b50\u53e5\u5728\u67e5\u8be2\u7684\u65f6\u5019\u6709\u65f6\u4f1a\u88ab\u8f6c\u6362\u4e3a exists \u7684\u65b9\u5f0f\u6765\u6267\u884c\uff0c\u53d8\u6210\u9010\u6761\u8bb0\u5f55\u8fdb\u884c\u904d\u5386\uff08\u7248\u672c 5.5 \u4e2d\u5b58\u5728\uff0c5.6 \u4e2d\u5df2\u505a\u4f18\u5316\uff09\u3002<\/p>\n

\"\"<\/p>\n

\u53c2\u8003\uff1a<\/p>\n

https:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/subquery-optimization.html<\/a><\/p>\n

\u56db\u3001\u89e3\u51b3\u65b9\u5f0f\uff08\u7248\u672c5.5\uff09<\/strong><\/span><\/div>\n

1. \u4f7f\u7528\u4e34\u65f6\u8868<\/strong><\/p>\n

select id,username from userinfo\r\n\r\nwhere id in (select author_id from\r\n\r\n(select author_id from article where type = 1) as tb);<\/pre>\n

\"\"<\/p>\n

2. \u4f7f\u7528 join<\/strong><\/p>\n

select a.id,a.username from userinfo a, article b\r\n\r\nwhere a.id = b.author_id and b.type = 1;<\/pre>\n

\"\"<\/p>\n

\u4e94\u3001\u8865\u5145<\/strong><\/span><\/div>\n

\u7248\u672c 5.6 \u5df2\u9488\u5bf9\u5b50\u67e5\u8be2\u505a\u4e86\u4f18\u5316\uff0c\u65b9\u5f0f\u8ddf\u3010\u56db\u3011\u4e2d\u7684\u4e34\u65f6\u8868\u65b9\u5f0f\u4e00\u6837\uff0c\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1a<\/p>\n

If\u00a0materialization\u00a0is not used, the optimizer sometimes rewrites a noncorrelated subquery as a correlated subquery.<\/strong><\/p>\n

For example, the following IN subquery is noncorrelated \u00a0( where_condition involves only columns from t2 and not t1 ):<\/p>\n

select * from t1<\/p>\n

where t1.a in (select t2.b from t2 where where_condition);<\/p>\n

The optimizer\u00a0might rewrite this as an EXISTS correlated subquery<\/strong>:<\/p>\n

select * from t1<\/p>\n

where exists (select t2.b from t2 where where_condition and t1.a=t2.b);<\/p>\n

Subquery materialization\u00a0using a temporary table avoids such rewrites and makes it possible to execute the subquery only once rather than once per row of the outer query<\/strong>.<\/p>\n

https:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/subquery-materialization.html<\/a><\/p>\n

\u6587\u7ae0\u6765\u81ea\u5fae\u4fe1\u516c\u4f17\u53f7\uff1aHULK\u4e00\u7ebf\u6280\u672f\u6742\u8c08<\/p><\/blockquote>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.yunweipai.com\/archives\/13002.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/mysql-in-subquery.html<\/a>\u7f16\u8f91\u5458\uff1a\u534e\u4e16\u53d1\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u524d\u8a00MySQL\u662f\u9879\u76ee\u4e2d\u5e38\u7528\u7684\u6570\u636e\u5e93\uff0c\u5176\u4e2din\u67e5\u8be2\u4e5f\u662f\u5f88\u5e38\u7528\u3002\u6700\u8fd1\u9879\u76ee\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u4e00\u4e2a\u51fa\u4e4e\u610f\u6599\u7684select\u67e5\u8be2\uff0c\u7adf\u7136\u7528\u4e8633\u79d2\uff01<\/p>\n","protected":false},"author":63,"featured_media":88962,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-60336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/60336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=60336"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/60336\/revisions"}],"predecessor-version":[{"id":88998,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/60336\/revisions\/88998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/88962"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=60336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=60336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=60336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}