一聚教程网:一个值得你收藏的教程网站

热门教程

mysql常用日期 字符串处理函数命令

时间:2022-11-14 23:37:18 编辑:袖梨 来源:一聚教程网

函数如下:
left,right 字符串截取
from_unixtime 格式化unix时间戳
concat 字符串连接函数
max 取某列最大值
min  取某列最小值
sum  计算某列的和
count 统计条数
md5  返回md5加密码的串
format  格式化数字为xx,xxx,xxx.xxxx格式 比如1,1000.123
length 计算某个字符串长度
distinct 去重复
replace 替换字符串
in  指定查询某个值的记录
like 模糊查询
is null 查询某个条件为空(null),注:null不等于”"
is not null 查询某个条件不为为空(null)
MATCH … AGAINST … mysql的全文索引查询

mysql left,right函数
left和right是一对截取字符串前几位可后几位的函数,left是从左向右开始计算,right相反是从右向左计算
例:
select left(name,10) as name from user; 显示用户名的前10位
select right(name,10) as name from user; 显示用户名的后10位
select * from user where left(datetime,10)=”2011-12-02″ 取出2011-12-02日注册的用户
select * from user where left(datetime,7)=”2011-12″ 取出2011-12月注册的用户
left,right不仅仅可以用于截取字符串,还可以用在where条件上。特别是用在查询条件上他的作用非常大。

mysql from_unixtime函数
from_unixtime函数用来对unix时间戳进行格式化,格式化成我们易读的日期时间格式。
例:
select from_unixtime(time, “%Y-%m-%d %H:%i:%s” ) as datetime from table; 把time字段格式化成易读的日期时间显示(time为unix时间戳)
select * from table where left(from_unixtime(time, “%Y-%m-%d” ))=’2011-12-02′ 取出2011-12-02日的记录

mysql concat 函数
concat函数 可以用来把某二个字符连接在一起查询或显示,也可以把字段和字符串进行连接。
例:
select concat(year,”-”,month,”-”,day) as datetime from table; 把表中year,month,day字段连接起来显示
select concat(“My name is:”,name) as name from table; 把字符串和字段连接起来显示
update software set icon=concat(“http://111com.net”,icon); 把数据库中icon批量更新并在原有的icon前增加域名 111com.net

mysql max,min函数
顾名思义max函数用于查询某个字段中的最大值
例:
select max(age) from user; 返回最大的年龄
select min(age) from user; 返回最小的年龄

mysql sum函数
sum函数 可对某个字符(int型)进行求和
例:
select sum(money) from user 计算出所有人的金钱总数
select sum(money),area from user group by area 计算出各地区人员的金钱总数

mysql count函数
统计聚合函数,可对sql查询的结果进行统计
例:
select count(*) as total from user 计算出总会员 111com.net

mysql md5函数
同php中的md5一样,对某个字符串进行加密
例:
select md5(password) as password from user 把明码的密码进行md5加密显示
insert into user(name,password) values(“abc”,md5(“abc”)); 写入user表把密码进行md5加密后存储

mysql format函数
用于格式化数字为xx,xxx.xxx格式的数字
例:
select format(downloads) as download from software; 把下载量格式化为xx,xxx格式如:12,000
select format(money,2) as money from user; 把用户金钱格式化为xx,xxx.xx格式,参数2为精确的小数点位数如:12,000.05

mysql length函数
计算某个字段值的长度
例:
select length(name) as length from user; 显示出用户名的长度
select * from table where length(aa) > 10 ; 查询某字段长度大于10的记录 php程序员站

mysql distinct函数
对某个字段去重复,(在某些时候group by也可以做到)
例:
select distinct(area) from user; 对地区进行去重复
select area,count(*) from user group by area; 对地区进行聚合并统计出数量

mysql replace函数
查找某个字符串并进行替换
例:
select replace(icon,”www.111com.net”,”img.111com.net“) from software; 把icon中的www.111com.net替换成替换成img.111com.net显示
update software set icon=replace(icon,”www.111com.net”,”img.111com.net“) ; 把数据库中icon的域名批量进行查找替换www.111com.net

mysql in函数
可批量指定几个值作为查询条件
例:
select * from user where user_id in(1,2,3,4,5,100,200,333)
select * from user where user_name in(“a”,”b”,”d”)

mysql like函数
可对某个字段进行模糊查询,”%”号用于匹配任意字符
例:
select * from user where name like “%王%”; 查询所有用户名中带”王”字符的用户
select * from user where name like “%王”; 查询所有用户名第一个字符为”王”字的用户

mysql is null函数
匹配某个字符为null值的记录,注:null不代表空符串”"
例:
select * from user where a is null ; 查询a字段为null的用户
select a.* from user as a left join add_user as b on a.user_id=b.user_id where b.user_id is null; 连表查询附加表add_user中没有附加用户信息数据的用户

mysql is not null函数
和is null用法一样,匹配某个字符不为空的记录

mysql MATCH … AGAINST 全文匹配函数
mysql的全文匹配函数,要使用此函数查询的字符必须增加了全文索引,另外mysql不支持中文全文索引,所以国人在开发中估计很少用到此函数。
match中包含要进行全文匹配的字段,多个字段用”,”号分割 against为匹配的字符串
例:
select * from software where match(title,body) against(“php”); 全文匹配title和body字段中包含”php”的记录
select * from software where match(title) against(“php mysql”); 全文匹配title字段中包含”php mysql”的记录。


mysql常用命令
1、mysql服务的启动和停止
service mysqld start
service mysqld stop

2、登陆mysql
语法如下: mysql -u用户名 -p用户密码
键入命令mysql -u root -p, 回车后提示你输入密码,例如输入123456,然后回车即可进入到mysql中了。
mysql的提示符是:mysql>

3、显示数据库列表
show databases;

缺省有两个数据库:mysql和test。mysql库存放着mysql的系统和用户权限信息,我们改密码和新增用户,实际上就是对这个库进行操作。

4、显示库中的数据表
use mysql; //选中一个数据库
show tables;

5、显示数据表的结构
describe 表名;

显示该数据表由哪几个字段组成,数据类型分别是什么。

6、显示表中的记录
select * from 表名;

7、赋予用户权限同时添加新用户
格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"
例如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:
grant select,insert,update,delete on *.* to user1@localhost identified by "password1";
赋予全部权限
grant all on *.* to user1@localhost identified by "password1";

如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。

8、重新加载授权表
flush privileges;

9、删除数据库中用户
use mysql;
delete from user where user="user1" and host="localhost";

10、建库与删库
create database 库名;
drop database 库名;

11、建表与删表
use 库名;
create table <表名> (<字段名1> <类型1> [,..<字段名n> <类型n>]);
drop table 表名;

例如:
create table class(id int(4) not null primary key, name char(20) not null,degree double(16,2));
建表时要设计字段数据类型,是否为主键等多种属性。

12、往表中加入记录
insert into class values ("0001","zhang","99.8");

13、更新表中数据
update class set degree="100.1" where id="0001";

14、清空表中记录
delete from 表名;

15、退出mysql
exit;

热门栏目