{"id":125016,"date":"2018-11-01T09:23:57","date_gmt":"2018-11-01T01:23:57","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=125016"},"modified":"2018-10-23T09:24:23","modified_gmt":"2018-10-23T01:24:23","slug":"mariadb-study-view","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mariadb-study-view.html","title":{"rendered":"Mariadb\u5b66\u4e60\u603b\u7ed3\uff08\u5341\u4e00\uff09\uff1a\u89c6\u56feVIEW"},"content":{"rendered":"
\u9996\u5148\uff0c\u4e00\u4e2a\u95ee\u9898\uff0c\u89c6\u56fe\uff08VIEW\uff09\u662f\u4ec0\u4e48\uff1f \u6709\u5173\u4f8b\u5b50\uff0c\u6211\u8fd9\u91cc\u60f3\u7528Mariadb\u7684KB\u5e93\u4e2d\u7684\u4e00\u7bc7\u6587\u7ae0\uff0cLet's GO\uff01<\/p>\n \u521b\u5efa\u96c7\u5458\uff08Employees\uff09\u8868\uff1a<\/p>\n \u5411\u96c7\u5458\u8868\u4e2d\u63d2\u5165\u6d4b\u8bd5\u6570\u636e\uff1a<\/p>\n \u521b\u5efa\u7b7e\u5230\uff08Hours\uff09\u8868\uff0c\u8bb0\u5f55\u4e86\u6240\u6709\u96c7\u5458\u7684\u4e0a\u73ed\u65f6\u95f4\u548c\u4e0b\u73ed\u65f6\u95f4\u7684\u4fe1\u606f\uff1a<\/p>\n \u63d2\u5165\u6d4b\u8bd5\u8bb0\u5f55\uff1a<\/p>\n \u73b0\u5728\uff0c\u6211\u4eec\u8981\u8003\u5bdf\u8fd9\u4e00\u5468\u54ea\u4f4d\u96c7\u5458\u8fdf\u5230\u4e86\uff08\u8d85\u8fc77:00:59\uff09\uff0c\u53ef\u4ee5\u5199\u51fa\u5982\u4e0bSQL\u8bed\u53e5\uff1a<\/p>\n \u4f46\u662f\uff0c\u516c\u53f8\u6709\u89c4\u5b9a\uff0c\u8fdf\u5230\u7684\u4eba\u5728\u4e0b\u73ed\u65f6\u628a\u8fdf\u5230\u7684\u65f6\u95f4\u8865\u51fa\u6765\u5c31\u53ef\u4ee5\u65e2\u5f80\u4e0d\u548e\u3002<\/p>\n \u4ee5\u4e0b\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570TIMESTAMPDIFF\uff0c\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u65f6\u95f4\u7684\u5dee\u503c\u3002<\/p>\n \u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n \u6240\u4ee5\uff0c\u6211\u4eec\u8981\u67e5\u8be2\u8fdf\u5230\u7684\u5458\u5de5\uff0c\u5373\u4e0b\u73ed\u6253\u5f00\u65f6\u95f4-\u4e0a\u73ed\u6253\u5f00\u65f6\u95f4\uff1c600\u5206\u949f\u7684\u4eba\uff0c\u4fbf\u53ef\u4ee5\u8ba4\u4e3a\u662f\u8fdf\u5230\u7684\u5458\u5de5\uff0c\u53ef\u5199\u51fa\u4e0b\u5217SQL\u8bed\u53e5\uff1a<\/p>\n \u8fd9\u4e2a\u67e5\u8be2\u6211\u4eec\u53ef\u80fd\u4f1a\u7ecf\u5e38\u7528\u5230\uff0c\u6bd5\u7adf\u8981\u77e5\u9053\u8c01\u8fdf\u5230\u4e86\u4e0d\u662f\uff1f\u4f46\u662f\u67e5\u8be2\u53c8\u6bd4\u8f83\u590d\u6742\u96be\u5199\uff0c\u8fd9\u65f6\u5019\uff0c\u89c6\u56fe\u5c31\u53ef\u4ee5\u5e2e\u52a9\u5230\u6211\u4eec\u4e86\u3002<\/p>\n \u6211\u4eec\u53ef\u4ee5\u5c06\u6b64\u67e5\u8be2\u521b\u5efa\u4e3a\u4e00\u4e2a\u89c6\u56fe\uff0c\u5f53\u5bf9\u89c6\u56fe\u8fdb\u884c\u67e5\u8be2\u65f6\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u67e5\u8be2\u4e86\u8fd9\u4e2a\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u521b\u5efa\u89c6\u56fe\u5982\u4e0b\uff1a<\/p>\n \u73b0\u5728\uff0c\u6211\u4eec\u6709\u4e86\u89c6\u56fe\uff0c\u67e5\u8be2\u4e00\u4e0b\u8bd5\u8bd5~<\/p>\n \u4ee5\u4e0a\uff0c\u4fbf\u662f\u89c6\u56fe\u7684\u4e00\u4e2a\u76f4\u89c2\u4f8b\u5b50\uff0c\u6765\u81eaMariaDB\u7684KB\u5e93\u3002<\/p>\n \u5f53\u89c6\u56fe\u5df2\u7ecf\u5b58\u5728\u65f6\uff0cCREATE OR REPLACE\u76f8\u5f53\u4e8eALTER VIEW<\/strong>\uff0c\u89c6\u56fe\u521b\u5efa\u65f6\u4f1a\u5c06*\u53d8\u4e3a\u5177\u4f53\u7684\u5217\u5f15\u7528<\/strong>\uff0c\u6240\u4ee5\u5bf9BASE\u8868\uff08\u88ab\u89c6\u56fe\u5f15\u7528\u7684\u8868\uff09\u589e\u52a0\u65b0\u7684\u5217\u65f6\uff0c\u5728\u89c6\u56fe\u4e2d\u4e0d\u4f1a\u663e\u793a\uff0c\u8bf7\u770b\u5982\u4e0b\u4f8b\u5b50\uff1a<\/p>\n [DEFINER = { user | CURRENT_USER | role | CURRENT_ROLE }] \u6765\u5b9a\u4e49\u8be5\u89c6\u56fe\u7684\u5b9a\u4e49\u8005\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d\u7528\u6237<\/p>\n [SQL SECURITY { DEFINER | INVOKER }] \u5b9a\u4e49\u6743\u9650\u68c0\u67e5\uff0c\u9ed8\u8ba4\u4e3aDEFINER\uff0c\u8fd9\u91cc\u6bd4\u8f83\u6709\u610f\u601d\uff0c\u6240\u4ee5\u9700\u8981\u89e3\u91ca\u4e00\u4e0b\uff1a<\/p>\n \u5047\u8bbe\u6211\u4eec\u7684\u89c6\u56fe\u8fd9\u6837select_statement\u4e3aSELECT * FROM city;\u4e14\u6709\u65b0\u7528\u6237test@'%'\u4ec5\u5bf9\u8be5\u89c6\u56fe\u6709SELECT\u6743\u9650 AS select_statement \u4e2d\u7684select_statement\u5c31\u662f\u89c6\u56fe\u7684\u5b9a\u4e49\u4e86\uff0cselect_statement\u53ef\u4ee5\u662f\u4ece\u5176\u4ed6\u89c6\u56fe\u4e2d\u68c0\u7d22\u6570\u636e\u3002<\/p>\n select_statement\u6709\u5982\u4e0b\u9650\u5236\uff1a<\/p>\n [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]->\u9009\u62e9Mysql\u5bf9\u89c6\u56fe\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u4e3aUNDEFINED\uff0c\u8bf7\u770b\u4e0b\u5217\u8be6\u89e3\uff1a<\/p>\n UNDEFINED<\/strong>\uff1a\u7531Mysql\u81ea\u52a8\u9009\u62e9\u5904\u7406\u529e\u6cd5\uff0c\u66f4\u503e\u5411\u4e8eMERGE\u3002<\/p>\n MERGE<\/strong>\uff1a\u5c06\u5bf9\u89c6\u56fe\u7684\u68c0\u7d22\u5b50\u53e5\u548c\u5b9a\u4e49\u4e2d\u7684\u5b50\u53e5\u5408\u5e76\u540e\u67e5\u8be2\uff0c\u4f8b\u5982\uff1a<\/p>\n \u4e5f\u5c31\u662fSELECT\u7684\u5b50\u53e5\u4f1a\u76f4\u63a5\u62fc\u63a5\uff08\u5408\u5e76\uff09\u5230\u89c6\u56fe\u5b9a\u4e49\u4e2d\u7684SELECT\u8bed\u53e5\u4e2d\u53bb\u3002<\/p>\n \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u4f7f\u7528\u4ee5\u4e0b\u5b50\u53e5\u5b9a\u4e49\u89c6\u56fe\u65f6\uff0c\u89c6\u56fe\u65e0\u6cd5\u4f7f\u7528MERGE\uff1a<\/p>\n TEMPTABLE<\/strong>\uff1a\u6267\u884c\u89c6\u56fe\u4e2d\u5b9a\u4e49\u7684SQL\u8bed\u53e5\uff0c\u5e76\u5c06\u7ed3\u679c\u96c6\u5b58\u5165\u4e34\u65f6\u8868\uff0c\u800c\u4ece\u89c6\u56fe\u68c0\u7d22\u65f6\u5b9a\u4e49\u7684\u5b50\u53e5\u4f1a\u88ab\u4f5c\u7528\u4e8e\u8fd9\u5f20\u4e34\u65f6\u8868\uff0c\u6240\u4ee5\u6027\u80fd\u4f1a\u6709\u6240\u635f\u8017\u3002<\/p>\n \u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n \u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n \u8ddfCREATE VIEW\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5c31\u4e0d\u8fc7\u591a\u89e3\u91ca\u4e86\u3002<\/p>\n \u8bed\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u5982\u4e0b\uff1a<\/p>\n \u89c6\u56feVIEW \u9996\u5148\uff0c\u4e00\u4e2a\u95ee\u9898\uff0c\u89c6\u56fe\uff08VIEW\uff09\u662f\u4ec0\u4e48\uff1f \u89c6\u56fe\u662f\u865a\u62df\u7684\u8868\uff0c\u4e0e\u5305\u542b\u6570\u636e\u7684\u8868\u4e0d\u4e00\u6837\uff0c\u89c6\u56fe\u53ea\u5305\u542b\u4f7f\u7528\u65f6 […]<\/p>\n","protected":false},"author":317,"featured_media":4820,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-125016","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\/125016","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=125016"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125016\/revisions"}],"predecessor-version":[{"id":125180,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125016\/revisions\/125180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/4820"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=125016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=125016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=125016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u89c6\u56fe\u662f\u865a\u62df\u7684\u8868\uff0c\u4e0e\u5305\u542b\u6570\u636e\u7684\u8868\u4e0d\u4e00\u6837\uff0c\u89c6\u56fe\u53ea\u5305\u542b\u4f7f\u7528\u65f6\u52a8\u6001\u68c0\u7d22\u6570\u636e\u7684\u67e5\u8be2<\/strong>\uff0c\u7b80\u5355\u800c\u8a00\uff0c\u89c6\u56fe\u662f\u590d\u6742SELECT\u7684\u62bd\u8c61\uff0c\u5c06\u590d\u6742SELECT\u8bed\u53e5\u62bd\u8c61\u6210\u4e00\u5f20\u865a\u62df\u7684\u8868\uff0c\u5f53\u5bf9\u8fd9\u4e2a\u8868\u6267\u884c\u7b80\u5355\u67e5\u8be2\u65f6\uff0c\u5b9e\u8d28\u4e0a\u5c31\u662f\u5bf9\u590d\u6742SELECT\u8bed\u53e5\u7684\u6267\u884c\u3002<\/p>\n\r\nCREATE TABLE `Employees` (\r\n `ID` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,\r\n `First_Name` VARCHAR(25) NOT NULL,\r\n `Last_Name` VARCHAR(25) NOT NULL,\r\n `Position` VARCHAR(25) NOT NULL,\r\n `Home_Address` VARCHAR(50) NOT NULL,\r\n `Home_Phone` VARCHAR(12) NOT NULL,\r\n PRIMARY KEY (`ID`)\r\n) ENGINE=MyISAM;\r\n<\/pre>\n
\r\nINSERT INTO `Employees` (`First_Name`, `Last_Name`, `Position`, `Home_Address`, `Home_Phone`)\r\nVALUES\r\n ('Mustapha', 'Mond', 'Chief Executive Officer', '692 Promiscuous Plaza', '326-555-3492'),\r\n ('Henry', 'Foster', 'Store Manager', '314 Savage Circle', '326-555-3847'),\r\n ('Bernard', 'Marx', 'Cashier', '1240 Ambient Avenue', '326-555-8456'),\r\n ('Lenina', 'Crowne', 'Cashier', '281 Bumblepuppy Boulevard', '328-555-2349'),\r\n ('Fanny', 'Crowne', 'Restocker', '1023 Bokanovsky Lane', '326-555-6329'),\r\n ('Helmholtz', 'Watson', 'Janitor', '944 Soma Court', '329-555-2478'); <\/pre>\n
\r\nCREATE TABLE `Hours` (\r\n `ID` TINYINT(3) UNSIGNED NOT NULL,\r\n `Clock_In` DATETIME NOT NULL,\r\n `Clock_Out` DATETIME NOT NULL\r\n) ENGINE=MyISAM;<\/pre>\n
\r\nINSERT INTO `Hours`\r\nVALUES ('1', '2005-08-08 07:00:42', '2005-08-08 17:01:36'),\r\n ('1', '2005-08-09 07:01:34', '2005-08-09 17:10:11'),\r\n ('1', '2005-08-10 06:59:56', '2005-08-10 17:09:29'),\r\n ('1', '2005-08-11 07:00:17', '2005-08-11 17:00:47'),\r\n ('1', '2005-08-12 07:02:29', '2005-08-12 16:59:12'),\r\n ('2', '2005-08-08 07:00:25', '2005-08-08 17:03:13'),\r\n ('2', '2005-08-09 07:00:57', '2005-08-09 17:05:09'),\r\n ('2', '2005-08-10 06:58:43', '2005-08-10 16:58:24'),\r\n ('2', '2005-08-11 07:01:58', '2005-08-11 17:00:45'),\r\n ('2', '2005-08-12 07:02:12', '2005-08-12 16:58:57'),\r\n ('3', '2005-08-08 07:00:12', '2005-08-08 17:01:32'),\r\n ('3', '2005-08-09 07:01:10', '2005-08-09 17:00:26'),\r\n ('3', '2005-08-10 06:59:53', '2005-08-10 17:02:53'),\r\n ('3', '2005-08-11 07:01:15', '2005-08-11 17:04:23'),\r\n ('3', '2005-08-12 07:00:51', '2005-08-12 16:57:52'),\r\n ('4', '2005-08-08 06:54:37', '2005-08-08 17:01:23'),\r\n ('4', '2005-08-09 06:58:23', '2005-08-09 17:00:54'),\r\n ('4', '2005-08-10 06:59:14', '2005-08-10 17:00:12'),\r\n ('4', '2005-08-11 07:00:49', '2005-08-11 17:00:34'),\r\n ('4', '2005-08-12 07:01:09', '2005-08-12 16:58:29'),\r\n ('5', '2005-08-08 07:00:04', '2005-08-08 17:01:43'),\r\n ('5', '2005-08-09 07:02:12', '2005-08-09 17:02:13'),\r\n ('5', '2005-08-10 06:59:39', '2005-08-10 17:03:37'),\r\n ('5', '2005-08-11 07:01:26', '2005-08-11 17:00:03'),\r\n ('5', '2005-08-12 07:02:15', '2005-08-12 16:59:02'),\r\n ('6', '2005-08-08 07:00:12', '2005-08-08 17:01:02'),\r\n ('6', '2005-08-09 07:03:44', '2005-08-09 17:00:00'),\r\n ('6', '2005-08-10 06:54:19', '2005-08-10 17:03:31'),\r\n ('6', '2005-08-11 07:00:05', '2005-08-11 17:02:57'),\r\n ('6', '2005-08-12 07:02:07', '2005-08-12 16:58:23');\r\n\r\n\/\/\u6240\u4ee5\uff0c\u8fd9\u5f20\u8868\u8bb0\u5f55\u4e862015-08-08-2015-08-12\u7684\u4e00\u4e2a\u5de5\u4f5c\u5468\u7684\u6253\u5361\u8bb0\u5f55\r\n<\/pre>\n
MariaDB [world]> SELECT E.First_Name,E.Last_Name,H.Clock_IN,H.Clock_Out\r\n -> FROM Employees AS E\r\n -> INNER JOIN Hours AS H ON E.ID = H.ID\r\n -> WHERE DATE_FORMAT(H.Clock_In,'%Y-%m-%d') >= '2005-08-08'\r\n -> AND DATE_FORMAT(H.Clock_In,'%Y-%m-%d') < = '2005-08-12' -> AND DATE_FORMAT(H.Clock_In,'%H:%i:%S') > '07:00:59';\r\n+------------+-----------+---------------------+---------------------+\r\n| First_Name | Last_Name | Clock_IN | Clock_Out |\r\n+------------+-----------+---------------------+---------------------+\r\n| Mustapha | Mond | 2005-08-09 07:01:34 | 2005-08-09 17:10:11 |\r\n| Mustapha | Mond | 2005-08-12 07:02:29 | 2005-08-12 16:59:12 |\r\n| Henry | Foster | 2005-08-11 07:01:58 | 2005-08-11 17:00:45 |\r\n| Henry | Foster | 2005-08-12 07:02:12 | 2005-08-12 16:58:57 |\r\n| Bernard | Marx | 2005-08-09 07:01:10 | 2005-08-09 17:00:26 |\r\n| Bernard | Marx | 2005-08-11 07:01:15 | 2005-08-11 17:04:23 |\r\n| Lenina | Crowne | 2005-08-12 07:01:09 | 2005-08-12 16:58:29 |\r\n| Fanny | Crowne | 2005-08-09 07:02:12 | 2005-08-09 17:02:13 |\r\n| Fanny | Crowne | 2005-08-11 07:01:26 | 2005-08-11 17:00:03 |\r\n| Fanny | Crowne | 2005-08-12 07:02:15 | 2005-08-12 16:59:02 |\r\n| Helmholtz | Watson | 2005-08-09 07:03:44 | 2005-08-09 17:00:00 |\r\n| Helmholtz | Watson | 2005-08-12 07:02:07 | 2005-08-12 16:58:23 |\r\n+------------+-----------+---------------------+---------------------+\r\n12 rows in set (0.00 sec)\r\n<\/pre>\n
\r\nTIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)\r\n\r\nunit\u6709\u5982\u4e0b\u503c\uff1a\r\n MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR\r\n\r\nMariaDB [world]> SELECT TIMESTAMPDIFF(HOUR,'2018-01-01','2018-01-02');\r\n+-----------------------------------------------+\r\n| TIMESTAMPDIFF(HOUR,'2018-01-01','2018-01-02') |\r\n+-----------------------------------------------+\r\n| 24 |\r\n+-----------------------------------------------+\r\n1 row in set (0.00 sec)\r\n<\/pre>\n
MariaDB [world]> SELECT \r\n -> E.First_Name,E.Last_Name,H.Clock_In,H.Clock_Out\r\n -> ,TIMESTAMPDIFF(MINUTE,H.Clock_In,H.Clock_Out)-600 AS DIFFERENCE\r\n -> FROM Employees AS E\r\n -> INNER JOIN Hours AS H\r\n -> ON E.ID = H.ID\r\n -> WHERE DATE_FORMAT(H.Clock_In , '%H:%i:%S') > '07:00:59'\r\n -> AND TIMESTAMPDIFF(MINUTE,H.Clock_In,H.Clock_Out) < 600;\r\n+------------+-----------+---------------------+---------------------+------------+\r\n| First_Name | Last_Name | Clock_In | Clock_Out | DIFFERENCE |\r\n+------------+-----------+---------------------+---------------------+------------+\r\n| Mustapha | Mond | 2005-08-12 07:02:29 | 2005-08-12 16:59:12 | -4 |\r\n| Henry | Foster | 2005-08-11 07:01:58 | 2005-08-11 17:00:45 | -2 |\r\n| Henry | Foster | 2005-08-12 07:02:12 | 2005-08-12 16:58:57 | -4 |\r\n| Bernard | Marx | 2005-08-09 07:01:10 | 2005-08-09 17:00:26 | -1 |\r\n| Lenina | Crowne | 2005-08-12 07:01:09 | 2005-08-12 16:58:29 | -3 |\r\n| Fanny | Crowne | 2005-08-11 07:01:26 | 2005-08-11 17:00:03 | -2 |\r\n| Fanny | Crowne | 2005-08-12 07:02:15 | 2005-08-12 16:59:02 | -4 |\r\n| Helmholtz | Watson | 2005-08-09 07:03:44 | 2005-08-09 17:00:00 | -4 |\r\n| Helmholtz | Watson | 2005-08-12 07:02:07 | 2005-08-12 16:58:23 | -4 |\r\n+------------+-----------+---------------------+---------------------+------------+\r\n9 rows in set (0.00 sec)\r\n<\/pre>\n
MariaDB [world]> CREATE SQL SECURITY INVOKER VIEW Employee_LateList AS\r\n -> SELECT \r\n -> E.First_Name,E.Last_Name,H.Clock_In,H.Clock_Out\r\n -> ,TIMESTAMPDIFF(MINUTE,H.Clock_In,H.Clock_Out)-600 AS DIFFERENCE\r\n -> FROM Employees AS E\r\n -> INNER JOIN Hours AS H\r\n -> ON E.ID = H.ID\r\n -> WHERE DATE_FORMAT(H.Clock_In , '%H:%i:%S') > '07:00:59'\r\n -> AND TIMESTAMPDIFF(MINUTE,H.Clock_In,H.Clock_Out) < 600; \r\nQuery OK, 0 rows affected (0.01 sec) \r\nMariaDB [world]> SHOW TABLES;\r\n+-------------------+\r\n| Tables_in_world |\r\n+-------------------+\r\n| Employee_LateList | \/\/\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u591a\u4e86\u4e00\u5f20\u8868\r\n| Employees |\r\n| Hours |\r\n| city |\r\n| country |\r\n| countrylanguage |\r\n| department |\r\n| user |\r\n+-------------------+\r\n8 rows in set (0.06 sec)\r\n<\/pre>\n
\r\nMariaDB [world]> SELECT * FROM Employee_LateList;\r\n+------------+-----------+---------------------+---------------------+------------+\r\n| First_Name | Last_Name | Clock_In | Clock_Out | DIFFERENCE |\r\n+------------+-----------+---------------------+---------------------+------------+\r\n| Mustapha | Mond | 2005-08-12 07:02:29 | 2005-08-12 16:59:12 | -4 |\r\n| Henry | Foster | 2005-08-11 07:01:58 | 2005-08-11 17:00:45 | -2 |\r\n| Henry | Foster | 2005-08-12 07:02:12 | 2005-08-12 16:58:57 | -4 |\r\n| Bernard | Marx | 2005-08-09 07:01:10 | 2005-08-09 17:00:26 | -1 |\r\n| Lenina | Crowne | 2005-08-12 07:01:09 | 2005-08-12 16:58:29 | -3 |\r\n| Fanny | Crowne | 2005-08-11 07:01:26 | 2005-08-11 17:00:03 | -2 |\r\n| Fanny | Crowne | 2005-08-12 07:02:15 | 2005-08-12 16:59:02 | -4 |\r\n| Helmholtz | Watson | 2005-08-09 07:03:44 | 2005-08-09 17:00:00 | -4 |\r\n| Helmholtz | Watson | 2005-08-12 07:02:07 | 2005-08-12 16:58:23 | -4 |\r\n+------------+-----------+---------------------+---------------------+------------+\r\n9 rows in set (0.00 sec)\r\n\/\/\u52a0\u4e2a\u6761\u4ef6\u518d\u67e5\u8be2\u4e0b\uff0c\u67e5\u8be22005-08-12\u5f53\u5929\u8fdf\u5230\u7684\u4eba\r\n\r\nMariaDB [world]> SELECT * FROM Employee_LateList \r\n -> WHERE DATE_FORMAT(Clock_In,'%Y-%m-%d') = '2005-08-12';\r\n+------------+-----------+---------------------+---------------------+------------+\r\n| First_Name | Last_Name | Clock_In | Clock_Out | DIFFERENCE |\r\n+------------+-----------+---------------------+---------------------+------------+\r\n| Mustapha | Mond | 2005-08-12 07:02:29 | 2005-08-12 16:59:12 | -4 |\r\n| Henry | Foster | 2005-08-12 07:02:12 | 2005-08-12 16:58:57 | -4 |\r\n| Lenina | Crowne | 2005-08-12 07:01:09 | 2005-08-12 16:58:29 | -3 |\r\n| Fanny | Crowne | 2005-08-12 07:02:15 | 2005-08-12 16:59:02 | -4 |\r\n| Helmholtz | Watson | 2005-08-12 07:02:07 | 2005-08-12 16:58:23 | -4 |\r\n+------------+-----------+---------------------+---------------------+------------+\r\n5 rows in set (0.00 sec)\r\n\/\/\u8fd9\u4e2aDATE_FORMAT(Clock_In,'%Y-%m-%d') = '2005-08-12'\u67e5\u8be2\u6761\u4ef6\u4f1a\u4ee5AND \u76f4\u63a5\u62fc\u63a5\u5230\u6211\u4eec\u7684\u590d\u6742SQL\u540e\u9762\r\n<\/pre>\n
\r\nCREATE\r\n [OR REPLACE]\r\n [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]\r\n [DEFINER = { user | CURRENT_USER | role | CURRENT_ROLE }]\r\n [SQL SECURITY { DEFINER | INVOKER }]\r\n VIEW [IF NOT EXISTS] view_name [(column_list)]\r\n AS select_statement\r\n [WITH [CASCADED | LOCAL] CHECK OPTION]\r\n<\/pre>\n
\r\nCREATE OR REPLACE SQL SECURITY INVOKER VIEW test_view AS SELECT * FROM city;\r\nMariaDB [world]> SELECT * FROM test_view LIMIT 3;\r\n+----+----------+-------------+----------+------------+\r\n| ID | Name | CountryCode | District | Population |\r\n+----+----------+-------------+----------+------------+\r\n| 1 | Kabul | AFG | Kabol | 1780000 |\r\n| 2 | Qandahar | AFG | Qandahar | 237500 |\r\n| 3 | Herat | AFG | Herat | 186800 |\r\n+----+----------+-------------+----------+------------+\r\n3 rows in set (0.00 sec)\r\n\/\/\u4e3acity\u8868\u589e\u52a0\u4e00\u4e2a\u5b57\u6bb5\r\nMariaDB [world]> ALTER TABLE city\r\n -> ADD COLUMN Test INT(3);\r\nQuery OK, 0 rows affected (0.20 sec)\r\nRecords: 0 Duplicates: 0 Warnings: 0\r\n\/\/\u4ececity\u8868\u68c0\u7d22\u4e00\u6761\u6570\u636e\r\nMariaDB [world]> SELECT * FROM city LIMIT 1;\r\n+----+-------+-------------+----------+------------+------+\r\n| ID | Name | CountryCode | District | Population | Test |\r\n+----+-------+-------------+----------+------------+------+\r\n| 1 | Kabul | AFG | Kabol | 1780000 | NULL |\r\n+----+-------+-------------+----------+------------+------+\r\n1 row in set (0.00 sec)\r\n\/\/\u4ece\u89c6\u56fe\u4e2d\u68c0\u7d22\u4e00\u6761\u6570\u636e\r\nMariaDB [world]> SELECT * FROM test_view LIMIT 1;\r\n+----+-------+-------------+----------+------------+\r\n| ID | Name | CountryCode | District | Population |\r\n+----+-------+-------------+----------+------------+\r\n| 1 | Kabul | AFG | Kabol | 1780000 |\r\n+----+-------+-------------+----------+------------+\r\n1 row in set (0.01 sec)\r\n<\/pre>\n
\n\u5f53SQL SECURITY\u4e3aDEFINER\u65f6\uff0ctest\u68c0\u7d22\u6b64\u89c6\u56fe\u65f6\uff0c\u8bbf\u95eecity\u8868\u7684\u7528\u6237\u5b9e\u9645\u4e0a\u662fdefiner\u7528\u6237
\n\u5f53SQL SECURITY\u4e3aINVOKER\u65f6\uff0ctest\u68c0\u7d22\u6b64\u89c6\u56fe\u65f6\uff0c\u8bbf\u95eecity\u8868\u7684\u7528\u6237\u65f6test\uff0c\u800ctest\u5bf9\u8be5\u8868\u6ca1\u6709\u68c0\u7d22\u6743\u9650\uff0c\u6240\u4ee5\u4f1a\u62a5\u9519\u3002<\/p>\n\n
\r\n\/\/\u521b\u5efa\u89c6\u56fe\u5982\u4e0b\r\nCREATE OR REPLACE ALGORITHM=MERGE SQL SECURITY INVOKER VIEW test_view AS SELECT * FROM city ORDER BY Id;\r\n\/\/\u67e5\u8be2\u89c6\u56fe\r\nSELECT * FROM test_view WHERE ID < 5;\r\n\/\/\u4ee5\u4e0a\u67e5\u8be2\u7b49\u540c\u4e8e\u4e0b\u9762\u7684\u67e5\u8be2\r\nSELECT ID,Name,CountryCode,District,Population FROM city WHERE ID < 5;\r\n<\/pre>\n
\n
\r\nDROP VIEW [IF EXISTS]\r\n view_name [, view_name] ...\r\n<\/pre>\n
\r\nALTER\r\n [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]\r\n [DEFINER = { user | CURRENT_USER }]\r\n [SQL SECURITY { DEFINER | INVOKER }]\r\n VIEW view_name [(column_list)]\r\n AS select_statement\r\n [WITH [CASCADED | LOCAL] CHECK OPTION]\r\n<\/pre>\n
\r\nSHOW CREATE VIEW view_name;\r\n<\/pre>\n
\n\u9664\u975e\u6709\u5fc5\u8981\uff0c\u771f\u7684\u4e0d\u5efa\u8bae\u4f7f\u7528\u5411\u89c6\u56fe\u4e2d\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u6570\u636e\uff0c\u9664\u4e86\u8bf8\u591a\u9650\u5236\u4ee5\u4e3a\uff0c\u6211\u611f\u89c9\u8fd8\u8fdd\u80cc\u4e86\u89c6\u56fe\u7684\u5b58\u5728\u539f\u5219\uff1b\u89c6\u56fe\uff0c\u82f1\u6587\u4e3aVIEW\uff0c\u800cVIEW\u672c\u8eab\u5c31\u662f\u4e00\u79cd\u89c2\u70b9\uff0c\u4e00\u79cd\u89d2\u5ea6\uff0c\u8fd8\u662f\u7528\u6765\u770b\u7684\u6bd4\u8f83\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"