当前位置:网站首页>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 ASC

perform :

SQL Fourth practice : String handling functions - The first 1 Zhang

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 :

SQL Fourth practice : String handling functions - The first 2 Zhang

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 :

SQL Fourth practice : String handling functions - The first 3 Zhang

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 .

原网站

版权声明
本文为[Game programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102045107191.html