牛客网之SQL必知必会(1)-字符串的截取、拼接、字母大小写

知识点

关键词:substing,concat,upper

用法:

  • 字符串的截取:substring(字符串,起始位置,截取字符数)

  • 字符串的拼接:concat(字符串1,字符串2,字符串3,...)

  • 字母大写:upper(字符串)

  • 字母小写:lower(字符串)

Products表

prod_name prod_desc
a0011 usb
a0019 iphone13
b0019 gucci t-shirts
c0019 gucci toy
d0019 lego toy carrots

【问题】编写 SQL 语句,从 Products 表中检索产品名称(prod_name)和描述(prod_desc),仅返回在描述中以先后顺序同时出现 toy 和 carrots 的产品。提示:只需要用带有三个 % 符号的 LIKE 即可。

select prod_name,
        prod_desc
from Products
where prod_desc like '%toy%carrots%'

【示例结果】返回产品名称和产品描述

prod_name prod_desc
d0019 lego toy carrots

81、返回顾客 ID(cust_id)、顾客名称(cust_name)和登录名(user_login),其中登录名全部为大写字母,并由顾客联系人的前两个字符(cust_contact)和其所在城市的前三个字符(cust_city)组成。提示:需要使用函数、拼接和别名。

给出 Customers表 如下:

cust_id cust_name cust_contact cust_city
a1 Andy Li Andy Li Oak Park
a2 Ben Liu Ben Liu Oak Park
a3 Tony Dai Tony Dai Oak Park
a4 Tom Chen Tom Chen Oak Park
a5 An Li An Li Oak Park
a6 Lee Chen Lee Chen Oak Park
a7 Hex Liu Hex Liu Oak Park
select
  cust_id,
  cust_name,
  upper(concat(substring(cust_name, 1, 2),substring(cust_city, 1, 3))) as user_login
from
  Customers

【示例结果】

返回顾客id cust_id,顾客名称cust_name,顾客登录名 user_login

cust_id cust_name user_login
a1 Andy Li ANOAK
a2 Ben Liu BEOAK
a3 Tony Dai TOOAK
a4 Tom Chen TOOAK
a5 An Li ANOAK
a6 Lee Chen LEOAK
a7 Hex Liu HEOAK

猜你喜欢

转载自blog.csdn.net/weixin_48272780/article/details/127954468