您的位置:首页 > 数据库 > MySQL

MySQL的一些基本查询,创建存储过程等

2013-07-19 21:52 513 查看
常用的查询条件有1、比较:=,<,>,<=,>=,!=,<>,!>,!<
2、确定范围:between and,not between and
3、确定集合:in,not in
4、字符匹配:like,not like
5、空值:is null, is not null

下面举个确定范围的简单例子:
select * from s2 where a between '2012-2-1' and '2013-2-2';
select * from s2 where a >='2012-2-1' and a<='2013-2-2';

在微软的sqlserver中取前三条记录用top如:select top 3 * from stu order by score desc;
而在Mysql中取前三条记录用limit如: select * from stu order by score desc limit 3; -- 按成绩降序排列,只取前三条
select * from stu order by score desc limit 2,3;-- 按成绩降序排列,从第二条开始取三条记录,limit是从0开始计数的 (分页用到limit)

bin() 函数
传一个十进制给它,它将十进制转换为二进制
eg:
mysql> select bin(255) as 'binary';
+----------+
| binary |
+----------+
| 11111111 |
+----------+
1 row in set (0.00 sec)

like模糊查询
这里%代表任意0个或多个字符eg:
mysql> select * from stu where stuName like'张%';
+---------------------+---------------------+-------+-----+-----------+
| a | b | price | sno | stuName |
+---------------------+---------------------+-------+-----+-----------+
| 2012-10-14 00:00:00 | 2013-07-19 09:11:17 | 100.0 | 1 | 张三 |
| 2013-07-18 18:00:11 | 2013-07-19 09:10:57 | 120.0 | 2 | 张三丰 |
| 2013-07-18 18:00:47 | 2013-07-19 09:10:57 | 170.0 | 3 | 张三丰 |
| 2012-02-04 00:00:00 | 2013-07-19 09:10:57 | 370.0 | 4 | 张三丰 |
+---------------------+---------------------+-------+-----+-----------+

这里_表示一定要有一个字符,且只有一个.eg:
select * from stu where stuName like '张_';
(如果查询出乱码的话,先查看一下客户端字符集是否是utf8,若不是则 set names utf8;将客户端字符集改为utf8,再更新开始插入的姓名,再进行查询。)

max(price)取价格最高的 min(price)取价格最低的 avg(price)求价格的平均值 sum(price)求价格的总和 count(*)计数,计算有多少条记录等
用法如下:
mysql> select type,max(price) ,count(1) from prod group by type;
+---------+------------+----------+
| type | max(price) | count(1) |
+---------+------------+----------+
| 类别1 | 200.5 | 2 |
| 类别2 | 200.5 | 2 |
+---------+------------+----------+
2 rows in set (0.00 sec)

mysql> select type,max(price) ,count(1) from prod group by type with rollup;
+---------+------------+----------+
| type | max(price) | count(1) |
+---------+------------+----------+
| 类别1 | 200.5 | 2 |
| 类别2 | 200.5 | 2 |
| NULL | 200.5 | 4 |
+---------+------------+----------+
3 rows in set (0.00 sec)
mysql> select deptID,max(stuID),count(1) from stu where stuID>3 group by deptID having count(1)>2;
分完组后的刷选一定是用having,而不是用order by

多表查询
内连接 inner join
select stuName,deptName from stu,dept where stu.deptID=dept.deptID;
这两条语句查询结果是一样的,只是下面这条用了inner join 内连接,其中on还可以改为where,但是在微软的sqlServer中on 不能改为where。
select stuName,deptName from stu inner join dept on stu.deptID=dept.deptID;

select stuName,coursesName,score from stud,courses,sc where stud.sno=sc.sno && courses.coursesID=sc.coursesID;
在两张表以上的内连接时,on就不能改为where了,所以在内连接时,不建议使用where.
select stuName,score,coursesName from stud inner join sc on stud.sno=sc.sno

左外连接 left join
inmysql> select stuName,score from stud left join sc on stud.sno=sc.sno;
+---------+-------+
| stuName | score |
+---------+-------+
| tom | 80 | 左外连接会将left join左边这张表的所有字段都显示出来
| tom | 70 |
| jerry | 90 | 右外连接跟左外连接相似,只是将right join右边的这张表的所有字段
| mike | NULL | 都显示出来
+---------+-------+
4 rows in set (0.00 sec)

mysql> select stuName,score from stud inner join sc on stud.sno=sc.sno;
+---------+-------+
| stuName | score |
+---------+-------+ 而内连接只显示满足条件的
| tom | 80 |
| tom | 70 |
| jerry | 90 |
+---------+-------+
3 rows in set (0.00 sec)ner join courses on sc.coursesID=courses.coursesID;

