1667. 修复表中的名字

Leetcode题目:SQL字符串函数

表: Users

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| user_id        | int     |
| name           | varchar |
+----------------+---------+
user_id 是该表的主键(具有唯一值的列)。
该表包含用户的 ID 和名字。名字仅由小写和大写字符组成。

编写解决方案,修复名字,使得只有第一个字符是大写的,其余都是小写的。

返回按 user_id 排序的结果表。

返回结果格式示例如下。

示例 1:

输入:
Users table:
+---------+-------+
| user_id | name  |
+---------+-------+
| 1       | aLice |
| 2       | bOB   |
+---------+-------+
输出:
+---------+-------+
| user_id | name  |
+---------+-------+
| 1       | Alice |
| 2       | Bob   |
+---------+-------+

解题思路

字符串函数

主要就是学习下SQL中处理字符串的几个函数,比如LEFT,SubString这些不怎么常用的。CONCAT最常用所以还好吧。

# Write your MySQL query statement below
SELECT user_id,
CONCAT(UPPER(LEFT(name,1)),LOWER(SUBSTRING(name,2))) AS name
FROM Users
ORDER BY user_id ASC
Subscribe
提醒
0 评论
Inline Feedbacks
View all comments
0
在此留下你的评论x