{"id":201728,"date":"2020-10-04T08:48:42","date_gmt":"2020-10-04T00:48:42","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=201728"},"modified":"2020-09-24T11:49:20","modified_gmt":"2020-09-24T03:49:20","slug":"teach-sqlite-subquery","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/teach-sqlite-subquery.html","title":{"rendered":"\u6559\u4f60\u4f7f\u7528SQLite \u5b50\u67e5\u8be2"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSQLite \u662f\u4e00\u4e2a\u8f6f\u4ef6\u5e93\uff0c\u5b9e\u73b0\u4e86\u81ea\u7ed9\u81ea\u8db3\u7684\u3001\u65e0\u670d\u52a1\u5668\u7684\u3001\u96f6\u914d\u7f6e\u7684\u3001\u4e8b\u52a1\u6027\u7684 SQL \u6570\u636e\u5e93\u5f15\u64ce\u3002SQLite \u662f\u5728\u4e16\u754c\u4e0a\u6700\u5e7f\u6cdb\u90e8\u7f72\u7684 SQL \u6570\u636e\u5e93\u5f15\u64ce\u3002SQLite \u6e90\u4ee3\u7801\u4e0d\u53d7\u7248\u6743\u9650\u5236\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u5b50\u67e5\u8be2\u6216\u79f0\u4e3a\u5185\u90e8\u67e5\u8be2\u3001\u5d4c\u5957\u67e5\u8be2\uff0c\u6307\u7684\u662f\u5728 SQLite \u67e5\u8be2\u4e2d\u7684 WHERE \u5b50\u53e5\u4e2d\u5d4c\u5165\u67e5\u8be2\u8bed\u53e5\u3002
\n\u4e00\u4e2a SELECT \u8bed\u53e5\u7684\u67e5\u8be2\u7ed3\u679c\u80fd\u591f\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u8bed\u53e5\u7684\u8f93\u5165\u503c\u3002
\n\u5b50\u67e5\u8be2\u53ef\u4ee5\u4e0e SELECT\u3001INSERT\u3001UPDATE \u548c DELETE \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u53ef\u4f34\u968f\u7740\u4f7f\u7528\u8fd0\u7b97\u7b26\u5982 =\u3001IN\u3001BETWEEN \u7b49\u3002<\/p>\n

SELECT \u8bed\u53e5\u4e2d\u7684\u5b50\u67e5\u8be2\u4f7f\u7528<\/strong><\/div>\n

\u5b50\u67e5\u8be2\u901a\u5e38\u4e0e SELECT \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\nSELECT column_name [, column_name ]\r\nFROM   table1 [, table2 ]\r\nWHERE  column_name OPERATOR\r\n      (SELECT column_name [, column_name ]\r\n      FROM table1 [, table2 ]\r\n      [WHERE])<\/pre>\n
\u5b9e\u4f8b<\/strong><\/span><\/div>\n

\u5047\u8bbe COMPANY \u8868\u6709\u4ee5\u4e0b\u8bb0\u5f55\uff1a<\/p>\n

\r\nID          NAME        AGE         ADDRESS     SALARY\r\n----------  ----------  ----------  ----------  ----------\r\n1           Paul        32          California  20000.0\r\n2           Allen       25          Texas       15000.0\r\n3           Teddy       23          Norway      20000.0\r\n4           Mark        25          Rich-Mond   65000.0\r\n5           David       27          Texas       85000.0\r\n6           Kim         22          South-Hall  45000.0\r\n7           James       24          Houston     10000.0<\/pre>\n

\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u68c0\u67e5 SELECT \u8bed\u53e5\u4e2d\u7684\u5b50\u67e5\u8be2\u4f7f\u7528\uff1a<\/p>\n

\r\nsqlite> SELECT * \r\n     FROM COMPANY \r\n     WHERE ID IN (SELECT ID \r\n                  FROM COMPANY \r\n                  WHERE SALARY > 45000) ;<\/pre>\n

\u8fd9\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c:<\/p>\n

\r\nID          NAME        AGE         ADDRESS     SALARY\r\n----------  ----------  ----------  ----------  ----------\r\n4           Mark        25          Rich-Mond   65000.0\r\n5           David       27          Texas       85000.0 <\/pre>\n
INSERT \u8bed\u53e5\u4e2d\u7684\u5b50\u67e5\u8be2\u4f7f\u7528<\/strong><\/div>\n

\u5b50\u67e5\u8be2\u4e5f\u53ef\u4ee5\u4e0e INSERT \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002INSERT \u8bed\u53e5\u4f7f\u7528\u5b50\u67e5\u8be2\u8fd4\u56de\u7684\u6570\u636e\u63d2\u5165\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\u3002\u5728\u5b50\u67e5\u8be2\u4e2d\u6240\u9009\u62e9\u7684\u6570\u636e\u53ef\u4ee5\u7528\u4efb\u4f55\u5b57\u7b26\u3001\u65e5\u671f\u6216\u6570\u5b57\u51fd\u6570\u4fee\u6539\u3002<\/p>\n

\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\nINSERT INTO table_name [ (column1 [, column2 ]) ]\r\n           SELECT [ *|column1 [, column2 ]\r\n           FROM table1 [, table2 ]\r\n           [ WHERE VALUE OPERATOR ]<\/pre>\n
\u5b9e\u4f8b<\/strong><\/span><\/div>\n

\u5047\u8bbe COMPANY_BKP \u7684\u7ed3\u6784\u4e0e COMPANY \u8868\u76f8\u4f3c\uff0c\u4e14\u53ef\u4f7f\u7528\u76f8\u540c\u7684 CREATE TABLE \u8fdb\u884c\u521b\u5efa\uff0c\u53ea\u662f\u8868\u540d\u6539\u4e3a COMPANY_BKP\u3002\u73b0\u5728\u628a\u6574\u4e2a COMPANY \u8868\u590d\u5236\u5230 COMPANY_BKP\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\nsqlite> INSERT INTO COMPANY_BKP\r\n     SELECT * FROM COMPANY \r\n     WHERE ID IN (SELECT ID \r\n                  FROM COMPANY) ;<\/pre>\n
UPDATE \u8bed\u53e5\u4e2d\u7684\u5b50\u67e5\u8be2\u4f7f\u7528<\/strong><\/div>\n

\u5b50\u67e5\u8be2\u53ef\u4ee5\u4e0e UPDATE \u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\u3002\u5f53\u901a\u8fc7 UPDATE \u8bed\u53e5\u4f7f\u7528\u5b50\u67e5\u8be2\u65f6\uff0c\u8868\u4e2d\u5355\u4e2a\u6216\u591a\u4e2a\u5217\u88ab\u66f4\u65b0\u3002<\/p>\n

\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\nUPDATE table\r\nSET column_name = new_value\r\n[ WHERE OPERATOR [ VALUE ]\r\n   (SELECT COLUMN_NAME\r\n   FROM TABLE_NAME)\r\n   [ WHERE) ]<\/pre>\n
\u5b9e\u4f8b<\/strong><\/span><\/div>\n

\u5047\u8bbe\uff0c\u6211\u4eec\u6709 COMPANY_BKP \u8868\uff0c\u662f COMPANY \u8868\u7684\u5907\u4efd\u3002<\/p>\n

\u4e0b\u9762\u7684\u5b9e\u4f8b\u628a COMPANY \u8868\u4e2d\u6240\u6709 AGE \u5927\u4e8e\u6216\u7b49\u4e8e 27 \u7684\u5ba2\u6237\u7684 SALARY \u66f4\u65b0\u4e3a\u539f\u6765\u7684 0.50 \u500d\uff1a<\/p>\n

\r\nsqlite> UPDATE COMPANY\r\n     SET SALARY = SALARY * 0.50\r\n     WHERE AGE IN (SELECT AGE FROM COMPANY_BKP\r\n                   WHERE AGE >= 27 );<\/pre>\n

\u8fd9\u5c06\u5f71\u54cd\u4e24\u884c\uff0c\u6700\u540e COMPANY \u8868\u4e2d\u7684\u8bb0\u5f55\u5982\u4e0b\uff1a<\/p>\n

\r\nID          NAME        AGE         ADDRESS     SALARY\r\n----------  ----------  ----------  ----------  ----------\r\n1           Paul        32          California  10000.0\r\n2           Allen       25          Texas       15000.0\r\n3           Teddy       23          Norway      20000.0\r\n4           Mark        25          Rich-Mond   65000.0\r\n5           David       27          Texas       42500.0\r\n6           Kim         22          South-Hall  45000.0\r\n7           James       24          Houston     10000.0<\/pre>\n
DELETE \u8bed\u53e5\u4e2d\u7684\u5b50\u67e5\u8be2\u4f7f\u7528<\/strong><\/div>\n

\u5b50\u67e5\u8be2\u53ef\u4ee5\u4e0e DELETE \u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u5c31\u50cf\u4e0a\u9762\u63d0\u5230\u7684\u5176\u4ed6\u8bed\u53e5\u4e00\u6837\u3002<\/p>\n

\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\nDELETE FROM TABLE_NAME\r\n[ WHERE OPERATOR [ VALUE ]\r\n   (SELECT COLUMN_NAME\r\n   FROM TABLE_NAME)\r\n   [ WHERE) ]<\/pre>\n
\u5b9e\u4f8b<\/strong><\/span><\/div>\n

\u5047\u8bbe\uff0c\u6211\u4eec\u6709 COMPANY_BKP \u8868\uff0c\u662f COMPANY \u8868\u7684\u5907\u4efd\u3002<\/p>\n

\u4e0b\u9762\u7684\u5b9e\u4f8b\u5220\u9664 COMPANY \u8868\u4e2d\u6240\u6709 AGE \u5927\u4e8e\u6216\u7b49\u4e8e 27 \u7684\u5ba2\u6237\u8bb0\u5f55\uff1a<\/p>\n

\r\nsqlite> DELETE FROM COMPANY\r\n     WHERE AGE IN (SELECT AGE FROM COMPANY_BKP\r\n                   WHERE AGE > 27 );<\/pre>\n

\u8fd9\u5c06\u5f71\u54cd\u4e24\u884c\uff0c\u6700\u540e COMPANY \u8868\u4e2d\u7684\u8bb0\u5f55\u5982\u4e0b\uff1a<\/p>\n

\r\nID          NAME        AGE         ADDRESS     SALARY\r\n----------  ----------  ----------  ----------  ----------\r\n2           Allen       25          Texas       15000.0\r\n3           Teddy       23          Norway      20000.0\r\n4           Mark        25          Rich-Mond   65000.0\r\n5           David       27          Texas       42500.0\r\n6           Kim         22          South-Hall  45000.0\r\n7           James       24          Houston     10000.0<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5b50\u67e5\u8be2\u6216\u79f0\u4e3a\u5185\u90e8\u67e5\u8be2\u3001\u5d4c\u5957\u67e5\u8be2\uff0c\u6307\u7684\u662f\u5728 SQLite \u67e5\u8be2\u4e2d\u7684 WHERE \u5b50\u53e5\u4e2d\u5d4c\u5165\u67e5\u8be2\u8bed\u53e5\u3002 \u4e00\u4e2a SE […]<\/p>\n","protected":false},"author":1481,"featured_media":201702,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-201728","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\/201728","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\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=201728"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201728\/revisions"}],"predecessor-version":[{"id":201739,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201728\/revisions\/201739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/201702"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=201728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=201728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=201728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}