当前位置:网站首页>[force deduction 10 days SQL introduction] Day3
[force deduction 10 days SQL introduction] Day3
2022-06-24 08:43:00 【Ly methane】
1667. Fix the name in the table
surface : Users
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| user_id | int |
| name | varchar |
+----------------+---------+
user_id Is the primary key of the table . This table contains the user's ID And the name . The name consists only of lowercase and uppercase characters .
Write a SQL Query to fix the name , So that only the first character is capitalized , The rest are lowercase . Back to press user_id Sorted result table .
Analysis of the answer
Query table , And found out name Make the first letter uppercase and the other letters lowercase , Reordering
concat(str1,str2, str3…) return str1 、 str2、str3、… The joining together of
concat_ws(separator ,str1,str2,…) return str1、str2、… The joining together of , Add a separator in the middle separator
lower(str) return str Lower case of , upper(str) return str Capitalization of
left(str, n) return str front n Characters , If str The length is less than n Just go back to str, If n A negative number returns an empty string ,right(str, n) , Empathy
substr(str,m,[n]) ,n Is the length can be omitted ,m Starting point , Intercept str From m Start , The length is n The string of
Knowing the above knowledge, you can write SQL Statement :
SELECT user_id, concat(upper(left(name, 1)), lower(substr(name, 2))) name
FROM users
ORDER BY user_id
1484. Sell products by date
surface Activities:
+-------------+---------+
| Name | type |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
This table has no primary key , It may contain duplicates . Each row of this table contains the product name and the date of sale in the market .
Write a query , To find each date 、 The number of different products sold and their names .
The names of products sold on each date shall be arranged in dictionary order . Back to press sell_date Sorted result table .
Input :
Activities surface :
+------------+-------------+
| sell_date | product |
+------------+-------------+
| 2020-05-30 | Headphone |
| 2020-06-01 | Pencil |
| 2020-06-02 | Mask |
| 2020-05-30 | Basketball |
| 2020-06-01 | Bible |
| 2020-06-02 | Mask |
| 2020-05-30 | T-Shirt |
+------------+-------------+
Output :
+------------+----------+------------------------------+
| sell_date | num_sold | products |
+------------+----------+------------------------------+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
| 2020-06-01 | 2 | Bible,Pencil |
| 2020-06-02 | 1 | Mask |
+------------+----------+------------------------------+
Analysis of the answer
group_concat usage
distinct Fields to connect order by Fields to sort separator Separator
group_concat(distinct product order by product separator ',')
count(distinct product) Get groups of different product
Group by sales date , Sort by date . Connect product names in groups , Sort by dictionary order
SELECT sell_date, count(distinct product) num_sold, group_concat(distinct product order by product separator ',') products
FROM activities
GROUP BY sell_date
ORDER BY sell_date
1527. A patient with a disease
Patient information sheet : Patients
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| patient_id | int |
| patient_name | varchar |
| conditions | varchar |
+--------------+---------+
patient_id ( In patients with ID) Is the primary key of the table .
'conditions' ( disease ) contain 0 One or more disease codes , Space off . This table contains information about patients in the hospital .
Write a SQL sentence , Query with I Diabetic patients ID (patient_id)、 Patient name (patient_name) And all disease codes they have (conditions).
I The code for diabetics always contains prefixes. DIAB1 .
Press In any order Return result table .
Input :
Patients surface :
+------------+--------------+--------------+
| patient_id | patient_name | conditions |
+------------+--------------+--------------+
| 1 | Daniel | YFEV COUGH |
| 2 | Alice | |
| 3 | Bob | DIAB100 MYOP |
| 4 | George | ACNE DIAB100 |
| 5 | Alain | DIAB201 |
+------------+--------------+--------------+
Output :
+------------+--------------+--------------+
| patient_id | patient_name | conditions |
+------------+--------------+--------------+
| 3 | Bob | DIAB100 MYOP |
| 4 | George | ACNE DIAB100 |
+------------+--------------+--------------+
explain :Bob and George All have code to DIAB1 Initial disease .
Analysis of the answer
Direct search conditions Contained in the DIAB1 The first one is ill , DIAB1 Or at the beginning , Either in the back
At the beginning is DIAB1% In the back is % Space DIAB1%,( To prevent a DDIAB1 This disease )
SELECT *
FROM Patients
WHERE conditions like 'DIAB1%' OR conditions like '% DIAB1%'
summary
String operation related usage
concat(str1, str2, str3,...) Returns the concatenation of these strings
concat_ws( Separator , str1, str2, .....) Returns the concatenation of these strings separated by delimiters
lower(str) return str Lower case of ,
upper(str) return str Capitalization of
left(str, n) return str front n Characters , If str The length is less than n Just go back to str, If n A negative number returns an empty string ,
right(str, n) , Empathy
substr(str,m,[n]) ,n Is the length can be omitted ,m Starting point , Intercept str From m Start , The length is n The string of
group by relevant
group_concat usage
distinct Fields to connect order by Fields to sort separator Separator
group_concat(distinct product order by product separator ',')
Statistics group by Data grouped into groups , Different types appear in a field
count(distinct Field )
边栏推荐
- Xtrabackup for data backup
- [real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
- 教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
- There was an error checking the latest version of pip
- Take my brother to do the project. It's cold
- 图片工具
- ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析
- One development skill a day: how to establish P2P communication based on webrtc?
- 【生活思考】计划与自律
- The pie chart with dimension lines can set various parameter options
猜你喜欢

Redis的Cluster集群数据倾斜

ZUCC_编译语言原理与编译_实验01 语言分析与简介

中国芯片独角兽公司

关于ETL看这篇文章就够了,三分钟让你明白什么是ETL

日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用

教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5

Jenkins自动化部署,连接不到所依赖的服务【已解决】

表单图片上传在Chorme中无法查看请求体的二进制图片信息

ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析

Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
随机推荐
Ordinary token
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
Common CVM transcribes audio using virtual sound card
Shell basic operators -- relational operators
Lombok use
5 minutes, excellent customer service chat handling skills
Permission model DAC ACL RBAC ABAC
Opencv实现图像的基本变换
Easydss anonymous live channel data volume instability optimization scheme sharing
OpenCV to realize the basic transformation of image
Variable declaration and some special variables in shell
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
JS to get the last element of the array
单目双视三维坐标确定
2022春招面试总结
Tencent cloud ASR product PHP realizes real-time voice authentication request
Matlab求解线性方程组Ax=b
【力扣10天SQL入门】Day3
Shell pass parameters
How to replace the web player easyplayerproactivex Key in OCX?