当前位置:网站首页>Mysql database, sorting and single line processing functions
Mysql database, sorting and single line processing functions
2022-07-24 02:30:00 【The end of the world and you】
Mysql database , Sorting and single line processing functions
1. Sort
See this article for the contents of the sample table
Mysql Support data sorting , for example , Now we sort the salary from small to large :
mysql> select ename,sal from emp order by sal;
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| JAMES | 950.00 |
| ADAMS | 1100.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN | 1600.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| JONES | 2975.00 |
| SCOTT | 3000.00 |
| FORD | 3000.00 |
| KING | 5000.00 |
+--------+---------+
14 rows in set (0.00 sec)
If you need to sort in descending order , You need to specify the desc:( Sort ascending by default , If you specify , Designated as asc that will do )
mysql> select ename,sal from emp order by sal desc;
+--------+---------+
| ename | sal |
+--------+---------+
| KING | 5000.00 |
| SCOTT | 3000.00 |
| FORD | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| ALLEN | 1600.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| SMITH | 800.00 |
+--------+---------+
14 rows in set (0.00 sec)
More complicated situation , Sort multiple fields :
For example, we want to arrange the salary in ascending order , With the same salary , Sort by name in descending order :
mysql> select ename,sal from emp order by sal,ename desc;
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| JAMES | 950.00 |
| ADAMS | 1100.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN | 1600.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| JONES | 2975.00 |
| SCOTT | 3000.00 |
| FORD | 3000.00 |
| KING | 5000.00 |
+--------+---------+
14 rows in set (0.00 sec)
Sort and combine conditions to find :
Ask to find out the salary in 1250 To 3500 Between , Sort by salary in descending order :
mysql> select ename,sal from emp where sal between 1250 and 3500 order by sal desc;
+--------+---------+
| ename | sal |
+--------+---------+
| SCOTT | 3000.00 |
| FORD | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| ALLEN | 1600.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
+--------+---------+
10 rows in set (0.00 sec)
2. Single line processing functions
Process one line before processing the next :( One input corresponds to one output )
Content to lowercase
mysql> select lower(ename) from emp;
+--------------+
| lower(ename) |
+--------------+
| smith |
| allen |
| ward |
| jones |
| martin |
| blake |
| clark |
| scott |
| king |
| turner |
| adams |
| james |
| ford |
| miller |
+--------------+
14 rows in set (0.00 sec)
Capitalize the content
mysql> select upper(ename) from emp;
+--------------+
| upper(ename) |
+--------------+
| SMITH |
| ALLEN |
| WARD |
| JONES |
| MARTIN |
| BLAKE |
| CLARK |
| SCOTT |
| KING |
| TURNER |
| ADAMS |
| JAMES |
| FORD |
| MILLER |
+--------------+
14 rows in set (0.00 sec)
Take the string
for example : We want to get the first letter of each name :
mysql> select substr(ename,1,1) from emp;
+-------------------+
| substr(ename,1,1) |
+-------------------+
| S |
| A |
| W |
| J |
| M |
| B |
| C |
| S |
| K |
| T |
| A |
| J |
| F |
| M |
+-------------------+
14 rows in set (0.00 sec)
String splicing
Splice everyone's empno and ename:
mysql> select concat(empno,ename) from emp;
+---------------------+
| concat(empno,ename) |
+---------------------+
| 7369SMITH |
| 7499ALLEN |
| 7521WARD |
| 7566JONES |
| 7654MARTIN |
| 7698BLAKE |
| 7782CLARK |
| 7788SCOTT |
| 7839KING |
| 7844TURNER |
| 7876ADAMS |
| 7900JAMES |
| 7902FORD |
| 7934MILLER |
+---------------------+
14 rows in set (0.00 sec)
Find the length
Take out the number of characters of each person's name :
mysql> select length(ename) from emp;
+---------------+
| length(ename) |
+---------------+
| 5 |
| 5 |
| 4 |
| 5 |
| 6 |
| 5 |
| 5 |
| 5 |
| 4 |
| 6 |
| 5 |
| 5 |
| 4 |
| 6 |
+---------------+
14 rows in set (0.00 sec)
Remove the front and back blanks
The query name is KING Details of , Do not include front and back blanks :
mysql> select * from emp where ename = trim('KING ');
+-------+-------+-----------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
+-------+-------+-----------+------+------------+---------+------+--------+
1 row in set (0.00 sec)
rounding
Yes 123.456 Retain 0 Decimal place
mysql> select round(123.456,0) from emp;
+------------------+
| round(123.456,0) |
+------------------+
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
| 123 |
+------------------+
14 rows in set (0.00 sec)
Generate random number
Generate 0 To 1 A random fraction of :
mysql> select rand() from emp;
+---------------------+
| rand() |
+---------------------+
| 0.06316715857309024 |
| 0.5963954959031152 |
| 0.7924760345299505 |
| 0.17319371567405176 |
| 0.48854050551405226 |
| 0.923121411281751 |
| 0.1499855706002429 |
| 0.9805636498896066 |
| 0.4528615683809496 |
| 0.3226169229695731 |
| 0.25449994043866164 |
| 0.304648964018234 |
| 0.75974502950883 |
| 0.8847782862230933 |
+---------------------+
14 rows in set (0.00 sec)
Null conversion
For NULL The result of the operation must be NULL So there was NULL Processing function
for example : Calculate the annual income of each employee ( a monthly salary + Monthly bonus ):
mysql> select ename,job,sal,
-> (case job when 'MANAGER' then sal*1.1 when 'SALESMAN' then sal*1.5 else sal*1.2 end) as newsal
-> from emp;
+--------+-----------+---------+---------+
| ename | job | sal | newsal |
+--------+-----------+---------+---------+
| SMITH | CLERK | 800.00 | 960.00 |
| ALLEN | SALESMAN | 1600.00 | 2400.00 |
| WARD | SALESMAN | 1250.00 | 1875.00 |
| JONES | MANAGER | 2975.00 | 3272.50 |
| MARTIN | SALESMAN | 1250.00 | 1875.00 |
| BLAKE | MANAGER | 2850.00 | 3135.00 |
| CLARK | MANAGER | 2450.00 | 2695.00 |
| SCOTT | ANALYST | 3000.00 | 3600.00 |
| KING | PRESIDENT | 5000.00 | 6000.00 |
| TURNER | SALESMAN | 1500.00 | 2250.00 |
| ADAMS | CLERK | 1100.00 | 1320.00 |
| JAMES | CLERK | 950.00 | 1140.00 |
| FORD | ANALYST | 3000.00 | 3600.00 |
| MILLER | CLERK | 1300.00 | 1560.00 |
+--------+-----------+---------+---------+
14 rows in set (0.00 sec)
边栏推荐
- Discussion on sending redundant API requests for Spartacus UI transfer state of SAP e-commerce cloud
- Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
- Responsive pbootcms template decoration design website
- MySQL---four JDBC
- “我们为什么要做 iVX ? ” ——访 iVX CEO 孟智平 了解 iVX 企业文化
- Enter cnpm -v and cnpm appears: the file c:\users\19457\appdata\roaming\npm\cnpm.ps1 cannot be loaded because running scripts is prohibited on this system.
- Sharing a case of controller restart caused by a CIFS bug in NetApp Fas series
- 利用宝塔面板计划任务执行自动推送网址到百度收录
- ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
- [jailhouse article] virtualization over multiprocessor system on chip an enabling paradigm for
猜你喜欢

