Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions solution/0100-0199/0197.Rising Temperature/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Weather 表:</code>

<!-- 这里可写通用的实现逻辑 -->

**方法一:自连接 + DATEDIFF/SUBDATE 函数**

我们可以通过自连接的方式,将 `Weather` 表中的每一行与它的前一行进行比较,如果温度更高,并且日期相差一天,那么就是我们要找的结果。

<!-- tabs:start -->

### **SQL**
Expand All @@ -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;
```

<!-- tabs:end -->
15 changes: 9 additions & 6 deletions solution/0100-0199/0197.Rising Temperature/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ In 2015-01-04, the temperature was higher than the previous day (20 -&gt; 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.

<!-- tabs:start -->

### **SQL**
Expand All @@ -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;
```

<!-- tabs:end -->
2 changes: 1 addition & 1 deletion solution/0100-0199/0197.Rising Temperature/Solution.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions solution/0500-0599/0584.Find Customer Referee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Customer 表:

<!-- 这里可写通用的实现逻辑 -->

**方法一:条件过滤**

我们可以直接筛选出 `referee_id` 不为 `2` 的客户姓名。注意,`referee_id` 为 `NULL` 的客户也应该被筛选出来。

<!-- tabs:start -->

### **SQL**
Expand Down
4 changes: 4 additions & 0 deletions solution/0500-0599/0584.Find Customer Referee/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,36 @@ ID = 96 的顾客曾经去过购物中心,并且没有进行任何交易。

<!-- 这里可写通用的实现逻辑 -->

**方法一:子查询 + 分组统计**

我们可以使用子查询,先找出所有没有进行交易的 `visit_id`,然后按照 `customer_id` 进行分组,统计每个顾客的没有进行交易的次数。

**方法二:左连接 + 分组统计**

我们也可以使用左连接,将 `Visits` 表和 `Transactions` 表按照 `visit_id` 进行连接,然后筛选出 `amount` 为 `NULL` 的记录,按照 `customer_id` 进行分组,统计每个顾客的没有进行交易的次数。

<!-- tabs:start -->

### **SQL**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```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;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,36 @@ 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.

<!-- tabs:start -->

### **SQL**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```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;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 7 additions & 4 deletions solution/1600-1699/1683.Invalid Tweets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 即可。

<!-- tabs:start -->

Expand Down
8 changes: 8 additions & 0 deletions solution/1600-1699/1683.Invalid Tweets/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$.

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Products 表:

<!-- 这里可写通用的实现逻辑 -->

**方法一:条件筛选**

我们直接筛选出 `low_fats``Y``recyclable``Y` 的产品编号即可。

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

<!-- tabs:start -->

### **SQL**
Expand Down