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

最新下载

热门教程

mysql 基本操作记录语句

时间:2013-09-16 编辑:简简单单 来源:一聚教程网

 

以下以数据库”ceshi”为例

1、连接数据库

mysql -u username -p password

2、创建/删除数据库

创建:create database ceshi;

删除:drop database ceshi;

3、创建/删除数据表

创建:create table students (sid int(10) auto_increment primary key,name varchar(255),course varchar(255),score int(10)) ;

删除:drop table students;

设置数据表编码:alter table `students` default character SET utf8 collate utf8_general_ci;

4、插入数据

    单条插入 :insert into students (name,course,score) values(value1,value2,value3);
    多条插入:insert into students (name,course,score) select value1[0],value1[1],value1[2] union select value2[0] ,value2[1],value2[2] union……
    从另外的一张表中读取多条数据添加到新表中:insert into students(col1,col2,col3) select a,b,c from tableA ;
    从其他的多张表中读取数据添加到新表中:insert ioto tableName(col1,col2,col3)  select a,b,c from tableA where a=1  union all select a,b,c from tableB where a=2            上边代码中的union all如果换成union,则相同记录只插入一次,不会重复插入。
    上边代码中的into都可以省略!

5、order by语句

select * from students order by score (asc);    从低往高排,默认,asc可省去

select * from students order by score desc;      从高往低排

6、group by语句

select * from students group by course;       查询数据按课程分组,只显示查询到的第一条

select * from students group by course order by socre;         order by 必须在 group by之后,group by 比order by先执行,order by不会对group by 内部进行排序,如果group by后只有一条记录,那么order by 将无效。要查出group by中最大的或最小的某一字段使用 max或min函数。


--====================简单的查增删改===========

  --查看学生表的全部数据

  select * from studio

  --插入一个新的学生信息

  insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黄兰淇",0,36,'南充','13943943334')

  --查看class全部数据

  select * from class

  --向class表增加两条条数据

  insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新电实训班','GXA-ncs-001','2008-03-11','都是很优秀的朋友')

  insert into class(cl_class,cl_coding,cl_o_time) values('阿坝师专实训班','GXA-ABSZ-001','2008-03-11')

  --更新一条的数据 条件的重要性

  update class set cl_remark='真的是不错' where cl_id=5

  --删除一条数据 条件的重要性

  delete from class where cl_id=7

  --修改列标题

  select cl_id as '班级主键',cl_class as '班级名称' from class

  select 名字=st_name from studio

  --使用文字串

  select '名字是:',st_name from studio

  --=============条件稍微复杂点的查增删改==============

  --主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null

  --查询cl_id 大于 1 的所有信息

  select * from class where cl_id>1

  --使用 or

  select * from class where cl_id<>10 or cl_class='百杰一班'

  --使用and

  select * from class where cl_id<>10 and cl_class='百杰一班'

  --使用like 和 %

  select * from class where cl_class like '百杰%'

  select * from class where cl_remark like '%上午%'

  --使用 between

  select * from class where cl_id between 3 and 5

  --使用 between 配合上 not

  select * from class where cl_id not between 3 and 5

  --使用 is not null

  select * from class where cl_remark is not null

  --使用 in

  select * from class where cl_class in('千星一班','百杰二班')

  --=================使用数学运算符======================

  --主要涉及到 + = *

  --查询Java相关课程分别要上多少周 按照每周5天,每天6节课来计算

  select '结果'=co_num/5/6 from course where co_name in ('Java基础','Java项目入门')

  --==================使用汇总函数 ========================

  --涉及到COUNT SUM AVG MAX MIN

  --查询课时数小于50的课程一共有多少门

  select count(*) from course where co_num<50

  --查询所有课程一共多少课时

  select sum(co_num) from course

  --计算全部课时费,假设每节课50块钱

  select sum(co_num)*50 from course

  --查询课时最少的课程

  select min(co_num) from course

  --查询课时最多的课程

  select max(co_num) from course

  --查询平均每门课多少课时

  select avg(co_num) from course

 

聚集函数:
最大值:max()       最小值 min()    平均值avg()
求和:   sum()        汇总: count ()
如:求每个部门的基本工资平均值
select 部门,avg(基本工资)  as 部门基本工资 from 员工表 group by 部门
显示平均基本工资大于3000的部门
Select  部门,avg(基本工资) from 员工表 group 部门 where avg(基本工资)>3000
此句错误。SQL规定在分组中使用条件不能用 Where 而是用 having
Select  部门,avg(基本工资) from 员工表 group by  部门 having avg(基本工资)>3000

八、多表查询:
一个数据库中的多个表,存在一定的联系,怎么样正常的显示这么表的信息?
现在有三个表:
yg
Name  sex  age 
宋洋  男   20 
马冬旭 女   40 
 Gs
Name   title      date       单位
宋洋   AD详解 2006-11-10  清华大学
马冬旭 linux    2005-01-01   人民大学
 dz
单位     地址
清华大学  五道口
人民大学   黄庄

第一种方法称为:交叉连接,在SQL SERVER中又称为笛卡尔乘积
但是要注意的默认生成的记录总数是两表记录之积
select * from yg,gs;
select * from yg,gs where yg.name=gs.name;
这才是我们想要的记录
第二种方法是用join连接:
内连接
select * from yg join gs on yg.name=gs.name
左外连接
右外连接
但没有全外连接

九、联合:
除了连接,mysql4。0以上的版本还支持UNION运算符,它用来把多个select查询号的输出连接成一个单独的结果集。大多数情况下,这个运算符用来把查询产生的结果集添加到不同的表,同时创建包括所有结果的一个单独表。比如面试的时候问你,有两个表,字段信息一样,让你用一条语句把两个表的信息组合为一个单独的表!
为了说明UNION运算符的使用方法,我们举一个例子:现在有两个表,分别存放的是男同学信息和女同学信息,如果用一个语句将所有同学的信息显示出来!
mysql> select * from nan;
+--------+-------+
| name   | score |
+--------+-------+
| 彭聪留 |    80 |
| 费优优 |    81 |
| 曲权   |    82 |
+--------+-------+
3 rows in set (0.00 sec) mysql> select * from nv;
+------+-------+
| name | score |
+------+-------+
| 彭红 |    80 |
| 费红 |    81 |
| 曲红 |    82 |
+------+-------+
3 rows in set (0.00 sec)
mysql> select * from nan union select * from nv;
+--------+-------+
| name   | score |
+--------+-------+
| 彭聪留 |    80 |
| 费优优 |    81 |
| 曲权   |    82 |
| 彭红   |    80 |
| 费红   |    81 |
| 曲红   |    82 |
+--------+-------+
6 rows in set (0.00 sec)
那如果有三个表怎么办?也是一样的操作!
但注意的是如果两个表的某条记录信息完全一致,则只显示为一条;如果想显示全部记录则在union后 加 all
mysql> select * from nan union all  select * from nv;
如果面试官又问你,如果想把显示的信息保存到一个表中怎么办?
mysql> create table 表名 select 语句;

热门栏目