当前位置:网站首页>SQL exercise 4: string processing function
SQL exercise 4: string processing function
2022-06-10 22:27:00 【Game programming】
1 Fix the name in the table
1.1 Title Description
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 .
An example of query result format is as follows :
Input :Users table:+---------+-------+| user_id | name |+---------+-------+| 1 | aLice || 2 | bOB |+---------+-------+ Output :+---------+-------+| user_id | name |+---------+-------+| 1 | Alice || 2 | Bob |+---------+-------+1.2 solve
For the first time, my idea was to splice strings directly :
select user_id,concat(upper(left(name,1)) ,lower(SUBSTRING(name,2))) as namefrom Usersorder by user_id ASCperform :

1.3 Knowledge point
- concat(str1,str2) Connect Concatenate two strings
upper(str) Capitalization String uppercase
lower(str) A lowercase letter String lowercase
LENGTH(str) length String length
SUBSTRING(str,start,end) Intercept Intercepting string ,start Start ,end end .
LEFT(str,len) Intercept Intercept the string from the left
RIGHT(str,len) Intercept Intercept the string from the right
2 Sell products by date
2.1 Title Description
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 SQL 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 .
The query result format is shown in the following example :
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 |+------------+----------+------------------------------+ explain : about 2020-05-30, The items for sale are (Headphone, Basketball, T-shirt), Arrange in dictionary order , And use commas ',' Separate . about 2020-06-01, The items for sale are (Pencil, Bible), Arrange in dictionary order , Separated by a comma . about 2020-06-02, The items for sale are (Mask), Just return the item name .2.2 solve
For repeated calculation distinct,count Count ,
select sell_date, count(distinct product) num_sold, GROUP_CONCAT(distinct product) products # By default, they are all well spliced from activitiesgroup by sell_date # Group by date order by sell_date; # Sort by date perform :

2.3 Knowledge point
- GROUP_CONCAT Text values inserted between values in the group . If you do not specify a separator , be GROUP_CONCAT Function uses a comma (,) As the default separator
group by By what group
order by By what sort , Default ascending order .desc Descending .
3 A patient with a disease
3.1 Title Description
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 .
The query result format is shown in the following example .
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 .3.2 solve
Use locate( character , Field name ) function , If you include , return >0 Number of numbers , Otherwise return to 0 , This method can only solve the problem of containing this character , Not the prefix problem , So there will be small problems in the test .
select patient_id,patient_name,conditions from Patients wherelocate('DIAB1', conditions)>0;Use like solve :
select patient_id,patient_name,conditions from Patientswhere conditions like "DIAB1%" # front or conditions like "% DIAB1%" # In the middle , Notice that there are spaces perform :

3.4 Knowledge point
- like Keyword search
author : Sichuan rookie
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- 数组 只出现一次的数字
- A WPF developed Print dialog box -printdialogx
- Solution to "XXX has broken path" error in idea
- Array move 0
- Latex error: file ‘xxx.sty‘ not found
- Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
- Abbkine column exkine Pro animal cell / tissue Total Protein Extraction Kit
- String analysis and use
- Ajout, suppression et modification des données du tableau [MySQL] (DML)
- To do desktop plug-in, a good helper for office workers
猜你喜欢

Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation

【MySQL】表的约束

Introduction to ZigBee module wireless transmission star topology networking structure

Visio to high quality pdf
![[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas](/img/00/798455f2db289e3aaed36981ea1e7c.png)
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas

JS anchor positioning can extend many functions

【MySQL】表结构的增删查改操作(DDL)
![[nk] Niuke monthly competition 51 f-average question](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] Niuke monthly competition 51 f-average question

Before we learn about high-performance computing, let's take a look at its history

【MySQL】表数据的增删查改(DML)
随机推荐
C language to judge whether a file or folder exists
数组 求并集
Record (III)
很流行的状态管理库 MobX 是怎么回事?
Latex error: file ‘xxx. sty‘ not found
Visio to high quality pdf
【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas
数组 从指定长度位旋转数组
How to view the occupied space of a table in MySQL database
php的exec函数
Exec function of PHP
Solution to "XXX has broken path" error in idea
磁盘序列号,磁盘ID,卷序列号的区别
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
笔记(二)
Modify frontsortinglayer variable of spritemask
Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered
torch_geometric
To do desktop plug-in, a good helper for office workers
C程序实例1--个人通讯录管理系统