天天看點

52. 擷取Employees中的first_name

題目描述

擷取Employees中的first_name,查詢按照first_name最後兩個字母,按照升序進行排列

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

輸出格式:

first_name
Chirstian
Tzvetan
Bezalel
Duangkaew
Georgi
Kyoichi
Anneke
Sumant
Mary
Parto
Saniya
select
  first_name
from
  employees
order by
  substr(first_name, -2);
           

或者

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

substr(X,Y) 函數,X是要截取的字元串,Y是截取開始的位置,第一個字元串的位置是1,不是0,如果Y等于length(X),則截取出最後一個字元。