当前位置:网站首页>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 .
边栏推荐
- Cordova plugin /jpush phonegap Aurora push_ Local push_ Message push
- Array to sum the heights of rising intervals
- String analysis and use
- torch_ geometric
- php的exec函数
- Bitwise and shift operators
- ICML2022 | Sharp-MAML:锐度感知的模型无关元学习
- SQL Server查询区分大小写
- Array intersection of two arrays II
- Modify frontsortinglayer variable of spritemask
猜你喜欢

AI blessing real-time interaction | analysis of zegoavatar facial expression following technology

Interpreting the new ecology of education in maker space

Abbexa AML1 DNA binding ELISA Kit instructions

Install MySQL on Linux system. Problems encountered in xshell

KDD2022 | 基于对抗性知识蒸馏的深度图神经网络压缩

修改SpriteMask 的 frontSortingLayer 变量

datagrip 报错 “The specified database user/password combination is rejected...”的解决方法

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

Icml2022 | sharp maml: model independent meta learning for sharpness perception

很流行的状态管理库 MobX 是怎么回事?
随机推荐
【Debug】could not find ref wiht poc XXX解决
磁盘序列号,磁盘ID,卷序列号的区别
Interpreting the new ecology of education in maker space
(11) Tableview
变量(自动变量、静态变量、寄存器变量、外部变量)与C的内存分配malloc/free、calloc/recalloc
Modify frontsortinglayer variable of spritemask
Abbexa cdan1 siRNA instruction manual
Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
leetcode:333. Maximum BST subtree
数组 移动0
JVM运行时数据区
SQL第四练:字符串处理函数
在模仿学习中进步的智能机器人
Before we learn about high-performance computing, let's take a look at its history
C program example 1 -- personal address book management system
C language - quick sorting in sorting
Exec function of PHP
NFT copyright / royalties
Intelligent robot making progress in imitation learning
Visio 转为高质量PDF