SQL52 获取Employees中的first_name

本文最后更新于:2022年4月9日 中午


题目描述

获取Employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列

1
2
3
4
5
6
7
8
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));

输入描述

输出描述

题解

1
2
3
select first_name from employees order by substr(first_name,length(first_name)-1,2);

select first_name from employees order by substr(first_name,-2);

备注:

  1. substr(字符串,起始位置,长度)

    起始位置:截取的子串的起始位置(注意:字符串的第一个字符的索引是1)。值为正时从字符串开始位置 开始计数,值为负时从字符串结尾位置开始计数。

  2. 在MySQL中:

    1
    SELECT first_name from employees order by right(first_name, 2);

参考文章:

MySQL 函数:https://www.runoob.com/mysql/mysql-functions.html