Home Database Mysql Tutorial 贤者时间太久了么?--MySQL继续玩_MySQL

贤者时间太久了么?--MySQL继续玩_MySQL

May 30, 2016 pm 05:10 PM
time

1、MySQL

 

-----运算符和函数-----

 

字符函数,数值运算符,比较运算等

 

----字符函数

 

---

 

CONCAT()--字符连接

 

CONCAT_WS()--使用指定的分隔符进行字符连接

 

mysql> SELECT CONCAT('imooc','-','MySQL');

+-----------------------------+

| CONCAT('imooc','-','MySQL') |

+-----------------------------+

| imooc-MySQL |

+-----------------------------+

 

mysql> USE TEST;

Database changed

mysql> SELECT * FROM TEST;

+----+----------+

| id | username |

+----+----------+

| 1 | Tom |

| 2 | Ben |

+----+----------+

2 rows in set (0.02 sec)

 

mysql> SELECT CONCAT(id,username) AS fullname FROM test;

+----------+

| fullname |

+----------+

| 1Tom |

| 2Ben |

+----------+

 

以上是CONCAT()的实例,而CONCAT_WS()如下

 

mysql> SELECT CONCAT_WS('-',id,username) FROM test;

+----------------------------+

| CONCAT_WS('-',id,username) |

+----------------------------+

| 1-Tom |

| 2-Ben |

+----------------------------+

 

CONCAT_WS()需要至少三个参数,第一个是分隔符,后面才是需要连接的东西

 

---

 

FORMAT(),数字格式化

 

mysql> SELECT FORMAT(234234.23423,2);

+------------------------+

| FORMAT(234234.23423,2) |

+------------------------+

| 234,234.23 |

+------------------------+

 

就是标准化,当然是外国人那一套

 

---

 

LOWER()&UPPER(),大小写变换

 

---

 

LEFT()&RIGHT(),获取左右侧字符

 

需要两位参数,从哪个字段取,从第几位开始的左右侧

 

mysql> SELECT UPPER(LEFT('andy',1));

+-----------------------+

| UPPER(LEFT('andy',1)) |

+-----------------------+

| A |

+-----------------------+

 

---

 

LENGTH(),获取字符串长度。注意,空格也要算在内

 

---

 

LTRIM(),RTRIM(),TRIM(),删除前/后/前后的空格

 

TRIM还能删除其他字符

 

mysql> SELECT TRIM(LEADING '!' FROM '!!!ANDY!!');

+------------------------------------+

| TRIM(LEADING '!' FROM '!!!ANDY!!') |

+------------------------------------+

| ANDY!! |

+------------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT TRIM(BOTH '!' FROM '!!!ANDY!!');

+---------------------------------+

| TRIM(BOTH '!' FROM '!!!ANDY!!') |

+---------------------------------+

| ANDY |

+---------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT TRIM(TRAILING '!' FROM '!!!ANDY!!');

+-------------------------------------+

| TRIM(TRAILING '!' FROM '!!!ANDY!!') |

+-------------------------------------+

| !!!ANDY |

+-------------------------------------+

1 row in set (0.00 sec)

 

---

 

REPLACE()

 

mysql> SELECT REPLACE('!!ANDY!SDL!!','!','');

+--------------------------------+

| REPLACE('!!ANDY!SDL!!','!','') |

+--------------------------------+

| ANDYSDL |

+--------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT REPLACE('!!ANDY!SDL!!','!','LALAL');

+-------------------------------------+

| REPLACE('!!ANDY!SDL!!','!','LALAL') |

+-------------------------------------+

| LALALLALALANDYLALALSDLLALALLALAL |

+-------------------------------------+

1 row in set (0.00 sec)

 

可以看到,替换是比较灵活的

 

---

 

SUBSTRING()

 

mysql> SELECT SUBSTRING('MYSQL','1','2');

+----------------------------+

| SUBSTRING('MYSQL','1','2') |

+----------------------------+

| MY |

+----------------------------+

1 row in set (0.00 sec)

 

注意mysql是从1开始,不是从0开始数数

 

---

 

做匹配

 

mysql> SELECT 'mysql' LIKE 'M%';

+-------------------+

| 'mysql' LIKE 'M%' |

+-------------------+

| 1 |

+-------------------+

1 row in set (0.00 sec)

 

mysql> SELECT * FROM test WHERE username LIKE '%m%';

+----+----------+

| id | username |

