2504. 把名字和职业联系起来

Leetcode题目:SubString和CONCAT

表: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| person_id   | int     |
| name        | varchar |
| profession  | ENUM    |
+-------------+---------+
person_id 是该表的主键(具有唯一值的列)。
该表中的每一行都包含一个人的 ID、姓名和职业。
profession 是 ENUM 类型,其值为 ('Doctor', 'Singer', 'Actor', 'Player', 'Engineer', 'Lawyer') 之一。

编写一个解决方案报告每个人的名字,后面是他们职业的第一个字母,用括号括起来。

返回按 person_id 降序排列 的结果表。

返回结果格式示例如下。

示例 1:

输入: 
Person 表:
+-----------+-------+------------+
| person_id | name  | profession |
+-----------+-------+------------+
| 1         | Alex  | Singer     |
| 3         | Alice | Actor      |
| 2         | Bob   | Player     |
| 4         | Messi | Doctor     |
| 6         | Tyson | Engineer   |
| 5         | Meir  | Lawyer     |
+-----------+-------+------------+
输出: 
+-----------+----------+
| person_id | name     |
+-----------+----------+
| 6         | Tyson(E) |
| 5         | Meir(L)  |
| 4         | Messi(D) |
| 3         | Alice(A) |
| 2         | Bob(P)   |
| 1         | Alex(S)  |
+-----------+----------+
解释: 请注意,在名称和职业的第一个字母之间不应该有任何空白。

解题思路

SubString+CONCAT

学习下这两个函数的用法。之前用过GROUP_CONCAT。CONCAT和GROUP_CONCAT都可以合并多个字符串/列。SubString的话就是从单个字符串中取n个字符了。

# Write your MySQL query statement below
SELECT person_id,CONCAT(name,'(',SUBSTRING(profession,1,1),')') AS name
FROM Person 
ORDER BY person_id DESC
Subscribe
提醒
0 评论
Inline Feedbacks
View all comments
0
在此留下你的评论x