Enter cnpm -v and cnpm appears: the file c:\users\19457\appdata\roaming\npm\cnpm.ps1 cannot be loaded because running scripts is prohibited on this system.

Running around, market and quantitative page function optimization! Stock quantitative analysis tool qtyx-v2.4.5

Digital transformation behind the reshaping growth of catering chain stores

Codeworks 5 questions per day (average 1500) - day 23

关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答

营员招募|心怀世界的AI青年们,联合国需要你为可持续发展助力!

Jina AI and datawhale jointly launched a learning project!

Network protocol details: TCP part1

JDBC tool class

Canvas drawing (mouse click to draw and lift to end)
随机推荐
Mysql数据库,分组函数篇
Performance test of ArrayList and LinkedList insertion based on jmh
Leetcode 70 climbing stairs, 199 right view of binary tree, 232 realizing queue with stack, 143 rearranging linked list
[MySQL] character set utf8mb4 cannot store the record of expression stepping on the pit
什么叫裸写SQL?express操作mysql用什么中件间或插件好呢?
关于 SAP 电商云 Spartacus UI Transfer State 冗余 API 请求发送的讨论
MySQL---four JDBC
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part2):图谱数据准备与导入
Installation, configuration and use of sentry
Qt开发串口通讯软件中的数据转换问题:读取时_QByteArray转string;发送时_数据格式;int转16进制格式string;string中截取字符;16进制数加法;string转ByteAr
微信小程序實現折線面積圖-玫瑰圖-立體柱狀圖
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
Data conversion problem in Qt development serial communication software: when reading_ Qbytearray to string; When sending_ Data format; Int to hexadecimal format string; Intercept characters in string
Enter cnpm -v and cnpm appears: the file c:\users\19457\appdata\roaming\npm\cnpm.ps1 cannot be loaded because running scripts is prohibited on this system.
1000 okaleido tiger launched binance NFT, triggering a rush to buy
Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection
Cinq ans de contact avec près d'une centaine de patrons, en tant que chasseur de têtes, j'a i découvert que le secret de la promotion n'est que quatre mots
网络协议详解:TCP Part1
Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
程序员必备技能----断点调试(IDEA版)