{"id":237540,"date":"2022-03-23T09:29:45","date_gmt":"2022-03-23T01:29:45","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=237540"},"modified":"2022-03-11T11:30:20","modified_gmt":"2022-03-11T03:30:20","slug":"mysql-multi-table-query","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-multi-table-query.html","title":{"rendered":"\u8ba9\u4eba\u53c8\u7231\u53c8\u6068\u7684Mysql\u591a\u8868\u67e5\u8be2"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728SQL\u5f00\u53d1\u5f53\u4e2d\uff0c\u591a\u8868\u8054\u67e5\u662f\u7edd\u5bf9\u7ed5\u4e0d\u5f00\u7684\u4e00\u79cd\u6280\u80fd\u3002\u540c\u6837\u7684\u67e5\u8be2\u7ed3\u679c\u4e0d\u540c\u7684\u5199\u6cd5\u5176\u8fd0\u884c\u6548\u7387\u4e5f\u662f\u5343\u5dee\u4e07\u522b\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u5f53\u4e2d\uff0c\u6211\u89c1\u8fc7(\u597d\u50cf\u8fd8\u5199\u8fc7~)\u4e0d\u5c11\u53c8\u957f\u53c8\u81ed\u7684\u67e5\u8be2SQL\uff0c\u6570\u636e\u91cf\u4e00\u4e0a\u6765\u67e5\u4e2a\u5341\u51e0\u5206\u949f\u90a3\u662f\u5bb6\u5e38\u4fbf\u996d\u3002\u56e0\u6b64\uff0c\u6df1\u5165\u7406\u89e3SQL\u7684\u591a\u8868\u67e5\u8be2\u673a\u5236\uff0c\u5c11\u5199\u4e00\u4e9b\u6162\u67e5\u8be2\uff0c\u5e94\u8be5\u53ef\u4ee5\u5c11\u6328\u70b9\u9a82\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1. \u524d\u8a00<\/strong><\/div>\n

\u5728SQL\u5f00\u53d1\u5f53\u4e2d\uff0c\u591a\u8868\u8054\u67e5\u662f\u7edd\u5bf9\u7ed5\u4e0d\u5f00\u7684\u4e00\u79cd\u6280\u80fd\u3002\u540c\u6837\u7684\u67e5\u8be2\u7ed3\u679c\u4e0d\u540c\u7684\u5199\u6cd5\u5176\u8fd0\u884c\u6548\u7387\u4e5f\u662f\u5343\u5dee\u4e07\u522b\u3002<\/p>\n

\u5728\u5b9e\u9645\u5f00\u53d1\u5f53\u4e2d\uff0c\u6211\u89c1\u8fc7(\u597d\u50cf\u8fd8\u5199\u8fc7~)\u4e0d\u5c11\u53c8\u957f\u53c8\u81ed\u7684\u67e5\u8be2SQL\uff0c\u6570\u636e\u91cf\u4e00\u4e0a\u6765\u67e5\u4e2a\u5341\u51e0\u5206\u949f\u90a3\u662f\u5bb6\u5e38\u4fbf\u996d\u3002<\/p>\n

\u56e0\u6b64\uff0c\u6df1\u5165\u7406\u89e3SQL\u7684\u591a\u8868\u67e5\u8be2\u673a\u5236\uff0c\u5c11\u5199\u4e00\u4e9b\u6162\u67e5\u8be2\uff0c\u5e94\u8be5\u53ef\u4ee5\u5c11\u6328\u70b9\u9a82\u3002<\/p>\n

\"\"<\/p>\n

2. \u7b49\u503c\u8fde\u63a5\u548c\u975e\u7b49\u503c\u8fde\u63a5<\/strong><\/div>\n
2.1 \u7b49\u503c\u8fde\u63a5<\/strong><\/span><\/div>\n

\u7b49\u503c\u8fde\u63a5\u662f\u5728\u591a\u8868\u67e5\u8be2\u4e2d\u6700\u57fa\u7840\uff0c\u4e5f\u6700\u7b80\u5355\u7684\u4e00\u79cd\uff0c\u5176\u503c\u4e3a\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u7b1b\u5361\u5c14\u79ef\u3002<\/p>\n

\u5728from\u540e\u9762\uff0c\u54ea\u4e2a\u8868\u5199\u5728\u524d\u9762\u7ed3\u679c\u4e2d\u54ea\u4e2a\u8868\u7684\u503c\u5c31\u5148\u51fa\u73b0\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\nselect *\r\nfrom student,\r\nfamily\r\nwhere student.family_id = family.id;<\/pre>\n

\"\"<\/p>\n

\u963f\u91cc\u5728\u6700\u65b0\u53d1\u5e03\u7684Java\u5f00\u53d1\u624b\u518c\u4e2d\u5f3a\u5236\u8981\u6c42\uff0c\u53ea\u8981\u6d89\u53ca\u591a\u4e2a\u8868\uff0c\u5fc5\u987b\u5728\u5217\u540d\u524d\u52a0\u8868\u7684\u522b\u540d(\u6216\u8868\u540d)\u8fdb\u884c\u9650\u5b9a<\/p>\n

2.2 \u975e\u7b49\u503c\u8fde\u63a5<\/strong><\/span><\/div>\n

\u975e\u7b49\u503c\u8fde\u63a5\u662f\u901a\u8fc7a\u8868\u4e2d\u7684\u503c\u5728b\u8868\u4e2d\u7684\u67d0\u4e00\u4e2a\u8303\u56f4\u6765\u8fdb\u884c\u7684\uff0c\u80fd\u591f\u5f88\u597d\u7684\u6ee1\u8db3\u9884\u8bbe\u5b9a\u597d\u7684\u5206\u6bb5\u7edf\u8ba1\u9700\u6c42\u3002<\/p>\n

\u975e\u7b49\u503c\u8fde\u63a5\u6709\u4e24\u79cd\u5199\u6cd5\uff0c\u4f7f\u7528between...and...\u6216\u5927\u4e8e\u53f7\u5c0f\u4e8e\u53f7<\/p>\n

\r\n-- \u7b2c\u4e00\u79cd\u5199\u6cd5\uff1a\u4f7f\u7528between...and...\r\nselect a.discipline_name, a.score, b.grade_tag\r\nfrom achievement a,\r\nachievement_grade b\r\nwhere a.score between b.lowest_score and b.highest_score;\r\n-- \u7b2c\u4e8c\u79cd\u5199\u6cd5\uff0c\u4f7f\u7528>=\u6216<=\r\nselect a.discipline_name, a.score, b.grade_tag\r\nfrom achievement a,\r\nachievement_grade b\r\nwhere a.score >= b.lowest_score\r\nand a.score <= b.highest_score;<\/pre>\n

\"\"<\/p>\n

3. \u81ea\u8fde\u63a5\u548c\u975e\u81ea\u8fde\u63a5<\/strong><\/div>\n
3.1 \u81ea\u8fde\u63a5<\/strong><\/span><\/div>\n

\u81ea\u8fde\u63a5\uff0c\u987e\u540d\u601d\u4e49\u5c31\u662f\u540c\u4e00\u5f20\u8868\u81ea\u5df1\u8ddf\u81ea\u5df1\u8fde\u63a5\uff0c\u4e3a\u4e86\u533a\u5206\u9700\u8981\u7ed9\u8868\u53d6\u4e0d\u540c\u7684\u522b\u540d\u3002\u5982\u4e00\u5f20\u6210\u7ee9\u8868\uff0c\u9700\u8981\u67e5\u8be2\u6240\u6709\u5206\u6570\u6bd4\u201c\u8bed\u6587\u201d\u9ad8\u7684\u6570\u636e\uff1a<\/p>\n

\"\"<\/p>\n

\u82e5\u4e0d\u4f7f\u7528\u81ea\u8fde\u63a5\uff0c\u9700\u8981\u5148\u901a\u8fc7\u67e5\u8be2\u8bed\u6587\u7684\u5206\u6570\uff0c\u7136\u540e\u518d\u67e5\u8be2\u5927\u4e8e\u8fd9\u4e2a\u5206\u6570\u7684\u6570\u636e\u3002<\/p>\n

\u5177\u4f53\u53ef\u4ee5\u6309\u5982\u4e0b\u6b65\u9aa4\u8fdb\u884c\u67e5\u8be2:<\/p>\n

\r\n-- \u5148\u67e5\u8be2\u8bed\u6587\u7684\u5206\u6570\r\nselect score from achievement where discipline_name = '\u8bed\u6587';\r\n-- \u518d\u67e5\u8be2\u5206\u6570\u6bd4\u8bed\u6587\u5206\u6570\u66f4\u9ad8\u7684\u6570\u636e\r\nselect * from achievement where score > 76;<\/pre>\n

\u800c\u4f7f\u7528\u81ea\u8fde\u63a5\uff0c\u5219\u53ef\u4ee5\u5728\u4e00\u6761sq\u8bed\u53e5\u91cc\u5b8c\u6210\u67e5\u8be2\uff1a<\/p>\n

\r\nselect a.*\r\nfrom achievement a,\r\nachievement b\r\nwhere b.discipline_name = '\u8bed\u6587'\r\nand a.score > b.score;\r\n<\/pre>\n

\"\"<\/p>\n

3.2 \u975e\u81ea\u8fde\u63a5<\/strong><\/span><\/div>\n

\u9664\u81ea\u8fde\u63a5\u5916\uff0c\u5176\u4ed6\u7684\u90fd\u53eb\u975e\u81ea\u8fde\u63a5~~~<\/p>\n

4. \u5185\u8fde\u63a5\u548c\u5916\u8fde\u63a5<\/strong><\/div>\n

\u5185\u8fde\u63a5\u548c\u5916\u8fde\u63a5\u7684\u533a\u5206\u672c\u8d28\u4e0a\u662f\u53e6\u4e00\u79cd\u5206\u7c7b\u65b9\u6cd5\uff0c\u5982\u5185\u8fde\u63a5\u5c31\u662f\u7b49\u503c\u8fde\u63a5\u3002<\/p>\n

    \n
  • \u5185\u8fde\u63a5\uff1a\u5408\u5e76\u5177\u6709\u540c\u4e00\u5217\u7684\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u8868\u7684\u884c, \u7ed3\u679c\u96c6\u4e2d\u4e0d\u5305\u542b\u4e00\u4e2a\u8868\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e0d\u5339\u914d\u7684\u884c<\/li>\n
  • \u5916\u8fde\u63a5\uff1a\u4e24\u4e2a\u8868\u5728\u8fde\u63a5\u8fc7\u7a0b\u4e2d\u9664\u4e86\u8fd4\u56de\u6ee1\u8db3\u8fde\u63a5\u6761\u4ef6\u7684\u884c\u4ee5\u5916\u8fd8\u8fd4\u56de\u5de6(\u6216\u53f3)\u8868\u4e2d\u4e0d\u6ee1\u8db3\u6761\u4ef6\u7684<\/li>\n
  • \u884c \uff0c\u8fd9\u79cd\u8fde\u63a5\u79f0\u4e3a\u5de6(\u6216\u53f3) \u5916\u8fde\u63a5\u3002\u6ca1\u6709\u5339\u914d\u7684\u884c\u65f6, \u7ed3\u679c\u8868\u4e2d\u76f8\u5e94\u7684\u5217\u4e3a\u7a7a(NULL)\u3002<\/li>\n
  • \u5de6\u5916\u8fde\u63a5\uff1a\u8fde\u63a5\u6761\u4ef6\u4e2d\u5de6\u8fb9\u7684\u8868\u4e5f\u79f0\u4e3a\u4e3b\u8868 \uff0c\u53f3\u8fb9\u7684\u8868\u79f0\u4e3a\u4ece\u8868 \u3002<\/li>\n
  • \u53f3\u5916\u8fde\u63a5\uff1a\u8fde\u63a5\u6761\u4ef6\u4e2d\u53f3\u8fb9\u7684\u8868\u4e5f\u79f0\u4e3a\u4e3b\u8868 \uff0c\u5de6\u8fb9\u7684\u8868\u79f0\u4e3a\u4ece\u8868 \u3002<\/li>\n
  • \u5168\u5916\u8fde\u63a5<\/li>\n<\/ul>\n
    4.1 \u6d4b\u8bd5\u6570\u636e<\/strong><\/span><\/div>\n

    \u6d4b\u8bd5\u7528\u5b66\u751f\u8868student\u548c\u5bb6\u5ead\u8868family\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n

    \"\"<\/p>\n

    \"\"<\/p>\n

    4.2 \u5de6\u5916\u8fde\u63a5<\/strong><\/span><\/div>\n
    \r\n-- \u67e5\u51fastudent\u4e2d\u7684\u6240\u6709\u6570\u636e\uff0c\u4e0d\u6ee1\u8db3\u7684\u663e\u793a\u4e3anull\r\n-- \u8fd9\u91ccstudent\u5728\u524d\u9762\r\nselect a.*\r\nfrom student a\r\nleft join family b on a.family_id = b.id<\/pre>\n

    \"\"<\/p>\n

    4.3 \u53f3\u5916\u8fde\u63a5<\/strong><\/span><\/div>\n
    \r\n-- \u67e5\u51fastudent\u4e2d\u7684\u6240\u6709\u6570\u636e\uff0c\u4e0d\u6ee1\u8db3\u7684\u663e\u793a\u4e3anull\r\n-- \u8fd9\u91ccstudent\u5728\u540e\u9762\r\nselect a.*\r\nfrom family b\r\nright join student a on b.id = a.family_id;<\/pre>\n

    \"\"<\/p>\n

    4.4 \u5168\u5916\u8fde\u63a5<\/strong><\/span><\/div>\n

    \u5f88\u9057\u61be\uff0cMySQL\u4e0d\u652f\u6301\u5168\u5916\u8fde\u63a5\u3002<\/p>\n

    \u9644\u5f55\uff1a\u6d4b\u8bd5\u6570\u636eSQL\u811a\u672c<\/p>\n

    \r\n-- auto-generated definition\r\ncreate table student\r\n(\r\n    id           int auto_increment\r\n        primary key,\r\n    student_id   int                                null comment '\u5b66\u53f7',\r\n    student_name varchar(40)                        null comment '\u59d3\u540d',\r\n    family_id    int                                null comment '\u5bb6\u5eadID',\r\n    create_time  datetime default CURRENT_TIMESTAMP null comment '\u521b\u5efa\u65f6\u95f4'\r\n)\r\n    comment '\u5b66\u751f\u8868';\r\n\r\ncreate table family\r\n(\r\n    id             int auto_increment\r\n        primary key,\r\n    family_name    varchar(40)                        null comment '\u5bb6\u5ead\u540d\u79f0',\r\n    family_address varchar(40)                        null comment '\u5bb6\u5ead\u5730\u5740',\r\n    create_time    datetime default CURRENT_TIMESTAMP null comment '\u521b\u5efa\u65f6\u95f4'\r\n)\r\n    comment '\u5bb6\u5ead\u8868';\r\n\r\ncreate table achievement\r\n(\r\n    id              int auto_increment\r\n        primary key,\r\n    score           int         null comment '\u5206\u6570',\r\n    discipline_name varchar(40) null comment '\u5b66\u79d1\u540d\u79f0',\r\n    student_id      int         null comment '\u5b66\u53f7'\r\n)\r\n    comment '\u6210\u7ee9\u8868';\r\n\r\ncreate table achievement_grade\r\n(\r\n    id            int auto_increment\r\n        primary key,\r\n    grade_tag     varchar(10)                        null comment '\u6863\u6b21',\r\n    lowest_score  int                                null comment '\u6700\u4f4e\u5206',\r\n    highest_score int                                null comment '\u6700\u9ad8\u5206',\r\n    create_time   datetime default CURRENT_TIMESTAMP null comment '\u521b\u5efa\u65f6\u95f4'\r\n)\r\n    comment '\u5206\u6570\u6863\u6b21\u8868';\r\n\r\nINSERT INTO achievement_grade (id, grade_tag, lowest_score, highest_score, create_time) VALUES (1, '\u4e0d\u53ca\u683c', 0, 60, '2022-03-02 11:44:01');\r\nINSERT INTO achievement_grade (id, grade_tag, lowest_score, highest_score, create_time) VALUES (2, '\u826f\u597d', 60, 80, '2022-03-02 11:44:01');\r\nINSERT INTO achievement_grade (id, grade_tag, lowest_score, highest_score, create_time) VALUES (3, '\u4f18\u79c0', 80, 100, '2022-03-02 11:44:01');\r\n\r\nINSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (1, 1, '\u5f20\u4e09', 1, '2022-03-02 09:55:01');\r\nINSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (2, 2, '\u674e\u56db', 2, '2022-03-02 09:55:01');\r\nINSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (3, 3, '\u738b\u4e94', 3, '2022-03-02 09:55:01');\r\nINSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (4, 4, '\u9ad8\u98de', null, '2022-03-02 19:45:14');\r\n\r\nINSERT INTO family (id, family_name, family_address, create_time) VALUES (1, '\u5f20\u4e09\u5bb6', '\u5317\u4eac', '2022-03-02 09:54:13');\r\nINSERT INTO family (id, family_name, family_address, create_time) VALUES (2, '\u674e\u56db\u5bb6', '\u4e0a\u6d77', '2022-03-02 09:54:13');\r\nINSERT INTO family (id, family_name, family_address, create_time) VALUES (3, '\u738b\u4e94\u5bb6', '\u897f\u4f2f\u5229\u4e9a', '2022-03-02 09:54:13');\r\n\r\nINSERT INTO achievement (id, score, discipline_name, student_id) VALUES (1, 76, '\u8bed\u6587', 1);\r\nINSERT INTO achievement (id, score, discipline_name, student_id) VALUES (2, 80, '\u6570\u5b66', 1);\r\nINSERT INTO achievement (id, score, discipline_name, student_id) VALUES (3, 65, '\u82f1\u8bed', 1);\r\nINSERT INTO achievement (id, score, discipline_name, student_id) VALUES (4, 98, '\u5730\u7406', 1);\r\nINSERT INTO achievement (id, score, discipline_name, student_id) VALUES (5, 77, '\u5386\u53f2', 1);\r\nINSERT INTO achievement (id, score, discipline_name, student_id) VALUES (6, 69, '\u751f\u7269', 1);<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u5728SQL\u5f00\u53d1\u5f53\u4e2d\uff0c\u591a\u8868\u8054\u67e5\u662f\u7edd\u5bf9\u7ed5\u4e0d\u5f00\u7684\u4e00\u79cd\u6280\u80fd\u3002\u540c\u6837\u7684\u67e5\u8be2\u7ed3\u679c\u4e0d\u540c\u7684\u5199\u6cd5\u5176\u8fd0\u884c\u6548\u7387\u4e5f\u662f\u5343\u5dee\u4e07\u522b\u3002 \u5728\u5b9e\u9645\u5f00\u53d1 […]<\/p>\n","protected":false},"author":317,"featured_media":208759,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-237540","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\/237540","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\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=237540"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237540\/revisions"}],"predecessor-version":[{"id":237550,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237540\/revisions\/237550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208759"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=237540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=237540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=237540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}