From 6c4748f84d82c6cd6856ea1df1a80b04a946c463 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 17 Oct 2023 11:09:47 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.0197,0584,1581,1683,1757 * No.0197.Rising Temperature * No.0584.Find Customer Referee * No.1581.Customer Who Visited but Did Not Make Any Transactions * No.1683.Invalid Tweets * No.1757.Recyclable and Low Fat Products --- .../0197.Rising Temperature/README.md | 15 ++++++----- .../0197.Rising Temperature/README_EN.md | 15 ++++++----- .../0197.Rising Temperature/Solution.sql | 2 +- .../0584.Find Customer Referee/README.md | 4 +++ .../0584.Find Customer Referee/README_EN.md | 4 +++ .../README.md | 27 ++++++++++--------- .../README_EN.md | 27 ++++++++++--------- .../Solution.sql | 17 +++++------- .../1600-1699/1683.Invalid Tweets/README.md | 11 +++++--- .../1683.Invalid Tweets/README_EN.md | 8 ++++++ .../README.md | 4 +++ .../README_EN.md | 4 +++ 12 files changed, 87 insertions(+), 51 deletions(-) diff --git a/solution/0100-0199/0197.Rising Temperature/README.md b/solution/0100-0199/0197.Rising Temperature/README.md index f228899869afd..c7f89a1be25ce 100644 --- a/solution/0100-0199/0197.Rising Temperature/README.md +++ b/solution/0100-0199/0197.Rising Temperature/README.md @@ -61,6 +61,10 @@ Weather 表: +**方法一:自连接 + DATEDIFF/SUBDATE 函数** + +我们可以通过自连接的方式,将 `Weather` 表中的每一行与它的前一行进行比较,如果温度更高,并且日期相差一天,那么就是我们要找的结果。 + ### **SQL** @@ -75,13 +79,12 @@ FROM ``` ```sql -SELECT - w2.id AS Id +# Write your MySQL query statement below +SELECT w1.id FROM - weather AS w1 - JOIN weather AS w2 ON DATE_ADD( w1.recordDate, INTERVAL 1 DAY) = w2.recordDate -WHERE - w1.temperature < w2.temperature + Weather AS w1 + JOIN Weather AS w2 + ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` diff --git a/solution/0100-0199/0197.Rising Temperature/README_EN.md b/solution/0100-0199/0197.Rising Temperature/README_EN.md index 9c928ee0d2f68..a346c9db29333 100644 --- a/solution/0100-0199/0197.Rising Temperature/README_EN.md +++ b/solution/0100-0199/0197.Rising Temperature/README_EN.md @@ -54,6 +54,10 @@ In 2015-01-04, the temperature was higher than the previous day (20 -> 30). ## Solutions +**Solution 1: Self-Join + DATEDIFF/SUBDATE Function** + +We can use self-join to compare each row in the `Weather` table with its previous row. If the temperature is higher and the date difference is one day, then it is the result we are looking for. + ### **SQL** @@ -68,13 +72,12 @@ FROM ``` ```sql -SELECT - w2.id AS Id +# Write your MySQL query statement below +SELECT w1.id FROM - weather AS w1 - JOIN weather AS w2 ON DATE_ADD( w1.recordDate, INTERVAL 1 DAY) = w2.recordDate -WHERE - w1.temperature < w2.temperature + Weather AS w1 + JOIN Weather AS w2 + ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` diff --git a/solution/0100-0199/0197.Rising Temperature/Solution.sql b/solution/0100-0199/0197.Rising Temperature/Solution.sql index e8ecebaee69ef..ee7f7f22250ed 100644 --- a/solution/0100-0199/0197.Rising Temperature/Solution.sql +++ b/solution/0100-0199/0197.Rising Temperature/Solution.sql @@ -3,4 +3,4 @@ SELECT w1.id FROM Weather AS w1 JOIN Weather AS w2 - ON DATEDIFF(w1.recordDate, w2.recordDate) = 1 AND w1.temperature > w2.temperature; + ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; diff --git a/solution/0500-0599/0584.Find Customer Referee/README.md b/solution/0500-0599/0584.Find Customer Referee/README.md index 80468389c082d..b2c39fcf8113e 100644 --- a/solution/0500-0599/0584.Find Customer Referee/README.md +++ b/solution/0500-0599/0584.Find Customer Referee/README.md @@ -56,6 +56,10 @@ Customer 表: +**方法一:条件过滤** + +我们可以直接筛选出 `referee_id` 不为 `2` 的客户姓名。注意,`referee_id` 为 `NULL` 的客户也应该被筛选出来。 + ### **SQL** diff --git a/solution/0500-0599/0584.Find Customer Referee/README_EN.md b/solution/0500-0599/0584.Find Customer Referee/README_EN.md index 1467f768c2291..a14a4697df340 100644 --- a/solution/0500-0599/0584.Find Customer Referee/README_EN.md +++ b/solution/0500-0599/0584.Find Customer Referee/README_EN.md @@ -55,6 +55,10 @@ Customer table: ## Solutions +**Solution 1: Conditional Filtering** + +We can directly filter out the customer names whose `referee_id` is not `2`. Note that the customers whose `referee_id` is `NULL` should also be filtered out. + ### **SQL** diff --git a/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README.md b/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README.md index 31af2d4a7d1a2..b84503bcd99c5 100644 --- a/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README.md +++ b/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README.md @@ -91,6 +91,14 @@ ID = 96 的顾客曾经去过购物中心,并且没有进行任何交易。 +**方法一:子查询 + 分组统计** + +我们可以使用子查询,先找出所有没有进行交易的 `visit_id`,然后按照 `customer_id` 进行分组,统计每个顾客的没有进行交易的次数。 + +**方法二:左连接 + 分组统计** + +我们也可以使用左连接,将 `Visits` 表和 `Transactions` 表按照 `visit_id` 进行连接,然后筛选出 `amount` 为 `NULL` 的记录,按照 `customer_id` 进行分组,统计每个顾客的没有进行交易的次数。 + ### **SQL** @@ -98,26 +106,21 @@ ID = 96 的顾客曾经去过购物中心,并且没有进行任何交易。 ```sql -SELECT - customer_id, - COUNT(*) AS count_no_trans +# Write your MySQL query statement below +SELECT customer_id, COUNT(1) AS count_no_trans FROM Visits -WHERE - visit_id NOT IN ( - SELECT visit_id - FROM Transactions - ) -GROUP BY customer_id; +WHERE visit_id NOT IN (SELECT visit_id FROM Transactions) +GROUP BY 1; ``` ```sql # Write your MySQL query statement below SELECT customer_id, COUNT(1) AS count_no_trans FROM - Visits AS v - LEFT JOIN Transactions AS t ON v.visit_id = t.visit_id + Visits + LEFT JOIN Transactions USING (visit_id) WHERE amount IS NULL -GROUP BY customer_id; +GROUP BY 1; ``` diff --git a/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README_EN.md b/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README_EN.md index b058a302e99b5..80afc20e9870d 100644 --- a/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README_EN.md +++ b/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README_EN.md @@ -87,6 +87,14 @@ As we can see, users with IDs 30 and 96 visited the mall one time without making ## Solutions +**Solution 1: Subquery + Grouping** + +We can use a subquery to first find all `visit_id`s that have not made any transactions, and then group by `customer_id` to count the number of times each customer has not made any transactions. + +**Solution 2: Left Join + Grouping** + +We can also use a left join to join the `Visits` table and the `Transactions` table on `visit_id`, and then filter out the records where `amount` is `NULL`. After that, we can group by `customer_id` to count the number of times each customer has not made any transactions. + ### **SQL** @@ -94,26 +102,21 @@ As we can see, users with IDs 30 and 96 visited the mall one time without making ```sql -SELECT - customer_id, - COUNT(*) AS count_no_trans +# Write your MySQL query statement below +SELECT customer_id, COUNT(1) AS count_no_trans FROM Visits -WHERE - visit_id NOT IN ( - SELECT visit_id - FROM Transactions - ) -GROUP BY customer_id; +WHERE visit_id NOT IN (SELECT visit_id FROM Transactions) +GROUP BY 1; ``` ```sql # Write your MySQL query statement below SELECT customer_id, COUNT(1) AS count_no_trans FROM - Visits AS v - LEFT JOIN Transactions AS t ON v.visit_id = t.visit_id + Visits + LEFT JOIN Transactions USING (visit_id) WHERE amount IS NULL -GROUP BY customer_id; +GROUP BY 1; ``` diff --git a/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/Solution.sql b/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/Solution.sql index d924846b2a1bb..a10764142ed97 100644 --- a/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/Solution.sql +++ b/solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/Solution.sql @@ -1,10 +1,7 @@ -SELECT - customer_id, - COUNT(*) AS count_no_trans -FROM Visits -WHERE - visit_id NOT IN ( - SELECT visit_id - FROM Transactions - ) -GROUP BY customer_id; +# Write your MySQL query statement below +SELECT customer_id, COUNT(1) AS count_no_trans +FROM + Visits + LEFT JOIN Transactions USING (visit_id) +WHERE amount IS NULL +GROUP BY 1; diff --git a/solution/1600-1699/1683.Invalid Tweets/README.md b/solution/1600-1699/1683.Invalid Tweets/README.md index 5660252b8bf64..bb058649aabd9 100644 --- a/solution/1600-1699/1683.Invalid Tweets/README.md +++ b/solution/1600-1699/1683.Invalid Tweets/README.md @@ -55,10 +55,13 @@ Tweets 表: -- `CHAR_LENGTH(str)`: 中文、数字、字母都是 1 字节 -- `LENGTH(str)`: - - utf8: 中文 3 字节,数字、字母 1 字节 - - gbk: 中文 2 字节,数字、字母 1 字节 +**方法一:使用 `CHAR_LENGTH` 函数** + +`CHAR_LENGTH()` 函数返回字符串的长度,其中中文、数字、字母都是 $1$ 字节。 + +`LENGTH()` 函数返回字符串的长度,其中 utf8 编码下,中文 $3$ 字节,数字、字母 $1$ 字节;gbk 编码下,中文 $2$ 字节,数字、字母 $1$ 字节。 + +对于本题,我们直接用 `CHAR_LENGTH` 函数获取字符串长度,筛选出长度大于 $15$ 的推文 ID 即可。 diff --git a/solution/1600-1699/1683.Invalid Tweets/README_EN.md b/solution/1600-1699/1683.Invalid Tweets/README_EN.md index 69356494849ee..c5fc81aaeab97 100644 --- a/solution/1600-1699/1683.Invalid Tweets/README_EN.md +++ b/solution/1600-1699/1683.Invalid Tweets/README_EN.md @@ -50,6 +50,14 @@ Tweet 2 has length = 32. It is an invalid tweet. ## Solutions +**Solution 1: Using `CHAR_LENGTH` Function** + +The `CHAR_LENGTH()` function returns the length of a string, where Chinese characters, numbers, and letters are all counted as $1$ byte. + +The `LENGTH()` function returns the length of a string, where under utf8 encoding, Chinese characters are counted as $3$ bytes, while numbers and letters are counted as $1$ byte; under gbk encoding, Chinese characters are counted as $2$ bytes, while numbers and letters are counted as $1$ byte. + +For this problem, we can directly use the `CHAR_LENGTH` function to get the length of the string, and filter out the tweet IDs with a length greater than $15$. + ### **SQL** diff --git a/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md b/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md index a1145fdbf7484..8666096fbc255 100644 --- a/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md +++ b/solution/1700-1799/1757.Recyclable and Low Fat Products/README.md @@ -59,6 +59,10 @@ Products 表: +**方法一:条件筛选** + +我们直接筛选出 `low_fats` 为 `Y` 且 `recyclable` 为 `Y` 的产品编号即可。 + ### **SQL** diff --git a/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md b/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md index b77b3a71b470c..8b5f02cc81aa9 100644 --- a/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md +++ b/solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md @@ -53,6 +53,10 @@ Products table: ## Solutions +**Solution 1: Conditional Filtering** + +We can directly filter the product IDs where `low_fats` is `Y` and `recyclable` is `Y`. + ### **SQL**