Rumah > pangkalan data > tutorial mysql > Cara menggunakan pengisihan MySQL dan fungsi pemprosesan baris tunggal

Cara menggunakan pengisihan MySQL dan fungsi pemprosesan baris tunggal

王林
Lepaskan: 2023-06-01 13:04:06
ke hadapan
640 orang telah melayarinya

1. Isih

Mysql menyokong operasi pengisihan data Contohnya, sekarang kami mengisih gaji dari kecil ke besar:

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)
Salin selepas log masuk

Jika anda perlu mengisih mengikut tertib menurun. nyatakan desc: (lalai Isih dalam tertib menaik, jika anda nyatakan, nyatakan asc)

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)
Salin selepas log masuk

Untuk situasi yang lebih kompleks, susun berbilang medan:

Sebagai contoh, kami ingin mengisih mengikut gaji dalam tertib menaik, dan gaji adalah sama Dalam kes ini, susun mengikut nama dalam tertib menurun:

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)
Salin selepas log masuk

Susun dan cari berdasarkan syarat:

Ia dikehendaki mencari gaji antara 1250 dan 3500, susun mengikut gaji dalam susunan menurun:

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)
Salin selepas log masuk

2. Fungsi pemprosesan satu baris

memproses satu baris sebelum memproses baris seterusnya: (satu input sepadan dengan satu output)

tukar kandungan kepada huruf kecil

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)
Salin selepas log masuk

tukar kandungan kepada huruf besar

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)
Salin selepas log masuk

Dapatkan subrentetan

Contohnya: kami ingin mendapatkan huruf pertama bagi setiap nama:

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)
Salin selepas log masuk

String splicing

Menyambung empno dan ename setiap orang :

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)
Salin selepas log masuk

Cari panjangnya

Dapatkan bilangan aksara nama setiap orang:

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)
Salin selepas log masuk

Alih keluar tempat kosong di hadapan dan di belakang

Soal maklumat terperinci nama KING, tidak termasuk Ruang putih sebelum dan selepas:

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)
Salin selepas log masuk

Bulatkan

Kekalkan 0 tempat perpuluhan untuk 123.456

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)
Salin selepas log masuk

Jana nombor rawak

Jana perpuluhan rawak dari 0 hingga 1:

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)
Salin selepas log masuk

Penukaran nol

Hasil operasi untuk NULL dalam pangkalan data mestilah NULL , jadi ada fungsi pemprosesan NULL

Contohnya: Kira pendapatan tahunan setiap pekerja (gaji bulanan + bonus bulanan) :

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)
Salin selepas log masuk

Atas ialah kandungan terperinci Cara menggunakan pengisihan MySQL dan fungsi pemprosesan baris tunggal. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:yisu.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan