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

版权声明
本文为[Ly methane]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240606321521.html