码迷,mamicode.com
首页 > 数据库 > 详细

mysql 常用命令

时间:2017-11-25 18:29:39      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:排序   blog   mini   let   alter   arc   10个   名称   表数   

★增系列★
----复制表(带数据)----
create table 表名 select *from 老表名

----复制表(不带数据)----
create table 表名 like 老表名

----插入已有表的数据到新表(格式一样)----
insert into 新表 select *from 旧表

----表中加入多个记录----
insert into 表名 values (),(),(); 其中括号里为表的结构

★删系列★
----删除表-----
drop table if exists 表名

----删除表中所有数据-----
delete from 表名

----删除表中的某个数据-----
delete from 表名 where 条目1 = xxx

----删除表中的某个重复数据的记录,保留id最小的一个数据-----
delete from 表名 where id not in (select minid from (select min(id) as minid from person group by 条目) b);
★改系列★
----修改字段名称----
alter table 表名 change 旧字段名 新字段名 类型

★查系列★
----查询一张表数据个数-----
select count(*) from 表名

----一张表某个id数据个数-----
select count(id) from 表名

----查询表某个id最大数据对应的整条数据-----
select *from 表名 where id in (select max(id) from 表名)

----查询某个数据的上一条数据,以id为标识-----
select *from 表名 as a where a.id > (select id from 表名 as b where a.id > b.id order by id desc limit 0,1)


★常用语句系列★
order by 排序:
select *from 表名 order by desc\asc

limit 分页,限制:
放末尾,格式常见为 limit 取记录起始位置(0为起始,如果省略则视作0),待取记录个数(为-1则取到末尾) \\ limit 3,10 表示从第4个数据起往后取10个。

join 拼接:
left join 表名1 on 表名2 // 以表1为基准,将表2按照表1的格式拼过来,多余的部分截掉,少的部分补NULL

exists 用于判断条件是否存在,成立,如果成立返回true,常见定式为:
select 条目1 from 表名 where exists(select 条目2 from 表名 where t1 > t2)
更详细的exists见:https://www.cnblogs.com/glory-jzx/archive/2012/07/19/2599215.html

group by 分组,能够将某列相同的信息合并成1行进行处理,经常与having连着使用,常见定式:
select *from 表名 group by 条目2 having count(条目) > 100

in 和 not in 表示某个字段是否存在,常用定式:
where 条目 in/ not in (select *from where 条目1 > 条目2)

mysql 常用命令

标签:排序   blog   mini   let   alter   arc   10个   名称   表数   

原文地址:http://www.cnblogs.com/qinqinkittay/p/7895731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!