题目
表:Tweets
+----------------+---------+ | Column Name | Type | +----------------+---------+ | tweet_id | int | | content | varchar | +----------------+---------+ 在 SQL 中,tweet_id 是这个表的主键。 content 只包含字母数字字符,'!',' ',不包含其它特殊字符。 这个表包含某社交媒体 App 中所有的推文。
查询所有无效推文的编号(ID)。当推文内容中的字符数严格大于 15
时,该推文是无效的。
以任意顺序返回结果表。
查询结果格式如下所示:
示例 1:
输入:
Tweets 表:
+----------+----------------------------------+
| tweet_id | content |
+----------+----------------------------------+
| 1 | Vote for Biden |
| 2 | Let us make America great again! |
+----------+----------------------------------+
输出:
+----------+
| tweet_id |
+----------+
| 2 |
+----------+
解释:
推文 1 的长度 length = 14。该推文是有效的。
推文 2 的长度 length = 32。该推文是无效的。
思路
- 从Tweets表中选择tweet_id,条件是对应content内字符的长度大于15。
代码实现
# Write your MySQL query statement below
select tweet_id from Tweets where length(content) > 15
知识积累
- length(str):返回str所包含的字节数(注意不是字符数),如果统计非英文字符串的字符数这个不适用。
- char_length(str):返回字符串str的长度,这个对非英文字符串也适用。
官方题解
- 因为凑巧content是全英文所以length(str)是适用的,官解给了更通用的符合目的的方法char_length(str),不同于length(str)统计的0是字节数,char_length(str)统计的是字符串中的字符数。
- 复现:
# Write your MySQL query statement below select tweet_id from Tweets where char_length(content) > 15