+----+----------+

| 1 | Tom |

+----+----------+

 

这里,%代表任意,类似window中的*

 

_代表任意一个字符

 

----数值运算符以及函数

 

几个很通用的函数简单的介绍下

 

CEIL()-向上取整---------FLOOR()-向下取整

 

DIV()-整数除法,也就是结果只有整数

 

MOD()-取余数

 

POWER()-幂运算

 

ROUND()-四舍五入

 

TRUNCATE()-数字截取(不再四舍五入)

 

不是两位参数,就是一位参数,大家自己试试哈

 

----比较运算符与函数

 

---

 

[NOT] BETWEEN...AND...

 

mysql> SELECT 123 BETWEEN 2 AND 123123;

+--------------------------+

| 123 BETWEEN 2 AND 123123 |

+--------------------------+

| 1 |

+--------------------------+

 

---

 

[NOT] IN(),给定几个区间来做判断

 

mysql> SELECT 123 IN(123,23,12);

+-------------------+

| 123 IN(123,23,12) |

+-------------------+

| 1 |

+-------------------+

1 row in set (0.00 sec)

 

mysql> SELECT 123 IN(120,23,12);

+-------------------+

| 123 IN(120,23,12) |

+-------------------+

| 0 |

+-------------------+

 

---

 

IS [NOT] NULL,判断是空么

 

mysql> SELECT 0 IS NULL;

+-----------+

| 0 IS NULL |

+-----------+

| 0 |

+-----------+

 

常用在检查是否为空

 

mysql> SELECT * FROM test WHERE username IS NULL;

Empty set (0.00 sec)

 

----日期时间函数

 

---

 

NOW(),返回当时的日期和时间

 

CURDATE(),CURTIME()

 

---

 

DATE_ADD(),当前日期的变化

 

mysql> SELECT DATE_ADD('2012-2-23', INTERVAL 234 DAY);

+-----------------------------------------+

| DATE_ADD('2012-2-23', INTERVAL 234 DAY) |

+-----------------------------------------+

| 2012-10-14 |

+-----------------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT DATE_ADD('2012-2-23', INTERVAL -234 DAY);

+------------------------------------------+

| DATE_ADD('2012-2-23', INTERVAL -234 DAY) |

+------------------------------------------+

| 2011-07-04 |

+------------------------------------------+

1 row in set (0.00 sec)

 

---

 

DATEDIFF(),得到两个日期间的差值

 

mysql> SELECT DATEDIFF('2304-2-2','1234-3-22');

+----------------------------------+

| DATEDIFF('2304-2-2','1234-3-22') |

+----------------------------------+

| 390760 |

+----------------------------------+

1 row in set (0.02 sec)

 

---

 

DATE_FORMAT(),日期的格式化,日期的格式是可以选的,也就是说日期的格式转换

 

mysql> SELECT DATE_FORMAT('2013-2-22','%m/%d/%y');

+-------------------------------------+

| DATE_FORMAT('2013-2-22','%m/%d/%y') |

+-------------------------------------+

| 02/22/13 |

+-------------------------------------+

1 row in set (0.00 sec)

 

----信息函数

 

USER(),VERSION(),DATEBASE(),CONNECTION_ID(),LAST_INSERT_ID()

 

----聚合函数

 

只有一个返回值是他们的特点

 

AVG(),COUNT(),MAX(),MIN(),SUM()

 

直接调用可能会有错误,一般是针对数据表的字段进行的操作

 

mysql> SELECT AVG(id) FROM test;

+---------+

| AVG(id) |

+---------+

| 1.5000 |

+---------+

 

----加密函数

 

MD5(),PASSWORD()

 

mysql> SELECT MD5('HELLOWORLD');

+----------------------------------+

| MD5('HELLOWORLD') |

+----------------------------------+

| e81e26d88d62aba9ab55b632f25f117d |

+----------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT PASSWORD('HELLOWORLD');

+-------------------------------------------+

| PASSWORD('HELLOWORLD') |

+-------------------------------------------+

| *3456E7782A7F539BC823C715DB60231B0C7DE847 |

+-------------------------------------------+

1 row in set (0.00 sec)

 

一般而言,网页的编程用的都是MD5,password仅仅用于修改当前用户的密码

 

----

 

注重自带函数的熟悉、了解,灵活的调用和嵌套运用

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? Mar 11, 2024 pm 05:37 PM

On the Douyin platform, many users are eager to obtain level certification, and the level 10 light sign shows the user's influence and recognition on Douyin. This article will delve into the price of Douyin’s level 10 light boards and the time it takes to reach this level to help users better understand the process. 1. How much does a level 10 Douyin light sign cost? The price of Douyin's 10-level light signs will vary depending on market fluctuations and supply and demand. The general price ranges from a few thousand yuan to ten thousand yuan. This price mainly includes the cost of the light sign itself and possible service fees. Users can purchase level 10 light signs through Douyin’s official channels or third-party service agencies, but they should pay attention to legal channels when purchasing to avoid false or fraudulent transactions. 2. How many days does it take to create a level 10 fan sign? Reach level 10 light sign

How long does it take to clear the Elden Ring? How long does it take to clear the Elden Ring? Mar 11, 2024 pm 12:50 PM

Players can experience the main plot of the game and collect game achievements when playing in Elden's Circle. Many players don't know how long it takes to clear Elden's Circle. The player's clearance process is 30 hours. How long does it take to clear the Elden Ring? Answer: 30 hours. 1. Although this 30-hour clearance time does not refer to a master-like speed pass, it also omits a lot of processes. 2. If you want to get a better game experience or experience the complete plot, then you will definitely need to spend more time on the duration. 3. If players collect them all, it will take about 100-120 hours. 4. If you only take the main line to brush BOSS, it will take about 50-60 hours. 5. If you want to experience it all: 150 hours of base time.

Can linux reset system time? Can linux reset system time? Mar 13, 2023 am 10:50 AM

Linux can reset the system time. The reset method is: 1. Use the date command to check the time; 2. Use the "yum install ntp" command to install ntp; 3. Use the "ntpdate -u ntp.api.bz" command to implement network time Just sync.

How to remove hours, minutes and seconds from time in php How to remove hours, minutes and seconds from time in php Mar 13, 2023 am 11:20 AM

How to use PHP to remove hours, minutes and seconds from time: 1. Create a PHP sample file; 2. Use the strtotime function to convert the date and time into a timestamp; 3. Use the date function to format the date or time to remove the hours, minutes and seconds.

How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? Mar 24, 2024 pm 01:31 PM

Xiaohongshu, a platform full of life and knowledge sharing, allows more and more creators to express their opinions freely. In order to get more attention and likes on Xiaohongshu, in addition to the quality of content, the time of publishing works is also crucial. So, how to set the time for Xiaohongshu to publish works? 1. How to set the time for publishing works on Xiaohongshu? 1. Understand the active time of users. First, it is necessary to clarify the active time of Xiaohongshu users. Generally speaking, 8 pm to 10 pm and weekend afternoons are the times when user activity is high. However, this time period will also vary depending on factors such as audience group and geography. Therefore, in order to better grasp the active period of users, it is recommended to conduct a more detailed analysis of the behavioral habits of different groups. By understanding users’ lives

Why does my Go program take longer to compile? Why does my Go program take longer to compile? Jun 09, 2023 pm 06:00 PM

In recent years, Go language has become the choice of more and more developers. However, compared to other programming languages, the compilation speed of Go language is not fast enough. Many developers will encounter this problem when compiling Go programs: Why does my Go program take longer to compile? This article will explore this issue from several aspects. The compiler architecture of Go language The compiler architecture of Go language adopts a three-stage design, which are front-end, middle layer and back-end. The front-end is responsible for translating the source code into intermediate code in Go language, and the middle layer will

Detailed explanation of Linux file time viewing techniques Detailed explanation of Linux file time viewing techniques Feb 21, 2024 pm 01:15 PM

Detailed explanation of Linux file time viewing techniques In Linux systems, file time information is very important for file management and tracking changes. The Linux system records file change information through three main time attributes, namely access time (atime), modification time (mtime) and change time (ctime). This article details how to view and manage this file time information, and provides specific code examples. 1. Check the file time information by using the ls command with the parameter -l to list the files.

How to use the time and date modules in Python How to use the time and date modules in Python Oct 16, 2023 am 08:11 AM

How to use the time and date modules in Python Introduction: In programming, dealing with time and dates are very common tasks. Python provides powerful time and date modules, making time and date operations easier and more convenient. This article will introduce the time and date modules in Python and provide specific code examples to help readers better understand and apply them. 1. Introducing the time and date module Python’s built-in time and date module is the datetime module. We need to introduce this module first.

See all articles