MySQL Fix Product Name Format(trim去空格+upper/lower大小写)

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

Table: Sales

+--------------+---------+ | Column Name | Type | +--------------+---------+ | sale_id | int | | product_name | varchar | | sale_date | date | +--------------+---------+ sale_id is the primary key for this table. Each row of this table contains 
the product name and the date it was sold. 

Since table Sales was filled manually in the year 2000, product_name may contain leading and/or trailing white spaces, also they are case-insensitive.

Write an SQL query to report

product_name in lowercase without leading or trailing white spaces.
sale_date in the format ('YYYY-MM')
total the number of times the product was sold in this month.
Return the result table ordered by product_name in ascending order, in case of a tie order it by sale_date in ascending order.

The query result format is in the following example.

Sales +------------+------------------+--------------+ | sale_id | product_name | sale_date | +------------+------------------+--------------+ | 1 | LCPHONE | 2000-01-16 | | 2 | LCPhone | 2000-01-17 | | 3 | LcPhOnE | 2000-02-18 | | 4 | LCKeyCHAiN | 2000-02-19 | | 5 | LCKeyChain | 2000-02-28 | | 6 | Matryoshka | 2000-03-31 | +------------+------------------+--------------+ Result table: +--------------+--------------+----------+ | product_name | sale_date | total | +--------------+--------------+----------+ | lcphone | 2000-01 | 2 | | lckeychain | 2000-02 | 2 | | lcphone | 2000-02 | 1 | | matryoshka | 2000-03 | 1 | +--------------+--------------+----------+ In January, 2 LcPhones were sold, please note that the product names are not case sensitive and may contain spaces. In Februery, 2 LCKeychains and 1 LCPhone were sold. In March, 1 matryoshka was sold. 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fix-product-name-format
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 大写upper\ucase,小写lower\lcase
  • trim去除空格,ltrim、rtrim只去除左侧或者右侧空格
# Write your MySQL query statement below select product_name, sale_date, count(*) total from ( select lcase(trim(product_name)) product_name, date_format(sale_date,'%Y-%m') sale_date from Sales ) t group by product_name, sale_date order by product_name, sale_date 

我的CSDN博客地址 https://michael.blog.csdn.net/

本文地址:https://blog.csdn.net/qq_21201267/article/details/107890180

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