mysql> select stuName,coursesName,score from stud left join sc on stud.sno=sc.sno left join courses on sc.coursesID=courses.coursesID;
+---------+-------------+-------+
| stuName | coursesName | score |
+---------+-------------+-------+
| tom | c | 80 |
| tom | ds | 70 |
| jerry | ds | 90 |
| mike | NULL | NULL |
+---------+-------------+-------+
4 rows in set (0.00 sec)

子查询
不推荐,因为效率不高
select * from emp where deptID = (select deptID from dept order by deptID limit 1);

select deptID into @a from dept where deptName = ‘cc’;
update … where deptID = @a;

记录联合
union all -- union去重复 sqlserver可用,mysql中不可用,会出错
select * from stu union all select * from stu2; 会有重复的记录
select * from stu union select * from stu2; 自动去掉了重复的记录

下午----创建存储过程----------------------------------------------------------------------------------

在MySQL中声明变量给变量赋值取变量的值
局部变量
-- declare声明的即局部变量
-- 在declare语句前不能有任何非declare语句
drop procedure if exists sp1;
delimiter // -- 定义为碰到//才表示语句结束
-- 如果没有将结束符定义为//的话,那么下面这个创建存储过程碰到;就结束了,根本执行不下去
create procedure sp1()
begin -- 体
declare a int;
declare b,c,d float default 3.14;
set a=10;-- 若不给a赋值,则a的值为null,若b,c,d无默认值且未赋值,则也为null
set b=1.23,c=2.34;
select a,b,c,d;
end//
delimiter ; -- 定义为碰到;表示语句结束
call sp1(); -- 调用存储过程时最好加上(),避免出错
drop procedure if exists sp1; -- 删除存储过程时不需要加();
全局变量 一个@开头的
全局变量可以一赋值马上就用 eg: mysql> set @a=10

select @c :='xyz' 这里:=也是赋值,赋值后会在屏幕上显示出来

@@开头的变量是MySQL内置的系统变量
显示存储过程的基本信息:show procedure status\G;
显示存储过程的创建信息:show create procedure sp1;

调用存储过程时输入参数插入数据表中的存储过程的创建如下:

drop procedure if exists sp1;
delimiter //
create procedure sp1(id int,name varchar(32))
begin
-- create table,insert into,select
create table if not exists stu2(
stuId int,
stuName varchar(16))engine=innodb charset=utf8;
insert into stu2 values(id,name);
select * from stu2;
end //
delimiter ;
call sp1(10,'王五');
-------------------------------------------------------------------------

创建存储过程时即插入数据到数据表中的存储过程的创建
drop procedure if exists sp1;
delimiter //
create procedure sp1()
begin
-- create table,insert into,select
create table if not exists stu2(
stuId int,
stuName varchar(16))engine=innodb charset=utf8;
insert into stu2 values(1,'张三'),(2,'李四');
select * from stu2;
end //
delimiter ;
call sp1();
---------------------------------------------------------------------------------------------

首先设定一个全局变量@x,将@x的值传入存储过程中的变量a之后,经在存储过程中计算后得出的结果又赋值给@x从存储过程中传出来,举例如下:

drop procedure if exists sp1;
delimiter //
create procedure sp1(inout a int)
begin
set a=a+10;
end //
delimiter ;
mysql> set @x =20;
Query OK, 0 rows affected (0.00 sec)
mysql> call sp1(@x);
Query OK, 0 rows affected (0.01 sec)
mysql> select @x;
+------+
| @x |
+------+
| 30 |
+------+
1 row in set (0.00 sec)
-----------------------------------------------------------------------------

再举一个简单例子:

drop procedure if exists sp1;
delimiter //
create procedure sp1(a int ,b int,out c int)
begin
select a + b into c;
end //
delimiter ;
call sp1(10,20,@x);select @x;
eg:mysql> call sp1(10,20,@x);
Query OK, 1 row affected (0.00 sec)

mysql> select @x;
+------+
| @x |
+------+
| 30 |
+------+
1 row in set (0.00 sec)

-----------------------------------------------------------------------------------------------------------------

今天所学的差不多就这些了,还有些不怎么明确的也就没写出来。在连接别台机子的数据库时,可以用[root@localhost ~]# ssh root@10.0.0.254 这条命令连接ip地址为10.0.0.254这台机子的数据库,但是前提是你得知道这机子的密码。但是还是不知道怎么连接别台机子装的mysql-5.6.11。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: