Mysql中的基础函数_MySQL
时间函数 select curdate(); 返回2014-09-12,不包含时分秒 select curtime(); 返回14:13:22,不包含年月日 select now(); 返回2014-09-12 10:46:17 select unix_timestamp(now()); unix_timestamp(date)返回date的UNIX时间戳 select unix_timestamp('2013-09-01'); Unix时间戳是1970-01-01起经过的秒数,不考虑润秒,可能会遭遇2038年问题。 select from_unixtime(1184134516); 返回 2007-07-11 14:15:16,与unix_timestamp(date)互为逆操作。 select week(now()),year(now()); 返回36与2014, week(date)返回date是一年中的第几周,year(date)返回date的年份 select hour(now()),minute(now()); --返回10与57.hour(date)与minute(date)会返回所给时间的小时,分钟。 select monthname(now());返回September,返回月份英文
格式化日期值 date_format(date,fmt),按fmt格式化日期date值,让date随心所欲地展示特定格式。 select date_format(now(),'%M,%D,%Y'); 返回'September,12th,2014' select date_format(now(),'%Y-%m-%d %T'); 返回'2014-09-12 11:06:03'
所给日期差INTERVAL时间段的日期 select date_add(now(),interval 31 day); 返回'2014-10-13 11:10:17' ,这是31天后的日期。 select date_add(now(),interval '1_2' year_month); 返回2015-11-12 11:10:55,这是1年又2个月后的日期 select date_add(now(),interval 31 day); 与 select date_add(now(),interval,'-1_-2'); 用负数表示依然可以,查31天前的日期与1年又2个月之前的日期。
两个日期之间相差的天数 select datediff('2008-08-08',now()); 返回-2226
字符串函数连接字符串 select concat('aa','bb','cc'); --返回aabbcc select concat('aa',null); --返回null,因为与null连接就返回null
根据位置做替换与插入 select insert('zhongguoren',6,3,'shan'); --返回zhongshanren,从第6个字符起(包含第6个字符),连续的3个字符替换成'shan' select insert('zhongguoren',6,0,'shan'); --返回zhongshanguoren,在6个字符前插入'shan'
根据特定字符串做替换 select replace('zhongguo','guo','shan'); --返回'zhongshan'
小写大写 select lower('BEIJING'),upper('china');
返回左右子串 select LEFT('beijing2008',7),RIGHT('beijing2008',4) ; --返回beijing与2008
根据位置返回子串 select substring('beijing2008',4,8); --返回'jing2008' 返回第4个字符起(包含第4个字符,连续8个字符)
左右填充 select lpad('2008',20,'beijing'),rpad('beijing',20,'2008'); 返回beijingbeijingbe2008与beijing2008200820082 lpad(str,n,pad)就是将pad字符串在左边起循环填充,直到整个字符串达到n长度。
去除左右空格 select ltrim(' beijing'),rtrim('beijing '); --去除左右空格 select trim(' beijing ');
重复字符串 select repeat('mysql ',3); 返回'mysql mysql mysql'
比较字符串的ASCII码大小 select strcmp('abcd','dbca'); --返回-1 STRCMP(s1,s2),如果s1比s2小返回-1,相等返回0,大于返回1.
数值函数返回绝对值 select abs(-0.8),abs(0.8); --都返回0.8
天花板整数 select ceil(-0.8),ceil(0.8); --返回0与1
地板整数 select floor(-0.8),floor(0.8); --返回-1与0
返回x/y的模 select mod(5,3),5%3; --都返回2 mod(x,y)与x%y相同
返回0-1之间的随机值 select rand();
如果想生成0-100之间的随机整数 select ceil(100*rand());--返回84
返回四舍五入的值 round(x,y) 不填y则y默认是0 select round(1.1) --返回1 select round(1.1,2) --返回1.10 select round(103,0); --精确到个位,返回103 select round(103,-1); --返回100,精确到十位
返回截断值,与round()相近,区别是截断而不是四舍五入 select truncate(1.235,2); --返回1.23 select truncate(18.235,-1); --返回10
显示给定数的二进制值 select bin(4); --返回100
流程函数 if(value,t,f) 如果value是真,返回t,否则返回f if函数像oracle的decode select if(role_id=13,'distributor',if(role_id=14,'dealer',null)) as "角色类型" from upgrade.customer;
替换Null值函数 ifnull(v1,v2) 如果v1不为空,返回v1,否则返回v2
与if()函数作用一样的case写法 select case when salary
多分支case写法 select case salary when 1000 then 'low' when 2000 then 'mid' else 'high' end from salary;
其他函数 database() 返回当前数据库名 version() 返回当前版本 user() 返回当前登录用户名 inet_aton(ip) 返回IP地址的数字表示 inet_ntoa(num) 返回数字代表的IP地址 password(str) 返回字符串str的加密版本 md5(str) 返回字符串str的MD5值

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.
