MySQL的一些基本查詢,創建存儲過程等


 常用的查詢條件有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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM