您好,欢迎访问一九零五行业门户网

13个mysql数据库的实用SQL小技巧

http://www.gbtags.com/gb/share/2351.htm mysql作为最成功的开源关系型数据库之一,拥有大批的粉丝(本人也是),在这篇文章中,我们精心收集了10个最实用的mysql查询技巧,希望能够带给大家惊喜,如果大家也有非常不错的sql,请留言与我们分享! sql,mysq
http://www.gbtags.com/gb/share/2351.htm
mysql作为最成功的开源关系型数据库之一,拥有大批的粉丝(本人也是),在这篇文章中,我们精心收集了10个最实用的mysql查询技巧,希望能够带给大家惊喜,如果大家也有非常不错的sql,请留言与我们分享!
sql  ,mysql
使用case来重新定义数值类型
select id,title,(case date when '0000-00-00' then '' else date end) as date from your_tableselect id,title,(case status when 0 then 'open' when 1 then 'close' else 'standby'end) as status from your_table

查找重复的email记录字段
select email, count(email) as q from emails_table group by email having q >1 order by q desc

取出随机顺序得到记录
select * from your_table order by rand()

使用update替换指定字段里的字符
update your_table set name=replace(name,'gbin1.com','gbtags.com') where name like '%john%';

重设置指定table里的自动增加数值
alter table your_table auto_increment =2
下一次你插入数据,那么id自动增加为2
为select语句添加一个自动增加的字段列
set@n=0;select @n:=@n+1 as number, name, surname from gbtags_users;

使用concat来连接字段
select concat(name,' ',surname) as complete_name from users
使用date方法来取出日期类型的指定部分
select id,title, year(date_field) from your_tableselect id,title, concat(month(date_field),'/',year(date_field))as new_date from your_table
上面语句,第一个select取出了年份,第二取出了月份和年份组合
针对unique key(唯一键)类型字段的重复插入不报错的insert语句
insert ignore into tags (tag) values ('good');
这个insert语句可以执行多次,不会报错,重复的插入会被忽略
使用全文索引并且匹配搜索
select*from articles where match(content_column) against ('music')
你需要首先添加全文搜索索引到指定的列(content_column)。注意如果你表里已经有数据的话,不会创建索引,所以你需要使用一个空的表来执行这个语句
如果查询一个月以前
select user, count(*) as logins from stat_log where action='login' and dt_when >= date_add(curdate(), interval -1 month) group by user

这个语句能够让你查询字段dt_when一个月以前的所有记录
设置正确的字符集
set names 'utf8';
请在连接开始后执行这个语句
从一个表插入另外一个表
insert into yourtable (field1,field2,field3) select newfield1,newfield2,'fixed value' from yourtable2

这个语句能够快速的帮助大家快速的添加一个表中的特定内容到另外一个表中。
希望这里我们介绍的这些sql能够帮助大家更好更快的使用mysql,如果大家也有很多自己保存的sql,请大家给我们留言!
其它类似信息

推荐信息