mysql 经典操作_SQL(基于MySQL)——经典用法大全

开头先声明一下,一般MySQL的语句命令因系统不同而有差异,有的只能大写,有的大小写通用。首先启动window下的Mysql服务:net start mysql

一、基础篇

小知识:

在SQL查询中:from后最多可以跟多少张表或视图:256

在SQL语句中出现 Order by,查询时,先排序,后取

在SQL中,一个字段的最大容量是8000,而对于nvarchar(4000),由于nvarchar是Unicode码。

1、说明:创建数据库

CREATE DATABASE yourDatabase;

2、说明:删除数据库drop database yourDatabase;

3、说明:创建新表create table yourTable(col1 type1 [not null] [primary key],col2 type2 [not null],..);

4、根据已有的表创建新表:

A:create table yourTable_new like yourTable _old ;

B:create table yourTab_new as select col1,col2… from yourTab_old definition only ;

5、说明:删除新表drop table yourTable

6、说明:增加一个列alter table yourTable add column col type

7、添加主键:Alter table yourTable add primary key(col)     删除主键:Alter table yourTable drop primary key(col)

8、创建索引:create [unique] index indexname on yourTable(col….)     删除索引:drop index indexname

注:索引是不可更改的,想更改必须删除重新建。

9、创建视图:create view viewname as select statement

删除视图:drop view viewname

10、说明:几个简单的基本的sql语句选择:select * from yourTable where范围

插入:insert into yourTable(field1,field2) values(value1,value2);

删除:delete from yourTable where 范围更新:update yourTable set field1=value1 where 范围

查找:select * from yourTable where field1 like ‘%value1%’;

排序:select * from table1 order by field1,field2 [desc]

总数:select count as totalcount from yourTable;

求和:select sum(field1) as sumvalue from yourTable;

平均:select avg(field1) as avgvalue from yourTable;

最大:select max(field1) as maxvalue from yourTable;

最小:select min(field1) as minvalue from yourTable;

11、分组:Group by:一张表,一旦分组 完成后,查询后只能得到组相关的信息。

组相关的信息:(统计信息) count,sum,max,min,avg 分组的标准)在selecte统计函数中的字段,不能和普通的字段放在一起;

二、提升篇

1、说明:子查询(表名1:a 表名2:b)select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

2、说明:显示文章、提交人和最后回复时间select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

3、说明:外连接查询(表名1:a 表名2:b)select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

4、说明:在线视图查询(表名1:a )select * from (SELECT a,b,c FROM a) T where t.a > 1;

5、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括select * from table1 where time between time1 and time2

select a,b,c, from table1 where a not between 数值1 and 数值2

6、说明:in 的使用方法s  elect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

7、说明:两张关联表,删除主表中已经在副表中没有的信息delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

8、说明:四表联查问题:select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

9、说明:日程安排提前五分钟提醒select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

10、说明:前10条记录select * from table1 where 范围 limit 10;

11、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

12、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表(select a from tableA ) except (select a from tableB) except (select a from tableC)

13、说明:随机取出10条数据

1.通过MYSQL内置的函数来操作,具体SQL代码如下:

SELECT * FROM tablename ORDER BY RAND()  LIMIT 10

2.不要将大量的工作给数据库去做,这样会导致数据库在某一集中并发时间内锁死并阻塞。建议通过代码随机生成一下1-X(总行数)之间的数字,然后将这10个随机数字作为查询条件,具体语句如:

SELECT * FROM tablename where ID in (2,8,4,11,12,9,3,1,33)

可能你还要进行重复排除,并且需要在程序中将10个值串联并连接进入SQL语句中。

14、说明:随机选择记录SELECT * FROM tablename ORDER BY RAND()  LIMIT 1;

15、说明:删除重复记录1)delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

2) select distinct * into temp from tablenamedelete from tablenameinsert into tablename select * from temp

评价: 这种操作牵连大量的数据的移动,这种做法不适合大容量但数据操作

备注:单引号,双引号对于MySQL来说没什么区别,但如果你是把SQL语句放到String等变量里面,还是用单引号比较好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值