{"id":863,"date":"2017-11-18T13:48:18","date_gmt":"2017-11-18T05:48:18","guid":{"rendered":"https:\/\/www.beneplot.com\/?p=863"},"modified":"2021-07-15T10:51:54","modified_gmt":"2021-07-15T02:51:54","slug":"sql%e4%bd%bf%e7%94%a8%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.beneplot.com\/?p=863","title":{"rendered":"SQL\u8bed\u53e5\u8bb0\u5f55"},"content":{"rendered":"<p><strong><span style=\"font-size: 16px;\">\u5f53\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u4f7f\u7528EXCEL\u5904\u7406\u6570\u636e\u66f4\u65b9\u4fbf\u548c\u9ad8\u6548\u3002<\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 16px;\">\u4f46\u82e5\u662f\u6570\u636e\u91cf\u5f88\u5927\uff0c\u6216\u662f\u9700\u8981\u5bf9\u8f83\u591a\u6570\u636e\u8fdb\u884c\u590d\u6742\u7684\u5904\u7406\uff0cSQL\u7684\u6548\u7387\u4fbf\u663e\u73b0\u51fa\u6765\u4e86\u3002<\/span><\/strong><\/p>\n<h2><strong><span style=\"font-size: 16px;\">\u5728\u8fd9\u91cc\u8bb0\u5f55\u5e38\u7528\u7684SQL\u8bed\u53e5\uff0c<\/span><\/strong><strong style=\"font-size: 1.28571rem;\"><span style=\"font-size: 16px;\">\u6211\u7684SQL\u6c34\u5e73\u6bd4\u8f83\u521d\u7ea7\u54c8<\/span><\/strong><strong style=\"font-size: 1.28571rem;\"><span style=\"font-size: 16px;\">\ud83d\ude42<\/span><\/strong><\/h2>\n<hr \/>\n<p>&nbsp;<\/p>\n<div>\/* \u5c061\u4e2a\u5df2\u6709\u6570\u636e\u8868\u4e2d\u7684\u5168\u90e8\u6570\u636e\u63d0\u53d6\u52301\u4e2a\u65b0\u521b\u5efa\u7684\u8868\u4e2d, <strong>\u8868\u7684\u65b0\u5efa\u4e0e\u5907\u4efd<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>CREATE TABLE<\/strong> \u65b0\u8868\u540d<strong> AS\u00a0<\/strong><\/code><\/div>\n<div><code><strong>SELECT<\/strong> * <strong>FROM<\/strong> \u5df2\u6709\u65e7\u8868\u540d<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\/* \u4e0b\u53e5\u4e0e\u4e0a\u53e5\u7684\u7ed3\u679c\u76f8\u540c*\/<\/div>\n<div>\u00a0<\/div>\n<div><strong><code>SELECT *<\/code><\/strong><code><strong>INTO<\/strong> \u65b0\u8868\u540d<\/code><br \/>\n<code><strong>FROM<\/strong> \u5df2\u6709\u65e7\u8868\u540d;<\/code><\/div>\n<div>\u00a0<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\/*\u5728\u88681\u4e2d<strong>\u63d2\u5165\u4e00\u884c\u65b0\u6570\u636e<\/strong>*\/<\/div>\n<div>\n<code><strong>INSERT INTO<\/strong> \u88681\u540d (\u52171\u540d, \u52172\u540d, \u52173\u540d, \u2026\u2026) <strong>VALUES<\/strong> (\u503c1, \u503c2, \u503c3, \u2026\u2026);<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\u00a0<\/div>\n<hr \/>\n<div>\/*\u5220\u9664<strong>\u5217<\/strong>*\/<\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>alter table<\/strong> \u8868\u540d<strong> drop column<\/strong> \u5217\u540d<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\/*\u5220\u9664<strong>\u884c<\/strong>*\/<\/div>\n<div>\n<div>\n<div>\n<div>\u00a0<\/div>\n<div><code><strong>DELETE FROM<\/strong> \u8868\u540d<\/code><\/div>\n<div><code><strong>WHERE<\/strong> \u5217\u540d = \u2018\u8981\u5220\u9664\u884c\u5bf9\u5e94\u7684\u5b57\u6bb5\u2019;<\/code><\/div>\n<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/* \u5c063\u4e2a\u5217\u540d\u76f8\u540c\u7684\u8868<strong>\u7eb5\u5411\u5408\u62101\u4e2a\u65b0\u8868<\/strong>,\u6bcf\u4e2a\u8868\u7684\u5217\u540d\u548c\u7ed3\u6784\u9700\u76f8\u540c*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>CREATE TABLE<\/strong> \u65b0\u8868\u540d <strong>AS<\/strong><\/code><\/div>\n<div><code><strong>SELECT<\/strong> * <strong>FROM<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>UNION ALL SELECT<\/strong> * <strong>FROM<\/strong> \u88682\u540d\u00a0<\/code><\/div>\n<div><code><strong>UNION ALL SELECT<\/strong> * <strong>FROM<\/strong> \u88683\u540d<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\/* \u5982\u4e0d\u52a0UNION\u540e\u9762\u7684ALL\uff0c\u5219\u5408\u5e76\u540e\u4f1a<strong>\u5220\u9664\u91cd\u590d\u6570\u636e<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\/* \u6c42\u4e0d\u540c\u8868\u7684<strong>\u4ea4\u96c6<\/strong>\uff0c\u5373\u63d0\u53d6\u88681\u548c\u88682\u5171\u6709\u7684\u5185\u5bb9*\/<\/div>\n<div>\u00a0<\/div>\n<div>\n<div><code><strong>SELECT<\/strong> * <strong>FROM<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>INTERSECT SELECT<\/strong> * <strong>FROM<\/strong> \u88682\u540d\u00a0<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/* \u6c42\u4e0d\u540c\u8868\u7684<strong>\u5dee\u96c6<\/strong>\uff0c\u5373\u63d0\u53d6\uff1a\u5220\u9664\u88681\u548c\u88682\u5171\u6709\u5185\u5bb9\u540e\u7684\u88681\u5185\u5bb9*\/<\/div>\n<div>\u00a0<\/div>\n<div>\n<div><code><strong>SELECT<\/strong> * <strong>FROM<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>EXCEPT SELECT<\/strong> * <strong>FROM<\/strong> \u88682\u540d\u00a0<\/code><\/div>\n<\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<hr \/>\n<div><span style=\"font-size: 1rem;\">\/* \u67e51\u5f20\u8868\u5171\u6709<\/span><strong style=\"font-size: 1rem;\">\u591a\u5c11\u884c\u6570\u636e<\/strong><span style=\"font-size: 1rem;\">*\/<\/span><\/div>\n<div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT COUNT<\/strong>(*) <strong>FROM<\/strong> \u8868\u540d<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<hr \/>\n<div><code><strong>SELECT CEILING<\/strong>(1.1)\u00a0<\/code> <span style=\"font-size: 1rem;\">\/* <strong>\u5411\u4e0a\u53d6\u6574<\/strong>\uff0c\u8fd4\u56de2*\/<\/span><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT FLOOR<\/strong>(1.9)\u00a0<\/code> <span style=\"font-size: 1rem;\">\/* <strong>\u5411\u4e0b\u53d6\u6574<\/strong>\uff0c\u8fd4\u56de1*\/<\/span><\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT round<\/strong>(1.567,0)\u00a0<\/code> <span style=\"font-size: 1rem;\">\/* <strong>\u56db\u820d\u4e94\u5165\u53d6\u6574<\/strong>\uff0c\u8fd4\u56de2*\/<\/span><\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/* \u5c06\u4e0d\u540c\u7684\u8868\u4f9d\u636e\u76f8\u540c\u7684\u884c\u540d\u6a2a\u5411\u5408\u62101\u4e2a\u65b0\u8868, <strong>\u7c7b\u4f3cexcel\u7684vlookup\u51fd\u6570<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>CREATE TABLE<\/strong> \u65b0\u8868\u540d <strong>AS<\/strong><\/code><\/div>\n<div><code><strong>SELECT * FROM<\/strong> \u88681\u540d <strong>AS<\/strong> \u88681\u522b\u540d<\/code><\/div>\n<div><code><strong>LEFT OUTER JOIN<\/strong> \u88682\u540d<strong> AS<\/strong> \u88682\u522b\u540d\u00a0<\/code><\/div>\n<div><code><strong>ON<\/strong> (\u88681\u522b\u540d.\u4e0e\u88682\u5185\u5bb9\u5339\u914d\u7684\u5217\u540d = \u88682\u522b\u540d.\u4e0e\u88681\u5185\u5bb9\u5339\u914d\u7684\u5217\u540d);<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\/*\u5982\u679c\u4f7f\u7528<strong>INNER JOIN<\/strong> \u66ff\u6362 OUTER JOIN ,\u5219\u63d0\u53d6\u7ed3\u679c\u4e3a\u4e24\u4e2a\u8868\u4e2d\u5171\u6709\u7684\u5185\u5bb9\uff0c\u4e0d\u4f1a\u51fa\u73b0NULL\u503c*\/<\/div>\n<div>\n<div>\u00a0<\/div>\n<div>\/* \u4f7f\u7528 <b>RIGHT <\/b>\u65f6FROM \u5b50\u53e5\u4e2d\u5199\u5728\u53f3\u4fa7\u7684\u8868\u662f\u4e3b\u8868*\/<\/div>\n<\/div>\n<div>\u00a0<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/* <strong>to_char\u51fd\u6570\u4e0eCAST\u51fd\u6570<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\/*\u5c06\u539f\u6709\u503c(\u975e\u5b57\u7b26\u4e32)\u8f6c\u6362\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u7c7b\u578b*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT to_char<\/strong>( \u5217\u540d, '\u81ea\u5b9a\u4e49\u683c\u5f0f' ) <strong>FROM<\/strong> \u8868\u540d<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/* \u4e0b\u4f8b\u5c06\u65f6\u95f4\u683c\u5f0f\u8f6c\u6362\u4e3a\u4ec5\u663e\u793a\u5e74\u6708\u7684\u5b57\u7b26\u4e32\u683c\u5f0f, \u5982\u5c062020-01-01\u8f6c\u4e3a &#8216;2020-01&#8217; *\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT to_char<\/strong>(time, 'YYYY-MM' ) <strong>FROM<\/strong> \u8868\u540d<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/* CAST(\u8f6c\u6362\u524d\u7684\u503c AS \u60f3\u8981\u8f6c\u6362\u7684\u6570\u636e\u7c7b\u578b),\u4e0b\u4f8b\u662f\u5c06\u5b57\u7b26\u4e32\u65e5\u671f\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b, \u8868\u9762\u770b\u4e0d\u51fa\u5dee\u522b*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT CAST<\/strong>(\u5217\u540d <strong>AS DATE<\/strong> ) <strong>FROM<\/strong> \u8868\u540d<\/code><\/div>\n<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/*<strong>COALESCE \u2014 \u5c06NULL\u8f6c\u6362\u4e3a\u5176\u4ed6\u503c<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div>\/*\u628a\u88681\u7684\u52171\u4e2d\u7684\u7a7a\u503cNULL\u5168\u90e8\u8f6c\u4e3aXXX *\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>UPDATE<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>SET<\/strong> \u52171\u540d = <strong>COALESCE<\/strong>(\u52171\u540d, 'XXX')<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u6216\u662f\uff1a<\/div>\n<div><code><strong>SELECT COALESCE<\/strong>(\u52171\u540d, 'XXX')<\/code><code><strong>FROM<\/strong> \u88681\u540d<\/code><\/div>\n<\/div>\n<hr \/>\n<p>&nbsp;<\/p>\n<div>\n<div>\/*<strong>\u63d0\u53d6\u88681\u524d5\u884c\u7684\u6570\u636e<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT<\/strong> *<strong> FROM<\/strong> \u88681\u540d<\/code><\/div>\n<div><code><strong>limit<\/strong> 5<\/code><\/div>\n<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\n<div>\u00a0<\/div>\n<div>\/*<strong>\u63d0\u53d6\u88681\u7684\u6240\u6709\u5217\u540d<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><strong><code>select column_name<\/code><\/strong><\/div>\n<div><strong><code>from information_schema.columns\u00a0<\/code><\/strong><\/div>\n<div><code><strong>where table_name<\/strong> = '\u88681\u540d';<\/code><\/div>\n<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/*\u4e0b\u8bed\u53e5\u7684\u610f\u601d\u662f\uff1a\u4ece\u88681\u4e2d\u63d0\u53d63\u5217\u6570\u636e,<\/div>\n<div>\u00a0<\/div>\n<div>\u63d0\u53d6\u51fa\u7684\u6570\u636e\u4ee5\u52173<strong>\u964d\u5e8f\u6392\u5217(\u4e0d\u52a0DESC\u5219\u4f1a\u4ee5\u5347\u5e8f\u6392\u5217)<\/strong>,<\/div>\n<div>\u00a0<\/div>\n<div>\u5e76\u4e14\u8fd9\u4e9b\u6570\u636e\u9700<strong>\u540c\u65f6\u6ee1\u8db32\u4e2a\u6761\u4ef6<\/strong>\uff1a\u52171=XX\u548c\u52172=1\u3002<\/div>\n<div>\u00a0<\/div>\n<div>\u6709\u65f6\u5019\u56e0\u4e3a\u67d0\u4e9b\u683c\u5f0f\u9650\u5236,\u773c\u89c1\u662fXX,\u4f46\u8f93\u5165XX\u5374\u63d0\u53d6\u4e0d\u51fa\u6570\u636e,<\/div>\n<div>\u00a0<\/div>\n<div>\u56e0\u6b64\u53ef\u5c1d\u8bd5\u4f7f\u7528\u201cLIKE &#8216;%XX%&#8217;\u201d\u6765\u63d0\u53d6XX\u6570\u636e*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT<\/strong> \u52171\u540d, \u52172\u540d, \u52173\u540d\u00a0<strong> FROM<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>where<\/strong> \u5217\u540d1 <strong>LIKE<\/strong> '%XX%'<strong> AND<\/strong> \u52172\u540d = 1<\/code><\/div>\n<div><code><strong>ORDER BY<\/strong> \u52173\u540d <strong>DESC<\/strong>;<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\/* <strong>LIKE\u4f7f\u7528\u8bf4\u660e<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><code>\u00a0<strong>LIKE<\/strong> 'XX%' \u4f1a\u5c06\u4ee5XX\u5f00\u5934\u7684\u6570\u636e\u63d0\u53d6\u51fa\u6765,\u5982XXYY,XX123\uff1b<\/code><\/div>\n<div><code>\u00a0<strong>LIKE<\/strong> '%XX' \u4f1a\u5c06\u4ee5XX\u7ed3\u5c3e\u7684\u6570\u636e\u63d0\u53d6\u51fa\u6765,\u5982YYXX,123XX\uff1b<\/code><\/div>\n<div><code><strong>\u00a0LIKE<\/strong> '%XX%' \u4f1a\u5c06\u542b\u6709XX\u7684\u6240\u6709\u6570\u636e\u63d0\u53d6\u51fa\u6765\u3002<\/code><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\n<div>\/*\u4e0b\u8bed\u53e5\u7684\u610f\u601d\u662f\uff1a\u4ece\u88681\u4e2d\u63d0\u53d64\u5217\u6570\u636e,<\/div>\n<div>\u00a0<\/div>\n<div>\u8fd9\u4e9b\u6570\u636e\u9700\u540c\u65f6\u6ee1\u8db32\u4e2a\u6761\u4ef6\uff1a\u52175=1\u548c\u52176=2,<\/div>\n<div>\u00a0<\/div>\n<div><strong>\u63d0\u53d6\u51fa\u7684\u6570\u636e\u4f1a\u5220\u9664\u91cd\u590d\u7684\u52172\u3001\u52173\u3001\u52174\u7ec4\u5408<\/strong>,<\/div>\n<div>\u00a0<\/div>\n<div><strong>\u63d0\u53d6\u51fa\u7684\u52171\u53d8\u6210\u8ba1\u6570\u5217, \u5373\u5728\u67d0\u4e2a\u52172\u3001\u52173\u3001\u52174\u7ec4\u5408\u4e0b,\u52171\u4e2d\u5305\u542b\u7684\u4e0e\u4e4b\u76f8\u5bf9\u5e94\u7684\u7684\u6570\u636e\u91cf<\/strong>\u3002<\/div>\n<div>\u00a0<\/div>\n<div>\u4f8b\u5b50\uff1a\u5047\u8bbe\u52172\u662f\u65f6\u95f4\u3001\u52173\u662f\u59d3\u540d\u3001\u52174\u5730\u70b9,\u201c2020.1\u6708\/\u5f20\u4e09\/\u5728\u4f53\u80b2\u573a\u201d\u6784\u6210\u4e86\u4e00\u4e2a\u7ec4\u5408,<\/div>\n<div>\u00a0<\/div>\n<div>\u5047\u5982\u52171\u662f\u4e8b\u4ef6,\u5982\u679c\u201d\u57282020.1\u6708\u5185,\u5f20\u4e09\u5728\u4f53\u80b2\u573a\u201c\u8fdb\u884c\u4e86\u8dd1\u6b65\u3001\u6253\u7bee\u7403\u3001\u8e22\u8db3\u7403\u4e09\u6b21\u8fd0\u52a8,<\/div>\n<div>\u00a0<\/div>\n<div>count\u7684\u8ba1\u7b97\u7ed3\u679c\u662f3\u3002*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>CREATE TABLE<\/strong> \u88682\u540d<strong> AS<\/strong><\/code><\/div>\n<div><code><strong>select count<\/strong>(\u52171\u540d), \u52172\u540d, \u52173\u540d, \u52174\u540d <strong>from<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>where<\/strong> \u52175\u540d = 1 <strong>AND<\/strong> \u52176\u540d = 2<\/code><\/div>\n<div><code><strong>GROUP BY<\/strong> \u52172\u540d, \u52173\u540d, \u52174\u540d<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\u00a0<\/div>\n<div>\/*\u4e0a\u9762\u7684\u7ed3\u679c\u544a\u8bc9\u6211\u4eec\uff1a\u67d0\u4eba\u5728\u67d0\u5730\u5728\u67d0\u65f6\u95f4\u6bb5\u5185\u505a\u4e86\u51e0\u4ef6\u4e8b,<\/div>\n<div>\u00a0<\/div>\n<div>\u63a5\u4e0b\u6765\u5982\u679c\u60f3\u77e5\u9053\uff1a\u5728\u6240\u6709\u65f6\u95f4\u6bb5\u5185,\u67d0\u4eba\u5728\u67d0\u5730\u505a\u4e86\u51e0\u4ef6\u4e8b,<\/div>\n<div>\u00a0<\/div>\n<div>\u4ecd\u65e7\u5047\u8bbe\u52173\u662f\u59d3\u540d\u3001\u52174\u5730\u70b9,<\/div>\n<div>\u00a0<\/div>\n<div>\u53ef\u4ee5\u5728\u4e0a\u9762\u63d0\u53d6\u7ed3\u679c\u7684\u88682\u4e0a\u518d\u6b21\u63d0\u53d6\u6570\u636e\u3002*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>select count<\/strong>(\u5217\u540d3), \u5217\u540d3, \u5217\u540d4<strong> from<\/strong> \u88682\u540d <strong>GROUP BY<\/strong> \u52173\u540d, \u52174\u540d<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\u00a0<\/div>\n<div>\/*\u4ee5\u4e0a2\u4e2a\u8bed\u53e5\u53ef\u7ec4\u5408\u4e3a1\u4e2a*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>select count<\/strong>(\u5217\u540d3), \u5217\u540d3, \u5217\u540d4<\/code><\/div>\n<div><strong><code>from<\/code><\/strong><\/div>\n<div><code>(<strong>select count<\/strong>(\u52171\u540d), \u52172\u540d, \u52173\u540d, \u52174\u540d <strong>from<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>where<\/strong> \u52175\u540d = 1<strong> AND<\/strong> \u52176\u540d = 2<\/code><\/div>\n<div><code><strong>GROUP BY<\/strong> \u52172\u540d, \u52173\u540d, \u52174\u540d)<strong> AS<\/strong> \u88682\u540d<\/code><\/div>\n<div><code><strong>GROUP BY<\/strong> \u52173\u540d, \u52174\u540d<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u4e0a\u9762\u8fd9\u4e2a\u8bed\u53e5\u548c\u4e0b\u9762\u8fd9\u4e2a\u8bed\u53e5\u7684\u76f8\u540c\u70b9\u662f\uff1a\u5b83\u4eec\u63d0\u53d6\u7684\u7ed3\u679c\u884c\u6570\u76f8\u540c,\u63d0\u53d6\u7684\u52173\u548c\u52174\u4e5f\u76f8\u540c\uff1b<\/div>\n<div>\u00a0<\/div>\n<div>\u4e0d\u540c\u70b9\u662f\uff1a\u5982\u679c\u57282020.1\u6708(\u540c\u4e00\u65f6\u95f4\u6bb5),\u5f20\u4e09\u6253\u4e86\u4e24\u6b21\u7bee\u7403,<\/div>\n<div>\u00a0<\/div>\n<div>\u4e0a\u4e00\u53e5\u4f1a\u8bb0\u5f551\u6b21(\u5176\u4e2d1\u4e2a<strong>GROUP BY\u7528\u4e8e\u5220\u9664<\/strong>\u540c\u4e00\u65f6\u95f4\u7684<strong>\u91cd\u590d\u503c<\/strong>),<\/div>\n<div>\u00a0<\/div>\n<div>\u4e0b\u4e00\u53e5\u4f1a\u8bb0\u5f552\u6b21*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>select count<\/strong>(\u5217\u540d1), \u5217\u540d3, \u5217\u540d4 <strong>from<\/strong> \u88681\u540d\u00a0<\/code><\/div>\n<div><code><strong>where<\/strong> \u5217\u540d5 = 1<strong> AND<\/strong> \u5217\u540d6 = 2<\/code><\/div>\n<div><code><strong>GROUP BY<\/strong> \u5217\u540d3, \u5217\u540d4<\/code><\/div>\n<\/div>\n<hr \/>\n<div>\n<div>\u00a0<\/div>\n<div>\/*<strong>\u4f7f\u7528DISTINCT\u53ef\u5220\u9664\u5217\u4e2d\u91cd\u590d\u7684\u6570\u636e<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT DISTINCT<\/strong> \u5217\u540d <strong>FROM<\/strong> \u8868\u540d;<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u4e0b\u8bed\u53e5\u548c\u4e0a\u8bed\u53e5\u4f1a\u5f97\u5230\u540c\u6837\u7684\u7ed3\u679c, \u5982\u4e0a\u6240\u8ff0, <strong>GROUP BY\u4e5f\u6709\u5220\u9664\u91cd\u590d\u6570\u636e\u7684\u4f5c\u7528<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT<\/strong> \u5217\u540d <strong>FROM<\/strong> \u8868\u540d<\/code><\/div>\n<div><code><strong>GROUP BY<\/strong> \u5217\u540d;<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/*<strong>\u4e0b\u8bed\u53e5\u4f1a\u5220\u9664\u52171\u548c\u52172\u7ec4\u5408\u540e\u7684\u91cd\u590d\u6570\u636e<\/strong>*\/<\/div>\n<div>\u00a0<\/div>\n<div><code><strong>SELECT DISTINCT<\/strong> \u52171\u540d, \u52172\u540d<\/code><\/div>\n<div><code><strong>FROM<\/strong> \u8868\u540d;<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>\/*<strong>\u5c06\u4e24\u5217\u5185\u5bb9\u5408\u6210\u4e3a\u4e00\u5217<\/strong>* \/<\/div>\n<div>\u00a0<\/div>\n<div>\/*\u63d0\u53d6\u4e24\u5217\u6570\u636e* \/<\/div>\n<div>\u00a0<\/div>\n<div><code>select e.last_name , e.first_name <\/code><br \/>\n<code>from name AS e<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u63d0\u53d61\u5217\u6570\u636e\uff0c\u4ee5\u5355\u5f15\u53f7&#8217;\u4e3a\u5206\u9694(\u5728psql\u4e2d\uff0c\u8981\u663e\u793a\u5355\u5f15\u53f7&#8217;\u9700\u8981\u952e\u5165\u4e24\u4e2a\u5355\u5f15\u53f7&#8221;)\uff0c<strong><code>||<\/code>\u610f\u4e3a\u5408\u5e76\u6570\u636e<\/strong>* \/<\/div>\n<div><code><strong>select<\/strong> e.last_name <strong>||<\/strong>''''<strong>||<\/strong> e.first_name as name<\/code><br \/>\n<code>from name e<\/code><\/div>\n<div>\u00a0<\/div>\n<hr \/>\n<div><strong>\/*\u89e6\u53d1\u5668\u8bbe\u7f6e* \/<\/strong><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u5f53\u67d0\u4e2a\u8868\u53d1\u751f\u67d0\u4e8b\u4ef6\u65f6\uff08INSERT\u3001UPDATE\u3001DELETE\u7b49\uff09\uff0c\u5176\u4ed6\u5de5\u4f5c\u8868\u4f1a\u76f8\u5e94\u53d1\u751f\u6539\u53d8*\/<\/div>\n<div>\u00a0<\/div>\n<div>\/*psql\u4e2d\u7684\u89e6\u53d1\u5668\u8bbe\u5b9a*\/<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/*\u521b\u5efa\u4e24\u4e2a\u8868*\/<\/div>\n<div>\u00a0<\/div>\n<div><code>drop table if exists test1;<\/code><\/div>\n<div><code>drop table if exists test2;<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>CREATE TABLE test1(<\/code><\/div>\n<div><code>\u00a0 \u00a0ID INT PRIMARY KEY\u00a0 \u00a0 \u00a0NOT NULL,<\/code><\/div>\n<div><code>\u00a0 \u00a0NAME\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0TEXT\u00a0 \u00a0 NOT NULL,<\/code><\/div>\n<div><code>\u00a0 \u00a0AGE\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 INT\u00a0 \u00a0 \u00a0NOT NULL,<\/code><\/div>\n<div><code>\u00a0 \u00a0ADDRESS\u00a0 \u00a0 \u00a0 \u00a0 CHAR(50)<\/code><\/div>\n<div><code>);<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>CREATE TABLE test2(<\/code><\/div>\n<div><code>\u00a0 \u00a0 Num INT NOT NULL,<\/code><\/div>\n<div><code>\u00a0 \u00a0 NAME TEXT NOT NULL<\/code><\/div>\n<div><code>);<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u9996\u5148\u9700\u8981\u521b\u5efa\u89e6\u53d1\u5668\u51fd\u6570*\/<\/div>\n<div>\u00a0<\/div>\n<div><code>CREATE OR REPLACE FUNCTION tfun() RETURNS TRIGGER AS $trigger1$<\/code><\/div>\n<div><code>\u00a0 \u00a0BEGIN<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0 INSERT INTO test2 VALUES (new.ID, new.name);<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0 RETURN NEW;<\/code><\/div>\n<div><code>\u00a0 \u00a0END;<\/code><\/div>\n<div><code>$trigger1$ LANGUAGE plpgsql;<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u521b\u5efa\u89e6\u53d1\u5668\uff0c\u6ce8\u610f\uff01\u89e6\u53d1\u5668\u540d\u5e94\u4e0e\u8868\u540d\u6709\u8054\u7cfb\uff0c\u65b9\u4fbf\u540e\u671f\u67e5\u627e*\/<\/div>\n<div>\u00a0<\/div>\n<div><code>CREATE TRIGGER trigger_test1\u00a0<\/code><\/div>\n<div><code>\u00a0 \u00a0AFTER INSERT ON test1 FOR EACH ROW\u00a0<\/code><\/div>\n<div><code> EXECUTE PROCEDURE tfun();<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u89e6\u53d1\u5668\u68c0\u9a8c\uff0c\u5728test1\u8868\u4e2d\u63d2\u5165\u4fe1\u606f\u65f6\uff0cID\u548cNAME\u4f1a\u81ea\u52a8\u6dfb\u8865\u5230test2\u8868\u4e2d*\/<\/div>\n<div>\u00a0<\/div>\n<div><code>INSERT INTO test1 (ID,NAME,AGE,ADDRESS)VALUES (1, 'Bene', 18, 'China' );<\/code><\/div>\n<div><code>select * from test1;<\/code><\/div>\n<div><code>select * from test2;<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/*\u67e5\u627e\u6570\u636e\u5e93\u4e2d\u5df2\u6709\u7684\u89e6\u53d1\u5668*\/<\/div>\n<div><code>SELECT * FROM pg_trigger;<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\/*\u5220\u9664\u89e6\u53d1\u5668\uff0c<code>DROP TRIGGER \u89e6\u53d1\u5668\u540d on \u6240\u5728\u8868\u540d<\/code>*\/<\/div>\n<div><code>DROP TRIGGER trigger_test1 on test1;<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<hr \/>\n<p>\/*<strong>\u8ba1\u7b97\u4e00\u4e32\u5b57\u7b26\u4e2d\u6709\u51e0\u4e2a\u9017\u53f7<\/strong>*\/<\/p>\n<div><code>SELECT LENGTH('i,10ve,y0u')-LENGTH(e) FROM <\/code><br \/>\n<code>(SELECT REPLACE ('i,10ve,y0u',',','')AS e) AS a<\/code><\/div>\n<hr \/>\n<p><strong>\/*\u4ee5\u59d3\u540d\u6700\u540e\u4e24\u4e2a\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f*<\/strong><strong style=\"font-size: 1rem;\">\/<\/strong><\/p>\n<div>\/*\u4f7f\u7528<code>substring<\/code>\uff0c<code>'..$'<\/code> \u4e2d\u4e24\u4e2a\u70b9\u4ee3\u8868\u4e24\u4e2a\u5b57\u6bcd\uff0c\u7b26\u53f7$\u8868\u793a\u4ece\u6700\u540e\u5f00\u59cb\u8ba1\u7b97*\/<\/div>\n<div>\u00a0<\/div>\n<div><code>SELECT first_name FROM<\/code><br \/>\n<code>(SELECT first_name,substring(first_name,'..$' ) AS e FROM name<\/code><br \/>\n<code>ORDER BY e,first_name) AS c<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\/*\u4f7f\u7528<code>RIGHT<\/code>*\/<\/div>\n<div>\u00a0<\/div>\n<\/div>\n<div><code>SELECT first_name FROM name ORDER BY RIGHT(first_name,2);<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<hr \/>\n<div>\u00a0<\/div>\n<div>To be continued&#8230;<\/div>\n<div>\u00a0<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u4f7f\u7528EXCEL\u5904\u7406\u6570\u636e\u66f4\u65b9\u4fbf\u548c\u9ad8\u6548\u3002 \u4f46\u82e5\u662f\u6570\u636e\u91cf\u5f88\u5927\uff0c\u6216\u662f\u9700\u8981\u5bf9\u8f83\u591a\u6570\u636e\u8fdb\u884c\u590d\u6742\u7684\u5904\u7406\uff0cSQ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.beneplot.com\/?p=863\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL\u8bed\u53e5\u8bb0\u5f55 - Beneplot\" \/>\n<meta property=\"og:description\" content=\"\u5f53\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u4f7f\u7528EXCEL\u5904\u7406\u6570\u636e\u66f4\u65b9\u4fbf\u548c\u9ad8\u6548\u3002 \u4f46\u82e5\u662f\u6570\u636e\u91cf\u5f88\u5927\uff0c\u6216\u662f\u9700\u8981\u5bf9\u8f83\u591a\u6570\u636e\u8fdb\u884c\u590d\u6742\u7684\u5904\u7406\uff0cSQ [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.beneplot.com\/?p=863\" \/>\n<meta property=\"og:site_name\" content=\"Beneplot\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-18T05:48:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-15T02:51:54+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.beneplot.com\/#website\",\"url\":\"https:\/\/www.beneplot.com\/\",\"name\":\"Beneplot\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.beneplot.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.beneplot.com\/?p=863#webpage\",\"url\":\"https:\/\/www.beneplot.com\/?p=863\",\"name\":\"SQL\\u8bed\\u53e5\\u8bb0\\u5f55 - Beneplot\",\"isPartOf\":{\"@id\":\"https:\/\/www.beneplot.com\/#website\"},\"datePublished\":\"2017-11-18T05:48:18+00:00\",\"dateModified\":\"2021-07-15T02:51:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.beneplot.com\/#\/schema\/person\/ea14f85ae789ceaaa712ceee1dd1f95b\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.beneplot.com\/?p=863\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.beneplot.com\/#\/schema\/person\/ea14f85ae789ceaaa712ceee1dd1f95b\",\"name\":\"beneplot\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.beneplot.com\/#personlogo\",\"inLanguage\":\"zh-CN\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15d96ce801cfddbd59ef2b0d986cd9b1?s=96&r=g\",\"caption\":\"beneplot\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/863"}],"collection":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=863"}],"version-history":[{"count":23,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/863\/revisions"}],"predecessor-version":[{"id":961,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/863\/revisions\/961"}],"wp:attachment":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}