經常使用的查詢條件有一、比較:=,<,>,<=,>=,!=,<>,!>,!<
二、肯定範圍:between and,not between and
三、肯定集合:in,not in
四、字符匹配:like,not like
五、空值: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
下午----建立存儲過程----------------------------------------------------------------------------------sql
在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' 這裏:=也是賦值,賦值後會在屏幕上顯示出來ssh
@@開頭的變量是MySQL內置的系統變量
顯示存儲過程的基本信息:show procedure status\G;
顯示存儲過程的建立信息:show create procedure sp1;函數
調用存儲過程時輸入參數插入數據表中的存儲過程的建立以下:sqlserver
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,'王五');
-------------------------------------------------------------------------server
建立存儲過程時即插入數據到數據表中的存儲過程的建立
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();
---------------------------------------------------------------------------------------------ip
首先設定一個全局變量@x,將@x的值傳入存儲過程當中的變量a以後,經在存儲過程當中計算後得出的結果又賦值給@x從存儲過程當中傳出來,舉例以下:it
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)
-----------------------------------------------------------------------------io
再舉一個簡單例子:
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。