当前位置:网站首页>[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 )
边栏推荐
- Get screen width and height tool class
- PHP代码加密的几种方案
- ZUCC_编译语言原理与编译_实验01 语言分析与简介
- uniapp 热更新后台管理
- Common CVM transcribes audio using virtual sound card
- Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
- xargs使用技巧 —— 筑梦之路
- New technology practice, encapsulating the permission application library step by step with the activity results API
- [micro services ~nacos] Nacos service providers and service consumers
- Lombok use
猜你喜欢

Markdown to realize text link jump

Permission model DAC ACL RBAC ABAC

【团队管理】测试团队绩效管理的25点小建议
![[untitled]](/img/94/792e8363dbfe67770e93b0dcdc8e72.png)
[untitled]

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

A preliminary study of IO model

Qt 中发送自定义事件

Vscode install the remote -wsl plug-in to connect to the local WSL

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar

Redis的Cluster集群数据倾斜
随机推荐
Glusterfs replacement failure brick
图片工具
ZUCC_编译语言原理与编译_实验04 语言与文法
A preliminary study of IO model
Application of tidb in Netease games
Introduction to RCNN, fast RCNN and fast RCNN
Three ways to uninstall Symantec Endpoint Protection Symantec
String to Base64
Use cpulimit to free up your CPU
[micro services ~nacos] Nacos service providers and service consumers
Tencent conference API - get rest API & webhook application docking information
Opencv get (propid) common values
liunx服务器 telnet 带用户名 端口登陆方法
Common date formatter and QT method for obtaining current time
There was an error checking the latest version of pip
ZUCC_编译语言原理与编译_实验03 编译器入门
JUC个人简单笔记
PHP代码加密+扩展解密实战
Matlab求解线性方程组Ax=b
Common CVM transcribes audio using virtual sound card