{"id":649,"date":"2024-06-14T15:24:40","date_gmt":"2024-06-14T07:24:40","guid":{"rendered":"https:\/\/www.muxuetianyin.cn\/?p=649"},"modified":"2024-06-14T15:24:41","modified_gmt":"2024-06-14T07:24:41","slug":"%e9%ab%98%e9%a2%91-sql-50-%e9%a2%98%ef%bc%88%e5%9f%ba%e7%a1%80%e7%89%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.muxuetianyin.cn\/?p=649","title":{"rendered":"\u9ad8\u9891 SQL 50 \u9898\uff08\u57fa\u7840\u7248\uff09"},"content":{"rendered":"\n<h2>1.\u53ef\u56de\u6536\u4e14\u4f4e\u8102\u7684\u4ea7\u54c1<\/h2>\n\n\n\n<p>\u8868\uff1a<code>Products<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+---------+<br>&nbsp;| Column Name | Type &nbsp;  |<br>&nbsp;+-------------+---------+<br>&nbsp;| product_id  | int &nbsp; &nbsp; |<br>&nbsp;| low_fats &nbsp;  | enum &nbsp;  |<br>&nbsp;| recyclable  | enum &nbsp;  |<br>&nbsp;+-------------+---------+<br>&nbsp;product_id \u662f\u8be5\u8868\u7684\u4e3b\u952e\uff08\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\uff09\u3002<br>&nbsp;low_fats \u662f\u679a\u4e3e\u7c7b\u578b\uff0c\u53d6\u503c\u4e3a\u4ee5\u4e0b\u4e24\u79cd ('Y', 'N')\uff0c\u5176\u4e2d 'Y' \u8868\u793a\u8be5\u4ea7\u54c1\u662f\u4f4e\u8102\u4ea7\u54c1\uff0c'N' \u8868\u793a\u4e0d\u662f\u4f4e\u8102\u4ea7\u54c1\u3002<br>&nbsp;recyclable \u662f\u679a\u4e3e\u7c7b\u578b\uff0c\u53d6\u503c\u4e3a\u4ee5\u4e0b\u4e24\u79cd ('Y', 'N')\uff0c\u5176\u4e2d 'Y' \u8868\u793a\u8be5\u4ea7\u54c1\u53ef\u56de\u6536\uff0c\u800c 'N' \u8868\u793a\u4e0d\u53ef\u56de\u6536\u3002<\/pre>\n\n\n\n<p>\u7f16\u5199\u89e3\u51b3\u65b9\u6848\u627e\u51fa\u65e2\u662f\u4f4e\u8102\u53c8\u662f\u53ef\u56de\u6536\u7684\u4ea7\u54c1\u7f16\u53f7\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c <strong>\u65e0\u987a\u5e8f\u8981\u6c42<\/strong> \u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Products \u8868\uff1a<br>&nbsp;+-------------+----------+------------+<br>&nbsp;| product_id  | low_fats | recyclable |<br>&nbsp;+-------------+----------+------------+<br>&nbsp;| 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Y &nbsp; &nbsp; &nbsp;  | N &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Y &nbsp; &nbsp; &nbsp;  | Y &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | N &nbsp; &nbsp; &nbsp;  | Y &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Y &nbsp; &nbsp; &nbsp;  | Y &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | N &nbsp; &nbsp; &nbsp;  | N &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+-------------+----------+------------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+-------------+<br>&nbsp;| product_id  |<br>&nbsp;+-------------+<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;+-------------+<br>&nbsp;\u89e3\u91ca\uff1a<br>&nbsp;\u53ea\u6709\u4ea7\u54c1 id \u4e3a 1 \u548c 3 \u7684\u4ea7\u54c1\uff0c\u65e2\u662f\u4f4e\u8102\u53c8\u662f\u53ef\u56de\u6536\u7684\u4ea7\u54c1\u3002<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select product_id from Products where  low_fats = 'Y' and recyclable  = 'Y'<\/pre>\n\n\n\n<h2>2.\u5bfb\u627e\u7528\u6237\u63a8\u8350\u4eba<\/h2>\n\n\n\n<p>\u8868: <code>Customer<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+---------+<br>&nbsp;| Column Name | Type &nbsp;  |<br>&nbsp;+-------------+---------+<br>&nbsp;| id &nbsp; &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| name &nbsp; &nbsp; &nbsp;  | varchar |<br>&nbsp;| referee_id  | int &nbsp; &nbsp; |<br>&nbsp;+-------------+---------+<br>&nbsp;\u5728 SQL \u4e2d\uff0cid \u662f\u8be5\u8868\u7684\u4e3b\u952e\u5217\u3002<br>&nbsp;\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u8868\u793a\u4e00\u4e2a\u5ba2\u6237\u7684 id\u3001\u59d3\u540d\u4ee5\u53ca\u63a8\u8350\u4ed6\u4eec\u7684\u5ba2\u6237\u7684 id\u3002<\/pre>\n\n\n\n<p>\u627e\u51fa\u90a3\u4e9b <strong>\u6ca1\u6709\u88ab<\/strong> <code>id = 2<\/code> \u7684\u5ba2\u6237 <strong>\u63a8\u8350<\/strong> \u7684\u5ba2\u6237\u7684\u59d3\u540d\u3002<\/p>\n\n\n\n<p>\u4ee5 <strong>\u4efb\u610f\u987a\u5e8f<\/strong> \u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a <br>&nbsp;Customer \u8868:<br>&nbsp;+----+------+------------+<br>&nbsp;| id | name | referee_id |<br>&nbsp;+----+------+------------+<br>&nbsp;| 1  | Will | null &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 2  | Jane | null &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 3  | Alex | 2 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 4  | Bill | null &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 5  | Zack | 1 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 6  | Mark | 2 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+----+------+------------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+------+<br>&nbsp;| name |<br>&nbsp;+------+<br>&nbsp;| Will |<br>&nbsp;| Jane |<br>&nbsp;| Bill |<br>&nbsp;| Zack |<br>&nbsp;+------+<\/pre>\n\n\n\n<p>\u6267\u884c\u8bed\u53e5<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select NAME from<br>&nbsp;Customer<br>&nbsp;WHERE  referee_id !=2 or referee_id is null;<br>&nbsp;\u200b<br>&nbsp;SELECT name FROM Customer WHERE referee_id &lt;&gt; 2 OR referee_id IS NULL;<br>&nbsp;\u200b<br>&nbsp;select name from Customer where  id &nbsp;not in  (select id from Customer where referee_id  = 2 )<\/pre>\n\n\n\n<h2>3.\u5927\u7684\u56fd\u5bb6<\/h2>\n\n\n\n<p><code>World<\/code> \u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+---------+<br>&nbsp;| Column Name | Type &nbsp;  |<br>&nbsp;+-------------+---------+<br>&nbsp;| name &nbsp; &nbsp; &nbsp;  | varchar |<br>&nbsp;| continent &nbsp; | varchar |<br>&nbsp;| area &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| population  | int &nbsp; &nbsp; |<br>&nbsp;| gdp &nbsp; &nbsp; &nbsp; &nbsp; | bigint  |<br>&nbsp;+-------------+---------+<br>&nbsp;name \u662f\u8be5\u8868\u7684\u4e3b\u952e\uff08\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\uff09\u3002<br>&nbsp;\u8fd9\u5f20\u8868\u7684\u6bcf\u4e00\u884c\u63d0\u4f9b\uff1a\u56fd\u5bb6\u540d\u79f0\u3001\u6240\u5c5e\u5927\u9646\u3001\u9762\u79ef\u3001\u4eba\u53e3\u548c GDP \u503c\u3002<\/pre>\n\n\n\n<p>\u5982\u679c\u4e00\u4e2a\u56fd\u5bb6\u6ee1\u8db3\u4e0b\u8ff0\u4e24\u4e2a\u6761\u4ef6\u4e4b\u4e00\uff0c\u5219\u8ba4\u4e3a\u8be5\u56fd\u662f <strong>\u5927\u56fd<\/strong> \uff1a<\/p>\n\n\n\n<ul><li>\u9762\u79ef\u81f3\u5c11\u4e3a 300 \u4e07\u5e73\u65b9\u516c\u91cc\uff08\u5373\uff0c<code>3000000 km2<\/code>\uff09\uff0c\u6216\u8005<\/li><li>\u4eba\u53e3\u81f3\u5c11\u4e3a 2500 \u4e07\uff08\u5373 <code>25000000<\/code>\uff09<\/li><\/ul>\n\n\n\n<p>\u7f16\u5199\u89e3\u51b3\u65b9\u6848\u627e\u51fa <strong>\u5927\u56fd<\/strong> \u7684\u56fd\u5bb6\u540d\u79f0\u3001\u4eba\u53e3\u548c\u9762\u79ef\u3002<\/p>\n\n\n\n<p>\u6309 <strong>\u4efb\u610f\u987a\u5e8f<\/strong> \u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;World \u8868\uff1a<br>&nbsp;+-------------+-----------+---------+------------+--------------+<br>&nbsp;| name &nbsp; &nbsp; &nbsp;  | continent | area &nbsp;  | population | gdp &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+-------------+-----------+---------+------------+--------------+<br>&nbsp;| Afghanistan | Asia &nbsp; &nbsp;  | 652230  | 25500100 &nbsp; | 20343000000  |<br>&nbsp;| Albania &nbsp; &nbsp; | Europe &nbsp;  | 28748 &nbsp; | 2831741 &nbsp;  | 12960000000  |<br>&nbsp;| Algeria &nbsp; &nbsp; | Africa &nbsp;  | 2381741 | 37100000 &nbsp; | 188681000000 |<br>&nbsp;| Andorra &nbsp; &nbsp; | Europe &nbsp;  | 468 &nbsp; &nbsp; | 78115 &nbsp; &nbsp;  | 3712000000 &nbsp; |<br>&nbsp;| Angola &nbsp; &nbsp;  | Africa &nbsp;  | 1246700 | 20609294 &nbsp; | 100990000000 |<br>&nbsp;+-------------+-----------+---------+------------+--------------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+-------------+------------+---------+<br>&nbsp;| name &nbsp; &nbsp; &nbsp;  | population | area &nbsp;  |<br>&nbsp;+-------------+------------+---------+<br>&nbsp;| Afghanistan | 25500100 &nbsp; | 652230  |<br>&nbsp;| Algeria &nbsp; &nbsp; | 37100000 &nbsp; | 2381741 |<br>&nbsp;+-------------+------------+---------+<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select name,population,area from World where area &gt;= 3000000 or population &gt;= 25000000<\/pre>\n\n\n\n<h2>4.\u6587\u7ae0\u6d4f\u89c8 I<\/h2>\n\n\n\n<p><code>Views<\/code> \u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+---------------+---------+<br>&nbsp;| Column Name &nbsp; | Type &nbsp;  |<br>&nbsp;+---------------+---------+<br>&nbsp;| article_id &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| author_id &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| viewer_id &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| view_date &nbsp; &nbsp; | date &nbsp;  |<br>&nbsp;+---------------+---------+<br>&nbsp;\u6b64\u8868\u53ef\u80fd\u4f1a\u5b58\u5728\u91cd\u590d\u884c\u3002\uff08\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728 SQL \u4e2d\u8fd9\u4e2a\u8868\u6ca1\u6709\u4e3b\u952e\uff09<br>&nbsp;\u6b64\u8868\u7684\u6bcf\u4e00\u884c\u90fd\u8868\u793a\u67d0\u4eba\u5728\u67d0\u5929\u6d4f\u89c8\u4e86\u67d0\u4f4d\u4f5c\u8005\u7684\u67d0\u7bc7\u6587\u7ae0\u3002<br>&nbsp;\u8bf7\u6ce8\u610f\uff0c\u540c\u4e00\u4eba\u7684 author_id \u548c viewer_id \u662f\u76f8\u540c\u7684\u3002<\/pre>\n\n\n\n<p>\u8bf7\u67e5\u8be2\u51fa\u6240\u6709\u6d4f\u89c8\u8fc7\u81ea\u5df1\u6587\u7ae0\u7684\u4f5c\u8005<\/p>\n\n\n\n<p>\u7ed3\u679c\u6309\u7167 <code>id<\/code> \u5347\u5e8f\u6392\u5217\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u7684\u683c\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Views \u8868\uff1a<br>&nbsp;+------------+-----------+-----------+------------+<br>&nbsp;| article_id | author_id | viewer_id | view_date  |<br>&nbsp;+------------+-----------+-----------+------------+<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | 3 &nbsp; &nbsp; &nbsp; &nbsp; | 5 &nbsp; &nbsp; &nbsp; &nbsp; | 2019-08-01 |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | 3 &nbsp; &nbsp; &nbsp; &nbsp; | 6 &nbsp; &nbsp; &nbsp; &nbsp; | 2019-08-02 |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | 7 &nbsp; &nbsp; &nbsp; &nbsp; | 7 &nbsp; &nbsp; &nbsp; &nbsp; | 2019-08-01 |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | 7 &nbsp; &nbsp; &nbsp; &nbsp; | 6 &nbsp; &nbsp; &nbsp; &nbsp; | 2019-08-02 |<br>&nbsp;| 4 &nbsp; &nbsp; &nbsp; &nbsp;  | 7 &nbsp; &nbsp; &nbsp; &nbsp; | 1 &nbsp; &nbsp; &nbsp; &nbsp; | 2019-07-22 |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; &nbsp;  | 4 &nbsp; &nbsp; &nbsp; &nbsp; | 4 &nbsp; &nbsp; &nbsp; &nbsp; | 2019-07-21 |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; &nbsp;  | 4 &nbsp; &nbsp; &nbsp; &nbsp; | 4 &nbsp; &nbsp; &nbsp; &nbsp; | 2019-07-21 |<br>&nbsp;+------------+-----------+-----------+------------+<br>&nbsp;\u200b<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+------+<br>&nbsp;| id &nbsp; |<br>&nbsp;+------+<br>&nbsp;| 4 &nbsp;  |<br>&nbsp;| 7 &nbsp;  |<br>&nbsp;+------+<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select distinct author_id as id &nbsp;from  Views &nbsp;where author_id = viewer_id order by author_id asc<br>&nbsp;\u200b<br>&nbsp;select distinct author_id as id &nbsp;from  Views &nbsp;where author_id = viewer_id order by author_id<br>&nbsp;\u200b<\/pre>\n\n\n\n<h2>5.\u65e0\u6548\u63a8\u6587<\/h2>\n\n\n\n<p>\u8868\uff1a<code>Tweets<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+----------------+---------+<br>&nbsp;| Column Name &nbsp;  | Type &nbsp;  |<br>&nbsp;+----------------+---------+<br>&nbsp;| tweet_id &nbsp; &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| content &nbsp; &nbsp; &nbsp;  | varchar |<br>&nbsp;+----------------+---------+<br>&nbsp;\u5728 SQL \u4e2d\uff0ctweet_id \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002<br>&nbsp;\u8fd9\u4e2a\u8868\u5305\u542b\u67d0\u793e\u4ea4\u5a92\u4f53 App \u4e2d\u6240\u6709\u7684\u63a8\u6587\u3002<\/pre>\n\n\n\n<p>\u67e5\u8be2\u6240\u6709\u65e0\u6548\u63a8\u6587\u7684\u7f16\u53f7\uff08ID\uff09\u3002\u5f53\u63a8\u6587\u5185\u5bb9\u4e2d\u7684\u5b57\u7b26\u6570<strong>\u4e25\u683c\u5927\u4e8e<\/strong> <code>15<\/code> \u65f6\uff0c\u8be5\u63a8\u6587\u662f\u65e0\u6548\u7684\u3002<\/p>\n\n\n\n<p>\u4ee5<strong>\u4efb\u610f\u987a\u5e8f<\/strong>\u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Tweets \u8868\uff1a<br>&nbsp;+----------+----------------------------------+<br>&nbsp;| tweet_id | content &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+----------+----------------------------------+<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp;  | Vote for Biden &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp;  | Let us make America great again! |<br>&nbsp;+----------+----------------------------------+<br>&nbsp;\u200b<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+----------+<br>&nbsp;| tweet_id |<br>&nbsp;+----------+<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+----------+<br>&nbsp;\u89e3\u91ca\uff1a<br>&nbsp;\u63a8\u6587 1 \u7684\u957f\u5ea6 length = 14\u3002\u8be5\u63a8\u6587\u662f\u6709\u6548\u7684\u3002<br>&nbsp;\u63a8\u6587 2 \u7684\u957f\u5ea6 length = 32\u3002\u8be5\u63a8\u6587\u662f\u65e0\u6548\u7684\u3002<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select tweet_id from Tweets where  length(content) &gt; 15;<br>&nbsp;\u200b<br>&nbsp;SELECT tweet_id<br>&nbsp;FROM Tweets<br>&nbsp;WHERE char_length(content) &gt; 15<\/pre>\n\n\n\n<h2>6.\u4f7f\u7528\u552f\u4e00\u8868\u793a\u4ee3\u66ff\u5458\u5de5id<\/h2>\n\n\n\n<p><code>Employees<\/code> \u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+---------------+---------+<br>&nbsp;| Column Name &nbsp; | Type &nbsp;  |<br>&nbsp;+---------------+---------+<br>&nbsp;| id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| name &nbsp; &nbsp; &nbsp; &nbsp;  | varchar |<br>&nbsp;+---------------+---------+<br>&nbsp;\u5728 SQL \u4e2d\uff0cid \u662f\u8fd9\u5f20\u8868\u7684\u4e3b\u952e\u3002<br>&nbsp;\u8fd9\u5f20\u8868\u7684\u6bcf\u4e00\u884c\u5206\u522b\u4ee3\u8868\u4e86\u67d0\u516c\u53f8\u5176\u4e2d\u4e00\u4f4d\u5458\u5de5\u7684\u540d\u5b57\u548c ID \u3002<\/pre>\n\n\n\n<p><code>EmployeeUNI<\/code> \u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+---------------+---------+<br>&nbsp;| Column Name &nbsp; | Type &nbsp;  |<br>&nbsp;+---------------+---------+<br>&nbsp;| id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| unique_id &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;+---------------+---------+<br>&nbsp;\u5728 SQL \u4e2d\uff0c(id, unique_id) \u662f\u8fd9\u5f20\u8868\u7684\u4e3b\u952e\u3002<br>&nbsp;\u8fd9\u5f20\u8868\u7684\u6bcf\u4e00\u884c\u5305\u542b\u4e86\u8be5\u516c\u53f8\u67d0\u4f4d\u5458\u5de5\u7684 ID \u548c\u4ed6\u7684\u552f\u4e00\u6807\u8bc6\u7801\uff08unique ID\uff09\u3002<\/pre>\n\n\n\n<p>\u5c55\u793a\u6bcf\u4f4d\u7528\u6237\u7684 <strong>\u552f\u4e00\u6807\u8bc6\u7801\uff08unique ID \uff09<\/strong>\uff1b\u5982\u679c\u67d0\u4f4d\u5458\u5de5\u6ca1\u6709\u552f\u4e00\u6807\u8bc6\u7801\uff0c\u4f7f\u7528 null \u586b\u5145\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4ee5 <strong>\u4efb\u610f<\/strong> \u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u7684\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Employees \u8868:<br>&nbsp;+----+----------+<br>&nbsp;| id | name &nbsp; &nbsp; |<br>&nbsp;+----+----------+<br>&nbsp;| 1  | Alice &nbsp;  |<br>&nbsp;| 7  | Bob &nbsp; &nbsp;  |<br>&nbsp;| 11 | Meir &nbsp; &nbsp; |<br>&nbsp;| 90 | Winston  |<br>&nbsp;| 3  | Jonathan |<br>&nbsp;+----+----------+<br>&nbsp;EmployeeUNI \u8868:<br>&nbsp;+----+-----------+<br>&nbsp;| id | unique_id |<br>&nbsp;+----+-----------+<br>&nbsp;| 3  | 1 &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 11 | 2 &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 90 | 3 &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;+----+-----------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+-----------+----------+<br>&nbsp;| unique_id | name &nbsp; &nbsp; |<br>&nbsp;+-----------+----------+<br>&nbsp;| null &nbsp; &nbsp;  | Alice &nbsp;  |<br>&nbsp;| null &nbsp; &nbsp;  | Bob &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp; | Meir &nbsp; &nbsp; |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; &nbsp; | Winston  |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp; | Jonathan |<br>&nbsp;+-----------+----------+<br>&nbsp;\u89e3\u91ca\uff1a<br>&nbsp;Alice and Bob \u6ca1\u6709\u552f\u4e00\u6807\u8bc6\u7801, \u56e0\u6b64\u6211\u4eec\u4f7f\u7528 null \u66ff\u4ee3\u3002<br>&nbsp;Meir \u7684\u552f\u4e00\u6807\u8bc6\u7801\u662f 2 \u3002<br>&nbsp;Winston \u7684\u552f\u4e00\u6807\u8bc6\u7801\u662f 3 \u3002<br>&nbsp;Jonathan \u552f\u4e00\u6807\u8bc6\u7801\u662f 1 \u3002<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select unique_id,name &nbsp;from Employees e left join EmployeeUNI eu on e.id = eu.id &nbsp; &nbsp; <\/pre>\n\n\n\n<h2>7.\u4ea7\u54c1\u9500\u552e\u5206\u6790\u4e00<\/h2>\n\n\n\n<p>\u9500\u552e\u8868 <code>Sales<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+-------+<br>&nbsp;| Column Name | Type  |<br>&nbsp;+-------------+-------+<br>&nbsp;| sale_id &nbsp; &nbsp; | int &nbsp; |<br>&nbsp;| product_id  | int &nbsp; |<br>&nbsp;| year &nbsp; &nbsp; &nbsp;  | int &nbsp; |<br>&nbsp;| quantity &nbsp;  | int &nbsp; |<br>&nbsp;| price &nbsp; &nbsp; &nbsp; | int &nbsp; |<br>&nbsp;+-------------+-------+<br>&nbsp;(sale_id, year) \u662f\u9500\u552e\u8868 Sales \u7684\u4e3b\u952e\uff08\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u7684\u7ec4\u5408\uff09\u3002<br>&nbsp;product_id \u662f\u5173\u8054\u5230\u4ea7\u54c1\u8868 Product \u7684\u5916\u952e\uff08reference \u5217\uff09\u3002<br>&nbsp;\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u663e\u793a product_id \u5728\u67d0\u4e00\u5e74\u7684\u9500\u552e\u60c5\u51b5\u3002<br>&nbsp;\u6ce8\u610f: price \u8868\u793a\u6bcf\u5355\u4f4d\u4ef7\u683c\u3002<\/pre>\n\n\n\n<p>\u4ea7\u54c1\u8868 <code>Product<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+--------------+---------+<br>&nbsp;| Column Name  | Type &nbsp;  |<br>&nbsp;+--------------+---------+<br>&nbsp;| product_id &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| product_name | varchar |<br>&nbsp;+--------------+---------+<br>&nbsp;product_id \u662f\u8868\u7684\u4e3b\u952e\uff08\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\uff09\u3002<br>&nbsp;\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u8868\u793a\u6bcf\u79cd\u4ea7\u54c1\u7684\u4ea7\u54c1\u540d\u79f0\u3002<\/pre>\n\n\n\n<p>\u7f16\u5199\u89e3\u51b3\u65b9\u6848\uff0c\u4ee5\u83b7\u53d6 <code>Sales<\/code> \u8868\u4e2d\u6240\u6709 <code>sale_id<\/code> \u5bf9\u5e94\u7684 <code>product_name<\/code> \u4ee5\u53ca\u8be5\u4ea7\u54c1\u7684\u6240\u6709 <code>year<\/code> \u548c <code>price<\/code> \u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u8868 <strong>\u65e0\u987a\u5e8f\u8981\u6c42<\/strong> \u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u683c\u5f0f\u793a\u4f8b\u5982\u4e0b\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Sales \u8868\uff1a<br>&nbsp;+---------+------------+------+----------+-------+<br>&nbsp;| sale_id | product_id | year | quantity | price |<br>&nbsp;+---------+------------+------+----------+-------+ <br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; | 100 &nbsp; &nbsp; &nbsp;  | 2008 | 10 &nbsp; &nbsp; &nbsp; | 5000  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; | 100 &nbsp; &nbsp; &nbsp;  | 2009 | 12 &nbsp; &nbsp; &nbsp; | 5000  |<br>&nbsp;| 7 &nbsp; &nbsp; &nbsp; | 200 &nbsp; &nbsp; &nbsp;  | 2011 | 15 &nbsp; &nbsp; &nbsp; | 9000  |<br>&nbsp;+---------+------------+------+----------+-------+<br>&nbsp;Product \u8868\uff1a<br>&nbsp;+------------+--------------+<br>&nbsp;| product_id | product_name |<br>&nbsp;+------------+--------------+<br>&nbsp;| 100 &nbsp; &nbsp; &nbsp;  | Nokia &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 200 &nbsp; &nbsp; &nbsp;  | Apple &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 300 &nbsp; &nbsp; &nbsp;  | Samsung &nbsp; &nbsp;  |<br>&nbsp;+------------+--------------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+--------------+-------+-------+<br>&nbsp;| product_name | year  | price |<br>&nbsp;+--------------+-------+-------+<br>&nbsp;| Nokia &nbsp; &nbsp; &nbsp;  | 2008  | 5000  |<br>&nbsp;| Nokia &nbsp; &nbsp; &nbsp;  | 2009  | 5000  |<br>&nbsp;| Apple &nbsp; &nbsp; &nbsp;  | 2011  | 9000  |<br>&nbsp;+--------------+-------+-------+<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select product_name , year , price from Sales <br>&nbsp; s left join Product p on p.product_id = s.product_id ;<\/pre>\n\n\n\n<h2>8.\u8fdb\u5e97\u5374\u672a\u4ea4\u6613\u8fc7\u7684\u5ba2\u6237<\/h2>\n\n\n\n<p>\u8868\uff1a<code>Visits<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+---------+<br>&nbsp;| Column Name | Type &nbsp;  |<br>&nbsp;+-------------+---------+<br>&nbsp;| visit_id &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| customer_id | int &nbsp; &nbsp; |<br>&nbsp;+-------------+---------+<br>&nbsp;visit_id \u662f\u8be5\u8868\u4e2d\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u3002<br>&nbsp;\u8be5\u8868\u5305\u542b\u6709\u5173\u5149\u4e34\u8fc7\u8d2d\u7269\u4e2d\u5fc3\u7684\u987e\u5ba2\u7684\u4fe1\u606f\u3002<\/pre>\n\n\n\n<p>\u8868\uff1a<code>Transactions<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+----------------+---------+<br>&nbsp;| Column Name &nbsp;  | Type &nbsp;  |<br>&nbsp;+----------------+---------+<br>&nbsp;| transaction_id | int &nbsp; &nbsp; |<br>&nbsp;| visit_id &nbsp; &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| amount &nbsp; &nbsp; &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;+----------------+---------+<br>&nbsp;transaction_id \u662f\u8be5\u8868\u4e2d\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u3002<br>&nbsp;\u6b64\u8868\u5305\u542b visit_id \u671f\u95f4\u8fdb\u884c\u7684\u4ea4\u6613\u7684\u4fe1\u606f\u3002<\/pre>\n\n\n\n<p>\u6709\u4e00\u4e9b\u987e\u5ba2\u53ef\u80fd\u5149\u987e\u4e86\u8d2d\u7269\u4e2d\u5fc3\u4f46\u6ca1\u6709\u8fdb\u884c\u4ea4\u6613\u3002\u8bf7\u4f60\u7f16\u5199\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\uff0c\u6765\u67e5\u627e\u8fd9\u4e9b\u987e\u5ba2\u7684 ID \uff0c\u4ee5\u53ca\u4ed6\u4eec\u53ea\u5149\u987e\u4e0d\u4ea4\u6613\u7684\u6b21\u6570\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u4ee5 <strong>\u4efb\u4f55\u987a\u5e8f<\/strong> \u6392\u5e8f\u7684\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165:<br>&nbsp;Visits<br>&nbsp;+----------+-------------+<br>&nbsp;| visit_id | customer_id |<br>&nbsp;+----------+-------------+<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp;  | 23 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp;  | 9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 4 &nbsp; &nbsp; &nbsp;  | 30 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 5 &nbsp; &nbsp; &nbsp;  | 54 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 6 &nbsp; &nbsp; &nbsp;  | 96 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 7 &nbsp; &nbsp; &nbsp;  | 54 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 8 &nbsp; &nbsp; &nbsp;  | 54 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+----------+-------------+<br>&nbsp;Transactions<br>&nbsp;+----------------+----------+--------+<br>&nbsp;| transaction_id | visit_id | amount |<br>&nbsp;+----------------+----------+--------+<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | 5 &nbsp; &nbsp; &nbsp;  | 310 &nbsp;  |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | 5 &nbsp; &nbsp; &nbsp;  | 300 &nbsp;  |<br>&nbsp;| 9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | 5 &nbsp; &nbsp; &nbsp;  | 200 &nbsp;  |<br>&nbsp;| 12 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1 &nbsp; &nbsp; &nbsp;  | 910 &nbsp;  |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 2 &nbsp; &nbsp; &nbsp;  | 970 &nbsp;  |<br>&nbsp;+----------------+----------+--------+<br>&nbsp;\u8f93\u51fa:<br>&nbsp;+-------------+----------------+<br>&nbsp;| customer_id | count_no_trans |<br>&nbsp;+-------------+----------------+<br>&nbsp;| 54 &nbsp; &nbsp; &nbsp; &nbsp;  | 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 30 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 96 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+-------------+----------------+<br>&nbsp;\u89e3\u91ca:<br>&nbsp;ID = 23 \u7684\u987e\u5ba2\u66fe\u7ecf\u901b\u8fc7\u4e00\u6b21\u8d2d\u7269\u4e2d\u5fc3\uff0c\u5e76\u5728 ID = 12 \u7684\u8bbf\u95ee\u671f\u95f4\u8fdb\u884c\u4e86\u4e00\u7b14\u4ea4\u6613\u3002<br>&nbsp;ID = 9 \u7684\u987e\u5ba2\u66fe\u7ecf\u901b\u8fc7\u4e00\u6b21\u8d2d\u7269\u4e2d\u5fc3\uff0c\u5e76\u5728 ID = 13 \u7684\u8bbf\u95ee\u671f\u95f4\u8fdb\u884c\u4e86\u4e00\u7b14\u4ea4\u6613\u3002<br>&nbsp;ID = 30 \u7684\u987e\u5ba2\u66fe\u7ecf\u53bb\u8fc7\u8d2d\u7269\u4e2d\u5fc3\uff0c\u5e76\u4e14\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u4ea4\u6613\u3002<br>&nbsp;ID = 54 \u7684\u987e\u5ba2\u4e09\u5ea6\u9020\u8bbf\u4e86\u8d2d\u7269\u4e2d\u5fc3\u3002\u5728 2 \u6b21\u8bbf\u95ee\u4e2d\uff0c\u4ed6\u4eec\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u4ea4\u6613\uff0c\u5728 1 \u6b21\u8bbf\u95ee\u4e2d\uff0c\u4ed6\u4eec\u8fdb\u884c\u4e86 3 \u6b21\u4ea4\u6613\u3002<br>&nbsp;ID = 96 \u7684\u987e\u5ba2\u66fe\u7ecf\u53bb\u8fc7\u8d2d\u7269\u4e2d\u5fc3\uff0c\u5e76\u4e14\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u4ea4\u6613\u3002<br>&nbsp;\u5982\u6211\u4eec\u6240\u89c1\uff0cID \u4e3a 30 \u548c 96 \u7684\u987e\u5ba2\u4e00\u6b21\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u4ea4\u6613\u5c31\u53bb\u4e86\u8d2d\u7269\u4e2d\u5fc3\u3002\u987e\u5ba2 54 \u4e5f\u4e24\u6b21\u8bbf\u95ee\u4e86\u8d2d\u7269\u4e2d\u5fc3\u5e76\u4e14\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u4ea4\u6613\u3002<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select v.customer_id,count(v.customer_id) as count_no_trans from Visits v left join Transactions t on v.visit_id  = t.visit_id where t.transaction_id &nbsp;is null group by <br>&nbsp; &nbsp;  v.customer_id;<\/pre>\n\n\n\n<h2>9\u4e0a\u5347\u7684\u6e29\u5ea6*<\/h2>\n\n\n\n<p>\u8868\uff1a <code>Weather<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+---------------+---------+<br>&nbsp;| Column Name &nbsp; | Type &nbsp;  |<br>&nbsp;+---------------+---------+<br>&nbsp;| id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| recordDate &nbsp;  | date &nbsp;  |<br>&nbsp;| temperature &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;+---------------+---------+<br>&nbsp;id \u662f\u8be5\u8868\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u3002<br>&nbsp;\u6ca1\u6709\u5177\u6709\u76f8\u540c recordDate \u7684\u4e0d\u540c\u884c\u3002<br>&nbsp;\u8be5\u8868\u5305\u542b\u7279\u5b9a\u65e5\u671f\u7684\u6e29\u5ea6\u4fe1\u606f<\/pre>\n\n\n\n<p>\u7f16\u5199\u89e3\u51b3\u65b9\u6848\uff0c\u627e\u51fa\u4e0e\u4e4b\u524d\uff08\u6628\u5929\u7684\uff09\u65e5\u671f\u76f8\u6bd4\u6e29\u5ea6\u66f4\u9ad8\u7684\u6240\u6709\u65e5\u671f\u7684 <code>id<\/code> \u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c <strong>\u65e0\u987a\u5e8f\u8981\u6c42<\/strong> \u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u5b50\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Weather \u8868\uff1a<br>&nbsp;+----+------------+-------------+<br>&nbsp;| id | recordDate | Temperature |<br>&nbsp;+----+------------+-------------+<br>&nbsp;| 1  | 2015-01-01 | 10 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2  | 2015-01-02 | 25 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 3  | 2015-01-03 | 20 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 4  | 2015-01-04 | 30 &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+----+------------+-------------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+----+<br>&nbsp;| id |<br>&nbsp;+----+<br>&nbsp;| 2  |<br>&nbsp;| 4  |<br>&nbsp;+----+<br>&nbsp;\u89e3\u91ca\uff1a<br>&nbsp;2015-01-02 \u7684\u6e29\u5ea6\u6bd4\u524d\u4e00\u5929\u9ad8\uff0810 -&gt; 25\uff09<br>&nbsp;2015-01-04 \u7684\u6e29\u5ea6\u6bd4\u524d\u4e00\u5929\u9ad8\uff0820 -&gt; 30\uff09<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;date_add\u662f\u5bf9\u65e5\u671f\u7684\u589e\u52a0\uff0c\u5982\u679c\u5929\u6570\u4e3a\u8d1f\u6570\u65f6\uff0c\u5219\u8868\u793a\u5bf9\u65e5\u671f\u51cf\u5c11\uff0c<br>&nbsp;date_sub\u662f\u5bf9\u65e5\u671f\u7684\u51cf\u5c11\uff0c\u5982\u679c\u5929\u6570\u4e3a\u8d1f\u6570\u65f6\uff0c\u5219\u8868\u793a\u5bf9\u65e5\u671f\u589e\u52a0<br>&nbsp;\u200b<br>&nbsp;\u200b<br>&nbsp;SELECT <br>&nbsp; &nbsp;  t1.id<br>&nbsp;FROM <br>&nbsp; &nbsp;  Weather t1<br>&nbsp;JOIN <br>&nbsp;# \u8fde\u63a5\uff0ct1\u8868\u548ct2\u52a0\u4e00\u5929\u65f6\u95f4\u76f8\u540c\u7684\u65e5\u671f\uff0c\u7136\u540e\u5f97\u5230t1\u662f\u7b2c\u4e8c\u5929\u65f6\u95f4\uff0c\u7136\u540et1\u518d\u52a0\u6761\u4ef6\u662ft1\u300bt2\u5c31\u53ef\u4ee5\u63a8\u7b97\u51fa\u6765\u6bd4\u524d\u4e00\u5929\u9ad8\u7684id<br>&nbsp; &nbsp;  Weather t2 ON t1.recordDate = DATE_ADD(t2.recordDate, INTERVAL 1 DAY)<br>&nbsp;WHERE <br>&nbsp; &nbsp;  t1.temperature &gt; t2.temperature;<\/pre>\n\n\n\n<h2>10\u6bcf\u53f0\u673a\u5668\u8fdb\u7a0b\u7684\u5e73\u5747\u65f6\u95f4*<\/h2>\n\n\n\n<p>\u8868: <code>Activity<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+----------------+---------+<br>&nbsp;| Column Name &nbsp;  | Type &nbsp;  |<br>&nbsp;+----------------+---------+<br>&nbsp;| machine_id &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| process_id &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| activity_type  | enum &nbsp;  |<br>&nbsp;| timestamp &nbsp; &nbsp;  | float &nbsp; |<br>&nbsp;+----------------+---------+<br>&nbsp;\u8be5\u8868\u5c55\u793a\u4e86\u4e00\u5bb6\u5de5\u5382\u7f51\u7ad9\u7684\u7528\u6237\u6d3b\u52a8\u3002<br>&nbsp;(machine_id, process_id, activity_type) \u662f\u5f53\u524d\u8868\u7684\u4e3b\u952e\uff08\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u7684\u7ec4\u5408\uff09\u3002<br>&nbsp;machine_id \u662f\u4e00\u53f0\u673a\u5668\u7684ID\u53f7\u3002<br>&nbsp;process_id \u662f\u8fd0\u884c\u5728\u5404\u673a\u5668\u4e0a\u7684\u8fdb\u7a0bID\u53f7\u3002<br>&nbsp;activity_type \u662f\u679a\u4e3e\u7c7b\u578b ('start', 'end')\u3002<br>&nbsp;timestamp \u662f\u6d6e\u70b9\u7c7b\u578b,\u4ee3\u8868\u5f53\u524d\u65f6\u95f4(\u4ee5\u79d2\u4e3a\u5355\u4f4d)\u3002<br>&nbsp;'start' \u4ee3\u8868\u8be5\u8fdb\u7a0b\u5728\u8fd9\u53f0\u673a\u5668\u4e0a\u7684\u5f00\u59cb\u8fd0\u884c\u65f6\u95f4\u6233 , 'end' \u4ee3\u8868\u8be5\u8fdb\u7a0b\u5728\u8fd9\u53f0\u673a\u5668\u4e0a\u7684\u7ec8\u6b62\u8fd0\u884c\u65f6\u95f4\u6233\u3002<br>&nbsp;\u540c\u4e00\u53f0\u673a\u5668\uff0c\u540c\u4e00\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u5bf9\u5f00\u59cb\u65f6\u95f4\u6233\u548c\u7ed3\u675f\u65f6\u95f4\u6233\uff0c\u800c\u4e14\u5f00\u59cb\u65f6\u95f4\u6233\u6c38\u8fdc\u5728\u7ed3\u675f\u65f6\u95f4\u6233\u524d\u9762\u3002<\/pre>\n\n\n\n<p>\u73b0\u5728\u6709\u4e00\u4e2a\u5de5\u5382\u7f51\u7ad9\u7531\u51e0\u53f0\u673a\u5668\u8fd0\u884c\uff0c\u6bcf\u53f0\u673a\u5668\u4e0a\u8fd0\u884c\u7740 <strong>\u76f8\u540c\u6570\u91cf\u7684\u8fdb\u7a0b<\/strong> \u3002\u7f16\u5199\u89e3\u51b3\u65b9\u6848\uff0c\u8ba1\u7b97\u6bcf\u53f0\u673a\u5668\u5404\u81ea\u5b8c\u6210\u4e00\u4e2a\u8fdb\u7a0b\u4efb\u52a1\u7684\u5e73\u5747\u8017\u65f6\u3002<\/p>\n\n\n\n<p>\u5b8c\u6210\u4e00\u4e2a\u8fdb\u7a0b\u4efb\u52a1\u7684\u65f6\u95f4\u6307\u8fdb\u7a0b\u7684<code>'end' \u65f6\u95f4\u6233<\/code> \u51cf\u53bb <code>'start' \u65f6\u95f4\u6233<\/code>\u3002\u5e73\u5747\u8017\u65f6\u901a\u8fc7\u8ba1\u7b97\u6bcf\u53f0\u673a\u5668\u4e0a\u6240\u6709\u8fdb\u7a0b\u4efb\u52a1\u7684\u603b\u8017\u8d39\u65f6\u95f4\u9664\u4ee5\u673a\u5668\u4e0a\u7684\u603b\u8fdb\u7a0b\u6570\u91cf\u83b7\u5f97\u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u8868\u5fc5\u987b\u5305\u542b<code>machine_id\uff08\u673a\u5668ID\uff09<\/code> \u548c\u5bf9\u5e94\u7684 <strong>average time\uff08\u5e73\u5747\u8017\u65f6\uff09<\/strong> \u522b\u540d <code>processing_time<\/code>\uff0c\u4e14<strong>\u56db\u820d\u4e94\u5165\u4fdd\u75593\u4f4d\u5c0f\u6570\u3002<\/strong><\/p>\n\n\n\n<p>\u4ee5 <strong>\u4efb\u610f\u987a\u5e8f<\/strong> \u8fd4\u56de\u8868\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u53c2\u8003\u4f8b\u5b50\u5982\u4e0b\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Activity table:<br>&nbsp;+------------+------------+---------------+-----------+<br>&nbsp;| machine_id | process_id | activity_type | timestamp |<br>&nbsp;+------------+------------+---------------+-----------+<br>&nbsp;| 0 &nbsp; &nbsp; &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp;  | start &nbsp; &nbsp; &nbsp; &nbsp; | 0.712 &nbsp; &nbsp; |<br>&nbsp;| 0 &nbsp; &nbsp; &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp;  | end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1.520 &nbsp; &nbsp; |<br>&nbsp;| 0 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp;  | start &nbsp; &nbsp; &nbsp; &nbsp; | 3.140 &nbsp; &nbsp; |<br>&nbsp;| 0 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp;  | end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 4.120 &nbsp; &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp;  | start &nbsp; &nbsp; &nbsp; &nbsp; | 0.550 &nbsp; &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp;  | end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1.550 &nbsp; &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp;  | start &nbsp; &nbsp; &nbsp; &nbsp; | 0.430 &nbsp; &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp;  | end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1.420 &nbsp; &nbsp; |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp;  | start &nbsp; &nbsp; &nbsp; &nbsp; | 4.100 &nbsp; &nbsp; |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp;  | end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 4.512 &nbsp; &nbsp; |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp;  | start &nbsp; &nbsp; &nbsp; &nbsp; | 2.500 &nbsp; &nbsp; |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp;  | end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 5.000 &nbsp; &nbsp; |<br>&nbsp;+------------+------------+---------------+-----------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+------------+-----------------+<br>&nbsp;| machine_id | processing_time |<br>&nbsp;+------------+-----------------+<br>&nbsp;| 0 &nbsp; &nbsp; &nbsp; &nbsp;  | 0.894 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | 0.995 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | 1.456 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;+------------+-----------------+<br>&nbsp;\u89e3\u91ca\uff1a<br>&nbsp;\u4e00\u5171\u67093\u53f0\u673a\u5668,\u6bcf\u53f0\u673a\u5668\u8fd0\u884c\u7740\u4e24\u4e2a\u8fdb\u7a0b.<br>&nbsp;\u673a\u5668 0 \u7684\u5e73\u5747\u8017\u65f6: ((1.520 - 0.712) + (4.120 - 3.140)) \/ 2 = 0.894<br>&nbsp;\u673a\u5668 1 \u7684\u5e73\u5747\u8017\u65f6: ((1.550 - 0.550) + (1.420 - 0.430)) \/ 2 = 0.995<br>&nbsp;\u673a\u5668 2 \u7684\u5e73\u5747\u8017\u65f6: ((4.512 - 4.100) + (5.000 - 2.500)) \/ 2 = 1.456<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u200b<br>&nbsp;SELECT <br>&nbsp; &nbsp;  machine_id, <br>&nbsp; &nbsp;  ROUND(AVG(end_time - start_time), 3) AS processing_time<br>&nbsp;FROM (<br>&nbsp; &nbsp; &nbsp;SELECT <br>&nbsp; &nbsp; &nbsp; &nbsp;  start.machine_id,<br>&nbsp; &nbsp; &nbsp; &nbsp;  start.process_id,<br>&nbsp; &nbsp; &nbsp; &nbsp;  start.timestamp AS start_time,<br>&nbsp; &nbsp; &nbsp; &nbsp;  end.timestamp AS end_time<br>&nbsp; &nbsp; &nbsp;FROM <br>&nbsp; &nbsp; &nbsp; &nbsp;  Activity AS start<br>&nbsp; &nbsp; &nbsp;JOIN <br>&nbsp; &nbsp; &nbsp; &nbsp;  Activity AS end <br>&nbsp; &nbsp; &nbsp;ON <br>&nbsp; &nbsp; &nbsp; &nbsp;  start.machine_id = end.machine_id <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND start.process_id = end.process_id <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND start.activity_type = 'start' <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND end.activity_type = 'end'<br>&nbsp;) AS process_times<br>&nbsp;GROUP BY <br>&nbsp; &nbsp;  machine_id;<\/pre>\n\n\n\n<h2>11.\u5458\u5de5\u5956\u91d1<\/h2>\n\n\n\n<p>\u8868\uff1a<code>Employee<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+---------+<br>&nbsp;| Column Name | Type &nbsp;  |<br>&nbsp;+-------------+---------+<br>&nbsp;| empId &nbsp; &nbsp; &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| name &nbsp; &nbsp; &nbsp;  | varchar |<br>&nbsp;| supervisor  | int &nbsp; &nbsp; |<br>&nbsp;| salary &nbsp; &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;+-------------+---------+<br>&nbsp;empId \u662f\u8be5\u8868\u4e2d\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u3002<br>&nbsp;\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u90fd\u8868\u793a\u5458\u5de5\u7684\u59d3\u540d\u548c id\uff0c\u4ee5\u53ca\u4ed6\u4eec\u7684\u5de5\u8d44\u548c\u7ecf\u7406\u7684 id\u3002<\/pre>\n\n\n\n<p>\u8868\uff1a<code>Bonus<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+------+<br>&nbsp;| Column Name | Type |<br>&nbsp;+-------------+------+<br>&nbsp;| empId &nbsp; &nbsp; &nbsp; | int  |<br>&nbsp;| bonus &nbsp; &nbsp; &nbsp; | int  |<br>&nbsp;+-------------+------+<br>&nbsp;empId \u662f\u8be5\u8868\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u3002<br>&nbsp;empId \u662f Employee \u8868\u4e2d empId \u7684\u5916\u952e(reference \u5217)\u3002<br>&nbsp;\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u90fd\u5305\u542b\u4e00\u4e2a\u5458\u5de5\u7684 id \u548c\u4ed6\u4eec\u5404\u81ea\u7684\u5956\u91d1\u3002<\/pre>\n\n\n\n<p>\u7f16\u5199\u89e3\u51b3\u65b9\u6848\uff0c\u62a5\u544a\u6bcf\u4e2a\u5956\u91d1 <strong>\u5c11\u4e8e<\/strong> <code>1000<\/code> \u7684\u5458\u5de5\u7684\u59d3\u540d\u548c\u5956\u91d1\u6570\u989d\u3002<\/p>\n\n\n\n<p>\u4ee5 <strong>\u4efb\u610f\u987a\u5e8f<\/strong> \u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Employee table:<br>&nbsp;+-------+--------+------------+--------+<br>&nbsp;| empId | name &nbsp; | supervisor | salary |<br>&nbsp;+-------+--------+------------+--------+<br>&nbsp;| 3 &nbsp; &nbsp; | Brad &nbsp; | null &nbsp; &nbsp; &nbsp; | 4000 &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; | John &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp;  | 1000 &nbsp; |<br>&nbsp;| 2 &nbsp; &nbsp; | Dan &nbsp;  | 3 &nbsp; &nbsp; &nbsp; &nbsp;  | 2000 &nbsp; |<br>&nbsp;| 4 &nbsp; &nbsp; | Thomas | 3 &nbsp; &nbsp; &nbsp; &nbsp;  | 4000 &nbsp; |<br>&nbsp;+-------+--------+------------+--------+<br>&nbsp;Bonus table:<br>&nbsp;+-------+-------+<br>&nbsp;| empId | bonus |<br>&nbsp;+-------+-------+<br>&nbsp;| 2 &nbsp; &nbsp; | 500 &nbsp; |<br>&nbsp;| 4 &nbsp; &nbsp; | 2000  |<br>&nbsp;+-------+-------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+------+-------+<br>&nbsp;| name | bonus |<br>&nbsp;+------+-------+<br>&nbsp;| Brad | null  |<br>&nbsp;| John | null  |<br>&nbsp;| Dan  | 500 &nbsp; |<br>&nbsp;+------+-------+<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;select name,bonus &nbsp;from Employee e left join Bonus b on  e.empId = b.empId where b.bonus &lt; 1000 or b.bonus is null<\/pre>\n\n\n\n<h2>12.\u5b66\u751f\u4eec\u53c2\u52a0\u5404\u79d1\u6d4b\u8bd5\u7684\u6b21\u6570*<\/h2>\n\n\n\n<p>\u5b66\u751f\u8868: <code>Students<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+---------------+---------+<br>&nbsp;| Column Name &nbsp; | Type &nbsp;  |<br>&nbsp;+---------------+---------+<br>&nbsp;| student_id &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| student_name  | varchar |<br>&nbsp;+---------------+---------+<br>&nbsp;\u5728 SQL \u4e2d\uff0c\u4e3b\u952e\u4e3a student_id\uff08\u5b66\u751fID\uff09\u3002<br>&nbsp;\u8be5\u8868\u5185\u7684\u6bcf\u4e00\u884c\u90fd\u8bb0\u5f55\u6709\u5b66\u6821\u4e00\u540d\u5b66\u751f\u7684\u4fe1\u606f\u3002<\/pre>\n\n\n\n<p>\u79d1\u76ee\u8868: <code>Subjects<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+--------------+---------+<br>&nbsp;| Column Name  | Type &nbsp;  |<br>&nbsp;+--------------+---------+<br>&nbsp;| subject_name | varchar |<br>&nbsp;+--------------+---------+<br>&nbsp;\u5728 SQL \u4e2d\uff0c\u4e3b\u952e\u4e3a subject_name\uff08\u79d1\u76ee\u540d\u79f0\uff09\u3002<br>&nbsp;\u6bcf\u4e00\u884c\u8bb0\u5f55\u5b66\u6821\u7684\u4e00\u95e8\u79d1\u76ee\u540d\u79f0\u3002<\/pre>\n\n\n\n<p>\u8003\u8bd5\u8868: <code>Examinations<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+--------------+---------+<br>&nbsp;| Column Name  | Type &nbsp;  |<br>&nbsp;+--------------+---------+<br>&nbsp;| student_id &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;| subject_name | varchar |<br>&nbsp;+--------------+---------+<br>&nbsp;\u8fd9\u4e2a\u8868\u53ef\u80fd\u5305\u542b\u91cd\u590d\u6570\u636e\uff08\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728 SQL \u4e2d\uff0c\u8fd9\u4e2a\u8868\u6ca1\u6709\u4e3b\u952e\uff09\u3002<br>&nbsp;\u5b66\u751f\u8868\u91cc\u7684\u4e00\u4e2a\u5b66\u751f\u4fee\u8bfb\u79d1\u76ee\u8868\u91cc\u7684\u6bcf\u4e00\u95e8\u79d1\u76ee\u3002<br>&nbsp;\u8fd9\u5f20\u8003\u8bd5\u8868\u7684\u6bcf\u4e00\u884c\u8bb0\u5f55\u5c31\u8868\u793a\u5b66\u751f\u8868\u91cc\u7684\u67d0\u4e2a\u5b66\u751f\u53c2\u52a0\u4e86\u4e00\u6b21\u79d1\u76ee\u8868\u91cc\u67d0\u95e8\u79d1\u76ee\u7684\u6d4b\u8bd5\u3002<\/pre>\n\n\n\n<p>\u67e5\u8be2\u51fa\u6bcf\u4e2a\u5b66\u751f\u53c2\u52a0\u6bcf\u4e00\u95e8\u79d1\u76ee\u6d4b\u8bd5\u7684\u6b21\u6570\uff0c\u7ed3\u679c\u6309 <code>student_id<\/code> \u548c <code>subject_name<\/code> \u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u6784\u683c\u5f0f\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Students table:<br>&nbsp;+------------+--------------+<br>&nbsp;| student_id | student_name |<br>&nbsp;+------------+--------------+<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Alice &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | Bob &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; | John &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 6 &nbsp; &nbsp; &nbsp; &nbsp;  | Alex &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;+------------+--------------+<br>&nbsp;Subjects table:<br>&nbsp;+--------------+<br>&nbsp;| subject_name |<br>&nbsp;+--------------+<br>&nbsp;| Math &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| Physics &nbsp; &nbsp;  |<br>&nbsp;| Programming  |<br>&nbsp;+--------------+<br>&nbsp;Examinations table:<br>&nbsp;+------------+--------------+<br>&nbsp;| student_id | subject_name |<br>&nbsp;+------------+--------------+<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Math &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Physics &nbsp; &nbsp;  |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Programming  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | Programming  |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Physics &nbsp; &nbsp;  |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Math &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; | Math &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; | Programming  |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; | Physics &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | Math &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Math &nbsp; &nbsp; &nbsp; &nbsp; |<br>&nbsp;+------------+--------------+<br>&nbsp;\u8f93\u51fa\uff1a<br>&nbsp;+------------+--------------+--------------+----------------+<br>&nbsp;| student_id | student_name | subject_name | attended_exams |<br>&nbsp;+------------+--------------+--------------+----------------+<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Alice &nbsp; &nbsp; &nbsp;  | Math &nbsp; &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Alice &nbsp; &nbsp; &nbsp;  | Physics &nbsp; &nbsp;  | 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 1 &nbsp; &nbsp; &nbsp; &nbsp;  | Alice &nbsp; &nbsp; &nbsp;  | Programming  | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | Bob &nbsp; &nbsp; &nbsp; &nbsp;  | Math &nbsp; &nbsp; &nbsp; &nbsp; | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | Bob &nbsp; &nbsp; &nbsp; &nbsp;  | Physics &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; &nbsp;  | Bob &nbsp; &nbsp; &nbsp; &nbsp;  | Programming  | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 6 &nbsp; &nbsp; &nbsp; &nbsp;  | Alex &nbsp; &nbsp; &nbsp; &nbsp; | Math &nbsp; &nbsp; &nbsp; &nbsp; | 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 6 &nbsp; &nbsp; &nbsp; &nbsp;  | Alex &nbsp; &nbsp; &nbsp; &nbsp; | Physics &nbsp; &nbsp;  | 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 6 &nbsp; &nbsp; &nbsp; &nbsp;  | Alex &nbsp; &nbsp; &nbsp; &nbsp; | Programming  | 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; | John &nbsp; &nbsp; &nbsp; &nbsp; | Math &nbsp; &nbsp; &nbsp; &nbsp; | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; | John &nbsp; &nbsp; &nbsp; &nbsp; | Physics &nbsp; &nbsp;  | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 13 &nbsp; &nbsp; &nbsp; &nbsp; | John &nbsp; &nbsp; &nbsp; &nbsp; | Programming  | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+------------+--------------+--------------+----------------+<br>&nbsp;\u89e3\u91ca\uff1a<br>&nbsp;\u7ed3\u679c\u8868\u9700\u5305\u542b\u6240\u6709\u5b66\u751f\u548c\u6240\u6709\u79d1\u76ee\uff08\u5373\u4fbf\u6d4b\u8bd5\u6b21\u6570\u4e3a0\uff09\uff1a<br>&nbsp;Alice \u53c2\u52a0\u4e86 3 \u6b21\u6570\u5b66\u6d4b\u8bd5, 2 \u6b21\u7269\u7406\u6d4b\u8bd5\uff0c\u4ee5\u53ca 1 \u6b21\u7f16\u7a0b\u6d4b\u8bd5\uff1b<br>&nbsp;Bob \u53c2\u52a0\u4e86 1 \u6b21\u6570\u5b66\u6d4b\u8bd5, 1 \u6b21\u7f16\u7a0b\u6d4b\u8bd5\uff0c\u6ca1\u6709\u53c2\u52a0\u7269\u7406\u6d4b\u8bd5\uff1b<br>&nbsp;Alex \u5565\u6d4b\u8bd5\u90fd\u6ca1\u53c2\u52a0\uff1b<br>&nbsp;John  \u53c2\u52a0\u4e86\u6570\u5b66\u3001\u7269\u7406\u3001\u7f16\u7a0b\u6d4b\u8bd5\u5404 1 \u6b21\u3002<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;# Write your MySQL query statement below<br>&nbsp;SELECT <br>&nbsp; &nbsp;  s.student_id,<br>&nbsp; &nbsp;  s.student_name,<br>&nbsp; &nbsp;  sub.subject_name,<br>&nbsp; &nbsp;  COALESCE(COUNT(e.subject_name), 0) AS attended_exams<br>&nbsp;from <br>&nbsp; &nbsp;  Students s<br>&nbsp;JOIN <br>&nbsp; &nbsp;  Subjects sub<br>&nbsp;LEFT JOIN <br>&nbsp; &nbsp;  Examinations e ON s.student_id = e.student_id AND sub.subject_name = e.subject_name<br>&nbsp;group by s.student_id,s.student_name,sub.subject_name <br>&nbsp;\u200b<br>&nbsp;order by s.student_id,sub.subject_name <\/pre>\n\n\n\n<h2>13\u81f3\u5c11\u53c8\u4e94\u540d\u76f4\u63a5\u4e0b\u5c5e\u7684\u7ecf\u7406<\/h2>\n\n\n\n<p>\u8868: <code>Employee<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+-------------+---------+<br>&nbsp;| Column Name | Type &nbsp;  |<br>&nbsp;+-------------+---------+<br>&nbsp;| id &nbsp; &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp; |<br>&nbsp;| name &nbsp; &nbsp; &nbsp;  | varchar |<br>&nbsp;| department  | varchar |<br>&nbsp;| managerId &nbsp; | int &nbsp; &nbsp; |<br>&nbsp;+-------------+---------+<br>&nbsp;id \u662f\u6b64\u8868\u7684\u4e3b\u952e\uff08\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\uff09\u3002<br>&nbsp;\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u8868\u793a\u96c7\u5458\u7684\u540d\u5b57\u3001\u4ed6\u4eec\u7684\u90e8\u95e8\u548c\u4ed6\u4eec\u7684\u7ecf\u7406\u7684id\u3002<br>&nbsp;\u5982\u679cmanagerId\u4e3a\u7a7a\uff0c\u5219\u8be5\u5458\u5de5\u6ca1\u6709\u7ecf\u7406\u3002<br>&nbsp;\u6ca1\u6709\u5458\u5de5\u4f1a\u6210\u4e3a\u81ea\u5df1\u7684\u7ba1\u7406\u8005\u3002<\/pre>\n\n\n\n<p>\u7f16\u5199\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\uff0c\u627e\u51fa\u81f3\u5c11\u6709<strong>\u4e94\u4e2a\u76f4\u63a5\u4e0b\u5c5e<\/strong>\u7684\u7ecf\u7406\u3002<\/p>\n\n\n\n<p>\u4ee5 <strong>\u4efb\u610f\u987a\u5e8f<\/strong> \u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165: <br>&nbsp;Employee \u8868:<br>&nbsp;+-----+-------+------------+-----------+<br>&nbsp;| id  | name  | department | managerId |<br>&nbsp;+-----+-------+------------+-----------+<br>&nbsp;| 101 | John  | A &nbsp; &nbsp; &nbsp; &nbsp;  | Null &nbsp; &nbsp;  |<br>&nbsp;| 102 | Dan &nbsp; | A &nbsp; &nbsp; &nbsp; &nbsp;  | 101 &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 103 | James | A &nbsp; &nbsp; &nbsp; &nbsp;  | 101 &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 104 | Amy &nbsp; | A &nbsp; &nbsp; &nbsp; &nbsp;  | 101 &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 105 | Anne  | A &nbsp; &nbsp; &nbsp; &nbsp;  | 101 &nbsp; &nbsp; &nbsp; |<br>&nbsp;| 106 | Ron &nbsp; | B &nbsp; &nbsp; &nbsp; &nbsp;  | 101 &nbsp; &nbsp; &nbsp; |<br>&nbsp;+-----+-------+------------+-----------+<br>&nbsp;\u8f93\u51fa: <br>&nbsp;+------+<br>&nbsp;| name |<br>&nbsp;+------+<br>&nbsp;| John |<br>&nbsp;+------+<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;SELECT <br>&nbsp; &nbsp;  e.name<br>&nbsp;FROM <br>&nbsp; &nbsp;  Employee e<br>&nbsp;JOIN <br>&nbsp; &nbsp;  Employee sub ON e.id = sub.managerId<br>&nbsp;GROUP BY <br>&nbsp; &nbsp;  e.id, e.name<br>&nbsp;HAVING <br>&nbsp; &nbsp; &nbsp;COUNT(sub.id) &gt;= 5;<br>&nbsp;\u200b<br>&nbsp;# Write your MySQL query statement below<br>&nbsp;SELECT name <br>&nbsp;FROM Employee<br>&nbsp;WHERE id IN (<br>&nbsp; &nbsp; &nbsp;SELECT DISTINCT ManagerId<br>&nbsp; &nbsp; &nbsp;FROM Employee<br>&nbsp; &nbsp; &nbsp;GROUP BY ManagerID<br>&nbsp; &nbsp; &nbsp;HAVING COUNT(ManagerID) &gt;= 5<br>&nbsp;)<br>&nbsp;\u200b<br>&nbsp;\u200b<br>&nbsp;# Write your MySQL query statement below<br>&nbsp;select name from <br>&nbsp;(select coalesce(count(e2.managerId),0) as num,e1.name from Employee e1 join Employee e2 on e1.id = e2.managerId group by e1.id) as Employee where num&gt;=5<\/pre>\n\n\n\n<h2>14.\u786e\u8ba4\u7387<\/h2>\n\n\n\n<p>\u8868: <code>Signups<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+----------------+----------+<br>&nbsp;| Column Name &nbsp;  | Type &nbsp; &nbsp; |<br>&nbsp;+----------------+----------+<br>&nbsp;| user_id &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp;  |<br>&nbsp;| time_stamp &nbsp; &nbsp; | datetime |<br>&nbsp;+----------------+----------+<br>&nbsp;User_id\u662f\u8be5\u8868\u7684\u4e3b\u952e\u3002<br>&nbsp;\u6bcf\u4e00\u884c\u90fd\u5305\u542bID\u4e3auser_id\u7684\u7528\u6237\u7684\u6ce8\u518c\u65f6\u95f4\u4fe1\u606f\u3002<\/pre>\n\n\n\n<p>\u8868: <code>Confirmations<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;+----------------+----------+<br>&nbsp;| Column Name &nbsp;  | Type &nbsp; &nbsp; |<br>&nbsp;+----------------+----------+<br>&nbsp;| user_id &nbsp; &nbsp; &nbsp;  | int &nbsp; &nbsp;  |<br>&nbsp;| time_stamp &nbsp; &nbsp; | datetime |<br>&nbsp;| action &nbsp; &nbsp; &nbsp; &nbsp; | ENUM &nbsp; &nbsp; |<br>&nbsp;+----------------+----------+<br>&nbsp;(user_id, time_stamp)\u662f\u8be5\u8868\u7684\u4e3b\u952e\u3002<br>&nbsp;user_id\u662f\u4e00\u4e2a\u5f15\u7528\u5230\u6ce8\u518c\u8868\u7684\u5916\u952e\u3002<br>&nbsp;action\u662f\u7c7b\u578b\u4e3a('confirmed'\uff0c 'timeout')\u7684ENUM<br>&nbsp;\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u90fd\u8868\u793aID\u4e3auser_id\u7684\u7528\u6237\u5728time_stamp\u8bf7\u6c42\u4e86\u4e00\u6761\u786e\u8ba4\u6d88\u606f\uff0c\u8be5\u786e\u8ba4\u6d88\u606f\u8981\u4e48\u88ab\u786e\u8ba4('confirmed')\uff0c\u8981\u4e48\u88ab\u8fc7\u671f('timeout')\u3002<\/pre>\n\n\n\n<p>\u7528\u6237\u7684 <strong>\u786e\u8ba4\u7387<\/strong> \u662f <code>'confirmed'<\/code> \u6d88\u606f\u7684\u6570\u91cf\u9664\u4ee5\u8bf7\u6c42\u7684\u786e\u8ba4\u6d88\u606f\u7684\u603b\u6570\u3002\u6ca1\u6709\u8bf7\u6c42\u4efb\u4f55\u786e\u8ba4\u6d88\u606f\u7684\u7528\u6237\u7684\u786e\u8ba4\u7387\u4e3a <code>0<\/code> \u3002\u786e\u8ba4\u7387\u56db\u820d\u4e94\u5165\u5230 <strong>\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d<\/strong> \u3002<\/p>\n\n\n\n<p>\u7f16\u5199\u4e00\u4e2aSQL\u67e5\u8be2\u6765\u67e5\u627e\u6bcf\u4e2a\u7528\u6237\u7684 \u786e\u8ba4\u7387 \u3002<\/p>\n\n\n\n<p>\u4ee5 \u4efb\u610f\u987a\u5e8f \u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\u8f93\u5165\uff1a<br>&nbsp;Signups \u8868:<br>&nbsp;+---------+---------------------+<br>&nbsp;| user_id | time_stamp &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+---------+---------------------+<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; | 2020-03-21 10:16:13 |<br>&nbsp;| 7 &nbsp; &nbsp; &nbsp; | 2020-01-04 13:57:59 |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; | 2020-07-29 23:09:44 |<br>&nbsp;| 6 &nbsp; &nbsp; &nbsp; | 2020-12-09 10:39:37 |<br>&nbsp;+---------+---------------------+<br>&nbsp;Confirmations \u8868:<br>&nbsp;+---------+---------------------+-----------+<br>&nbsp;| user_id | time_stamp &nbsp; &nbsp; &nbsp; &nbsp;  | action &nbsp;  |<br>&nbsp;+---------+---------------------+-----------+<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; | 2021-01-06 03:30:46 | timeout &nbsp; |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; | 2021-07-14 14:00:00 | timeout &nbsp; |<br>&nbsp;| 7 &nbsp; &nbsp; &nbsp; | 2021-06-12 11:57:29 | confirmed |<br>&nbsp;| 7 &nbsp; &nbsp; &nbsp; | 2021-06-13 12:58:28 | confirmed |<br>&nbsp;| 7 &nbsp; &nbsp; &nbsp; | 2021-06-14 13:59:27 | confirmed |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; | 2021-01-22 00:00:00 | confirmed |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; | 2021-02-28 23:59:59 | timeout &nbsp; |<br>&nbsp;+---------+---------------------+-----------+<br>&nbsp;\u8f93\u51fa: <br>&nbsp;+---------+-------------------+<br>&nbsp;| user_id | confirmation_rate |<br>&nbsp;+---------+-------------------+<br>&nbsp;| 6 &nbsp; &nbsp; &nbsp; | 0.00 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 3 &nbsp; &nbsp; &nbsp; | 0.00 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 7 &nbsp; &nbsp; &nbsp; | 1.00 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;| 2 &nbsp; &nbsp; &nbsp; | 0.50 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  |<br>&nbsp;+---------+-------------------+<br>&nbsp;\u89e3\u91ca:<br>&nbsp;\u7528\u6237 6 \u6ca1\u6709\u8bf7\u6c42\u4efb\u4f55\u786e\u8ba4\u6d88\u606f\u3002\u786e\u8ba4\u7387\u4e3a 0\u3002<br>&nbsp;\u7528\u6237 3 \u8fdb\u884c\u4e86 2 \u6b21\u8bf7\u6c42\uff0c\u90fd\u8d85\u65f6\u4e86\u3002\u786e\u8ba4\u7387\u4e3a 0\u3002<br>&nbsp;\u7528\u6237 7 \u63d0\u51fa\u4e86 3 \u4e2a\u8bf7\u6c42\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u5f97\u5230\u4e86\u786e\u8ba4\u3002\u786e\u8ba4\u7387\u4e3a 1\u3002<br>&nbsp;\u7528\u6237 2 \u505a\u4e86 2 \u4e2a\u8bf7\u6c42\uff0c\u5176\u4e2d\u4e00\u4e2a\u88ab\u786e\u8ba4\uff0c\u53e6\u4e00\u4e2a\u8d85\u65f6\u3002\u786e\u8ba4\u7387\u4e3a 1 \/ 2 = 0.5\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u53ef\u56de\u6536\u4e14\u4f4e\u8102\u7684\u4ea7\u54c1 \u8868\uff1aProducts &nbsp;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/649"}],"collection":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=649"}],"version-history":[{"count":1,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":650,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/649\/revisions\/650"}],"wp:attachment":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}