{"id":305,"date":"2022-06-14T13:22:37","date_gmt":"2022-06-14T05:22:37","guid":{"rendered":"https:\/\/frogvps.com\/?p=305"},"modified":"2022-06-14T13:22:38","modified_gmt":"2022-06-14T05:22:38","slug":"mysql-dql-%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/frogvps.com\/?p=305","title":{"rendered":"MYSQL DQL \u8bed\u53e5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5355\u8868\u64cd\u4f5c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n&#91;ALL | DISTINCT | DISTINCTROW ]\n&#91;SQL_CACHE | SQL_NO_CACHE]\nselect_expr &#91;, select_expr ...]\n&#91;FROM table_references\n&#91;WHERE where_condition]\n&#91;GROUP BY {col_name | expr | position}\n&#91;ASC | DESC], ... &#91;WITH ROLLUP]]\n&#91;HAVING where_condition]\n&#91;ORDER BY {col_name | expr | position}\n&#91;ASC | DESC], ...]\n&#91;LIMIT {&#91;offset,] row_count | row_count OFFSET offset}]\n&#91;FOR UPDATE | LOCK IN SHARE MODE]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b57\u6bb5\u663e\u793a\u53ef\u4ee5\u4f7f\u7528\u522b\u540d\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">col1 AS alias1, col2 AS alias2, \u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name \u59d3\u540d,age \u5e74\u9f84 FROM students;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>WHERE\u5b50\u53e5\uff0c\u6307\u660e\u8fc7\u6ee4\u6761\u4ef6\u4ee5\u5b9e\u73b0&#8221;\u9009\u62e9&#8221;\u7684\u529f\u80fd\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e03\u5c14\u578b\u8868\u8fbe\u5f0f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b97\u672f\u64cd\u4f5c\u7b26\uff1a+, -, *, \/, %<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u8f83\u64cd\u4f5c\u7b26\uff1a=, &lt;=&gt;\uff08\u76f8\u7b49\u6216\u90fd\u4e3a\u7a7a\uff09, &lt;&gt;, !=(\u975e\u6807\u51c6SQL), &gt;, &gt;=, &lt;, &lt;=<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8303\u4f8b\u67e5\u8be2\uff1aBETWEEN min_num AND max_num<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fde\u7eed\u7684\u67e5\u8be2\uff1aIN (element1, element2, \u2026)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a7a\u67e5\u8be2\uff1aIS NULL, IS NOT NULL<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">DISTINCT \u53bb\u9664\u91cd\u590d\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DISTINCT gender FROM students;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6a21\u7cca\u67e5\u8be2: LIKE<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RLIKE\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7d22\u5f15\u5931\u6548\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">REGEXP\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u53ef\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e66\u5199\u6a21\u5f0f\uff0c\u540c\u4e0a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u903b\u8f91\u64cd\u4f5c\u7b26\uff1aNOT\uff0cAND\uff0cOR\uff0cXOR<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>GROUP BY\uff0c\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u628a\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c&#8221;\u5206\u7ec4&#8221;\u4ee5\u7528\u4e8e\u505a&#8221;\u805a\u5408&#8221;\u8fd0\u7b97\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u805a\u5408\u51fd\u6570\uff1acount()\uff0csum()\uff0cmax()\uff0cmin()\uff0cavg()\uff0c\u4e0d\u5bf9null\u7edf\u8ba1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">HAVING: \u5bf9\u5206\u7ec4\u805a\u5408\u8fd0\u7b97\u540e\u7684\u7ed3\u679c\u6307\u5b9a\u8fc7\u6ee4\u6761\u4ef6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u65e6\u5206\u7ec4 group by \uff0cselect\u8bed\u53e5\u540e\u53ea\u8ddf\u5206\u7ec4\u7684\u5b57\u6bb5\uff0c\u805a\u5408\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ORDER BY: \u6839\u636e\u6307\u5b9a\u7684\u5b57\u6bb5\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5347\u5e8f\uff1aASC<br>\u964d\u5e8f\uff1aDESC<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>LIMIT [[offset,]row_count]\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u67e5\u8be2\u7684\u7ed3\u679c\u8fdb\u884c\u8f93\u51fa\u884c\u6570\u6570\u91cf\u9650\u5236,\u8df3\u8fc7offset,\u663e\u793arow_count\u884c,offset\u9ed8\u4e3a\u503c\u4e3a0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5bf9\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u6570\u636e\u8bf7\u6c42\u65bd\u52a0&#8221;\u9501&#8221;\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">FOR UPDATE: \u5199\u9501\uff0c\u72ec\u5360\u6216\u6392\u5b83\u9501\uff0c\u53ea\u6709\u4e00\u4e2a\u8bfb\u548c\u5199\u64cd\u4f5c<br>LOCK IN SHARE MODE: \u8bfb\u9501\uff0c\u5171\u4eab\u9501\uff0c\u540c\u65f6\u591a\u4e2a\u8bfb\u64cd\u4f5c<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"878\" height=\"584\" src=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/06\/20220614a.jpg\" alt=\"\" class=\"wp-image-307\" srcset=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/06\/20220614a.jpg 878w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/06\/20220614a-300x200.jpg 300w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/06\/20220614a-768x511.jpg 768w\" sizes=\"auto, (max-width: 878px) 100vw, 878px\" \/><figcaption>\u591a\u8868\u67e5\u8be2\uff0c\u5373\u67e5\u8be2\u7ed3\u679c\u6765\u81ea\u4e8e\u591a\u5f20\u8868<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b50\u67e5\u8be2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u67e5\u8be2 subquery \u5373SQL\u8bed\u53e5\u8c03\u7528\u53e6\u4e00\u4e2aSELECT\u5b50\u53e5\uff0c\u53ef\u4ee5\u662f\u5bf9\u540c\u4e00\u5f20\u8868\uff0c\u4e5f\u53ef\u4ee5\u662f\u5bf9\u4e0d\u540c\u8868\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u56db\u79cd\u5e38\u89c1\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.\u7528\u4e8e\u6bd4\u8f83\u8868\u8fbe\u5f0f\u4e2d\u7684\u5b50\u67e5\u8be2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name,age FROM students WHERE age&gt;(SELECT avg(age) FROM teachers);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2.\u7528\u4e8eIN\u4e2d\u7684\u5b50\u67e5\u8be2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name,age FROM students WHERE age IN (SELECT age FROM teachers);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3.\u7528\u4e8eEXISTS \u548c Not EXISTS<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">EXISTS\u5b50\u53e5\u6839\u636e\u5176\u5185\u67e5\u8be2\u8bed\u53e5\u7684\u7ed3\u679c\u96c6\u7a7a\u6216\u8005\u975e\u7a7a\uff0c\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from students s where EXISTS (select * from teachers t where s.teacherid=t.tid);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4.\u7528\u4e8eFROM\u5b50\u53e5\u4e2d\u7684\u5b50\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select stuid,name,age from students where age &gt; (select avg(age) from students);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8054\u5408\u67e5\u8be2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8054\u5408\u67e5\u8be2 Union \u5b9e\u73b0\u7684\u6761\u4ef6\uff0c\u591a\u4e2a\u8868\u7684\u5b57\u6bb5\u6570\u91cf\u76f8\u540c\uff0c\u5b57\u6bb5\u540d\u548c\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4e0d\u540c\uff0c\u4f46\u4e00\u822c\u6570\u636e\u7c7b\u578b\u662f\u76f8\u540c\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name,age FROM students UNION SELECT name,age FROM teachers;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ea4\u53c9\u8fde\u63a5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">cross join \u5373\u591a\u8868\u7684\u8bb0\u5f55\u4e4b\u95f4\u505a\u7b1b\u5361\u5c14\u4e58\u79ef\u7ec4\u5408\uff0c\u5e76\u4e14\u591a\u4e2a\u8868\u7684\u5217\u6a2a\u5411\u5408\u5e76\u76f8\u52a0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982: \u7b2c\u4e00\u4e2a\u88683\u884c4\u5217,\u7b2c\u4e8c\u4e2a\u88685\u884c6\u5217,cross join\u540e\u7684\u7ed3\u679c\u4e3a3*5=15\u884c,4+6=10\u5217\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * from students CROSS JOIN teachers;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5185\u8fde\u63a5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">inner join \u5185\u8fde\u63a5\u53d6\u591a\u4e2a\u8868\u7684\u4ea4\u96c6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * from students AS s inner join teachers AS t ON s.teacherid=t.tid;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5de6\u548c\u53f3\u5916\u8fde\u63a5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5de6\u8fde\u63a5: \u4ee5\u5de6\u8868\u4e3a\u4e3b\u6839\u636e\u6761\u4ef6\u67e5\u8be2\u53f3\u8868\u6570\u636e\ufe50\u5982\u679c\u6839\u636e\u6761\u4ef6\u67e5\u8be2\u53f3\u8868\u6570\u636e\u4e0d\u5b58\u5728\u4f7f\u7528null\u503c\u586b\u5145\u3002<br>\u53f3\u8fde\u63a5: \u4ee5\u53f3\u8868\u4e3a\u4e3b\u6839\u636e\u6761\u4ef6\u67e5\u8be2\u5de6\u8868\u6570\u636e\ufe50\u5982\u679c\u6839\u636e\u6761\u4ef6\u67e5\u8be2\u5de6\u8868\u6570\u636e\u4e0d\u5b58\u5728\u4f7f\u7528null\u503c\u586b\u5145\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5de6\u5916\u8fde\u63a5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT s.id,s.name,s.age,s.teacherid,t.tid,t.name,t.age FROM students AS s LEFT OUTER JOIN teachers AS t ON s.teacherid=t.tid;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53f3\u5916\u8fde\u63a5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * from students s RIGHT OUTER JOIN teachers t ON s.teacherid=t.tid;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u8fde\u63a5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8868\u81ea\u8eab\u8fde\u63a5\u81ea\u8eab\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5047\u8bbe\u521b\u5efa\u4e00\u5f20\u8868\u683c\uff0c\u91cc\u9762\u6709id\uff0c\u59d3\u540d\uff0c\u548cleader\u7684id\nCREATE TABLE emp(id smallint,name varchar(20),leaderid smallint);\n#\u968f\u4fbf\u63d2\u51654\u6761\u8bb0\u5f55\nINSERT emp values(1,'frog',null),(2,'cat',1),(3,'mice',2),(4,'worm',1);\n#\u7528\u81ea\u8fde\u63a5\u67e5\u8be2\u6bcf\u4e2a\u4ebaleader\u7684\u540d\u5b57\nSELECT e.name,IFNULL(l.name,'\u65e0') FROM emp AS e LEFT JOIN emp AS l ON e.leaderid=l.id;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">SELECT \u8bed\u53e5\u5904\u7406\u7684\u987a\u5e8f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM Clause --&gt; WHERE Clause --&gt; GROUP BY --&gt; HAVING Clause --&gt;SELECT --&gt; ORDER BY --&gt; LIMIT<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u8868\u64cd\u4f5c \u8bed\u6cd5\uff1a \u5b57\u6bb5\u663e\u793a\u53ef\u4ee5\u4f7f\u7528\u522b\u540d\uff1a col1 AS alias1, col2 AS alias2, \u2026 WHERE\u5b50\u53e5\uff0c\u6307\u660e\u8fc7\u6ee4\u6761\u4ef6\u4ee5\u5b9e\u73b0&#8221;\u9009\u62e9&#8221;\u7684\u529f\u80fd\uff1a \u5e03\u5c14\u578b\u8868\u8fbe\u5f0f \u7b97\u672f\u64cd\u4f5c\u7b26\uff1a+, -, *, \/, % &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[36],"topic":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-35","tag-mysql"],"_links":{"self":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=305"}],"version-history":[{"count":2,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":308,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions\/308"}],"wp:attachment":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=305"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}