{"id":199431,"date":"2020-09-09T08:27:29","date_gmt":"2020-09-09T00:27:29","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=199431"},"modified":"2020-08-26T09:28:26","modified_gmt":"2020-08-26T01:28:26","slug":"teach-sqlite-select","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/teach-sqlite-select.html","title":{"rendered":"\u6559\u4f60\u4f7f\u7528SQLite-Select\u8bed\u53e5"},"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

\"\"<\/p>\n

\u8bed\u6cd5<\/strong><\/div>\n

SQLite \u7684 SELECT \u8bed\u53e5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\nSELECT column1, column2, columnN FROM table_name;<\/pre>\n

\u5728\u8fd9\u91cc\uff0ccolumn1, column2...\u662f\u8868\u7684\u5b57\u6bb5\uff0c\u4ed6\u4eec\u7684\u503c\u5373\u662f\u60a8\u8981\u83b7\u53d6\u7684\u3002\u5982\u679c\u60a8\u60f3\u83b7\u53d6\u6240\u6709\u53ef\u7528\u7684\u5b57\u6bb5\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u8bed\u6cd5\uff1a<\/p>\n

\r\nSELECT * FROM table_name;<\/pre>\n
\u5b9e\u4f8b<\/strong><\/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

\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4f7f\u7528 SELECT \u8bed\u53e5\u83b7\u53d6\u5e76\u663e\u793a\u6240\u6709\u8fd9\u4e9b\u8bb0\u5f55\u3002\u5728\u8fd9\u91cc\uff0c\u524d\u4e09\u4e2a\u547d\u4ee4\u88ab\u7528\u6765\u8bbe\u7f6e\u6b63\u786e\u683c\u5f0f\u5316\u7684\u8f93\u51fa\u3002<\/p>\n

\r\nsqlite>.header on\r\nsqlite>.mode column\r\nsqlite> SELECT * FROM COMPANY;<\/pre>\n

\u6700\u540e\uff0c\u5c06\u5f97\u5230\u4ee5\u4e0b\u7684\u7ed3\u679c\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

\u5982\u679c\u53ea\u60f3\u83b7\u53d6 COMPANY \u8868\u4e2d\u6307\u5b9a\u7684\u5b57\u6bb5\uff0c\u5219\u4f7f\u7528\u4e0b\u9762\u7684\u67e5\u8be2\uff1a<\/p>\n

\r\nsqlite> SELECT ID, NAME, SALARY FROM COMPANY;<\/pre>\n

\u4e0a\u9762\u7684\u67e5\u8be2\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n

\r\nID          NAME        SALARY\r\n----------  ----------  ----------\r\n1           Paul        20000.0\r\n2           Allen       15000.0\r\n3           Teddy       20000.0\r\n4           Mark        65000.0\r\n5           David       85000.0\r\n6           Kim         45000.0\r\n7           James       10000.0<\/pre>\n
\u8bbe\u7f6e\u8f93\u51fa\u5217\u7684\u5bbd\u5ea6<\/strong><\/div>\n

\u6709\u65f6\uff0c\u7531\u4e8e\u8981\u663e\u793a\u7684\u5217\u7684\u9ed8\u8ba4\u5bbd\u5ea6\u5bfc\u81f4 .mode column\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8f93\u51fa\u88ab\u622a\u65ad\u3002\u6b64\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 .width num, num.... \u547d\u4ee4\u8bbe\u7f6e\u663e\u793a\u5217\u7684\u5bbd\u5ea6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\nsqlite>.width 10, 20, 10\r\nsqlite>SELECT * FROM COMPANY;<\/pre>\n

\u4e0a\u9762\u7684 .width \u547d\u4ee4\u8bbe\u7f6e\u7b2c\u4e00\u5217\u7684\u5bbd\u5ea6\u4e3a 10\uff0c\u7b2c\u4e8c\u5217\u7684\u5bbd\u5ea6\u4e3a 20\uff0c\u7b2c\u4e09\u5217\u7684\u5bbd\u5ea6\u4e3a 10\u3002\u56e0\u6b64\u4e0a\u8ff0 SELECT \u8bed\u53e5\u5c06\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c\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
Schema \u4fe1\u606f<\/strong><\/div>\n

\u56e0\u4e3a\u6240\u6709\u7684\u70b9\u547d\u4ee4\u53ea\u5728 SQLite \u63d0\u793a\u7b26\u4e2d\u53ef\u7528\uff0c\u6240\u4ee5\u5f53\u60a8\u8fdb\u884c\u5e26\u6709 SQLite \u7684\u7f16\u7a0b\u65f6\uff0c\u60a8\u8981\u4f7f\u7528\u4e0b\u9762\u7684\u5e26\u6709 sqlite_master \u8868\u7684 SELECT \u8bed\u53e5\u6765\u5217\u51fa\u6240\u6709\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u7684\u8868\uff1a<\/p>\n

\r\nsqlite> SELECT tbl_name FROM sqlite_master WHERE type = 'table';<\/pre>\n

\u5047\u8bbe\u5728 testDB.db \u4e2d\u5df2\u7ecf\u5b58\u5728\u552f\u4e00\u7684 COMPANY \u8868\uff0c\u5219\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n

\r\ntbl_name\r\n----------\r\nCOMPANY<\/pre>\n

\u60a8\u53ef\u4ee5\u5217\u51fa\u5173\u4e8e COMPANY \u8868\u7684\u5b8c\u6574\u4fe1\u606f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\nsqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'COMPANY';<\/pre>\n

\u5047\u8bbe\u5728 testDB.db \u4e2d\u5df2\u7ecf\u5b58\u5728\u552f\u4e00\u7684 COMPANY \u8868\uff0c\u5219\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n

\r\nCREATE TABLE COMPANY(\r\n   ID INT PRIMARY KEY     NOT NULL,\r\n   NAME           TEXT    NOT NULL,\r\n   AGE            INT     NOT NULL,\r\n   ADDRESS        CHAR(50),\r\n   SALARY         REAL\r\n)<\/pre>\n","protected":false},"excerpt":{"rendered":"

SQLite \u7684 SELECT \u8bed\u53e5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT column1, column2, col […]<\/p>\n","protected":false},"author":1481,"featured_media":199408,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-199431","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\/199431","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=199431"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199431\/revisions"}],"predecessor-version":[{"id":199436,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199431\/revisions\/199436"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/199408"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=199431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=199431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=199431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}