SQL常用命令一直記得很模糊幾乎每次使用的時候都需要上網(wǎng)查詢,感覺數(shù)據(jù)庫查詢,應該是每個程序員的基本功啊,所以集中時間學習和練習了一些SQL語句的常用命令。這里先推薦兩個網(wǎng)站W3School在線學習網(wǎng)站和SQL Fiddle在線練習網(wǎng)站。希望能夠幫助到大家~~~
初始化Test表
- 創(chuàng)建Test表
DROP TABLE IF EXISTS test;
CREATE TABLE test (
_id integer PRIMARY KEY AUTOINCREMENT,
empno integer(11) NOT NULL,
name varchar NOT NULL,
job varchar,
mgr integer(11) ,
hirdate date NOT NULL,
sal double (8, 2),
comm double(8,2),
deptno integer(11)
);
- 插入數(shù)據(jù)
INSERT INTO test
(empno,name,job,mgr,hiredate,sal,comm,deptno)
VALUES
('7369', 'SMITH', 'CLERK', '7902', '2011-03-12', '800.00', null, '20'),
('7499', 'ALLEN', 'SALESMAN', '7698', '2012-03-12', '1600.00', '300.00', '30'),
('7521', 'WARD', 'SALESMAN', '7698', '2013-03-12', '1250.00', '500.00', '30'),
('7566', 'JONES', 'MANAGER', '7839', '2011-03-12', '2975.00', null, '20'),
('7654', 'MARTIN', 'SALESMAN', '7698', '2011-03-12', '1250.00', '1400.00', '30'),
('7698', 'BLAKE', 'MANAGER', '7839', '2011-03-12', '2850.00', null, '30'),
('7782', 'CLARK', 'MANAGER', '7839', '2015-03-12', '2450.00', null, '10'),
('7788', 'SCOTT', 'ANALYST', '7566', '2011-03-12', '3000.00', null, '20'),
('7839', 'KING', 'PRESIDENT', null, '2011-03-12', '5000.00', null, '10'),
('7844', 'TURNER', 'SALESMAN', '7698', '2014-03-12', '1500.00', '0.00', '30'),
('7876', 'ADAMS', 'CLERK', '7788', '2016-03-12', '1100.00', null, '20'),
('7900', 'JAMES', 'CLERK', '7698', '2015-03-12', '950.00', null, '30'),
('7902', 'FORD', 'ANALYST', '7566', '0000-00-00', '3000.00', null, '20'),
('7934', 'MILLER', 'CLERK', '7782', '2011-03-12', '1300.00', null, '10');
簡單查詢
- 查詢表中的所有數(shù)據(jù)
SELECT *
FROM test
- 查詢所有員工的姓名、工號和崗位。
SELECT name,empno,job
FROM test
- 查詢所有員工的年薪
Mysql中支持 + - * / %等數(shù)學運算。
-- as 起別名使查詢結果更直觀
SELECT name,sal * 12 as totalSal
FROM test
- 查詢結果的拼接顯示
SELECT CONCAT(ename,"的年薪為",sal * 12,"美元") AS total
FROM t_employee
CONCAT 采用數(shù)量可變的字符串自變量并將它們連接到單個字符串。 它需要至少兩個輸入值;否則將引發(fā)錯誤。 所有參數(shù)都隱式轉換為字符串類型,然后串聯(lián)在一起。 Null 值被隱式轉換為空字符串。
- 結果:
total |
---|
SMITH的年薪為9600.00美元 |
ALLEN的年薪為19200.00美元 |
WARD的年薪為15000.00美元 |
JONES的年薪為35700.00美元 |
條件查詢
條件查詢的完整語句結構如下:
select field1,field2,field3... from 表名 where 條件;
條件中,支持下列內容
- 關系運算符和邏輯運算符關系運算符:
-
>
<
=
!=
>=
<=
-
- 邏輯運算符:
-
and
&&
or
||
xor(異或)
not
!
-
- between… and … : 范圍查詢
- is null / is not null: 是否為null/是否不為null
- in:枚舉類型范圍查詢
- like : 模糊查詢
- 查詢工作為CLERK,并且薪水大于800的員工信息
select * from t_employee where job = 'CLERK' and sal > 800;
select * from t_employee where job = 'CLERK' && sal > 800;
- 查詢薪水在800和1500之間的員工信息
select * from t_employee where sal between 800 and 1500;
-- 或者
select * from t_employee where sal>=800 and sal<=1500;
** 注意這是一個前閉后閉的集合。**
- 查詢薪水不在800和1500之間的員工信息
select * from t_employee where sal not between 800 and 1500;
select * from t_employee where sal >1500 or sal<800;
- 查詢mgr為null的員工的信息/查詢comm獎金不為null的員工的信息
SELECT * FROM t_employee WHERE mgr IS NULL;
select * from t_employee where mgr is not null;
- 查詢工號不是7521、7782、7566和7788的員工信息
select * from t_employee where empno not in (7521,7782,7566,7788);
select * from t_employee where empno != 7521 && empno != 7782
&& empno != 7566 and empno != 7788;
- 查詢員工姓名中以A開頭的員工的信息
SELECT * FROM t_employee WHERE ename LIKE ("A%");
- 查詢員工姓名中第二個字母為A的員工的信息
select * from t_employee where ename like '_A%';
- 查詢員工姓名中含A的員工的信息
SELECT * FROM t_employee WHERE ename LIKE ("%A%");
查詢結果排序
- 查詢所有員工信息,并將員工按照工資的升序排列/降序排列
SELECT * FROM t_employee order by sal ASC;
select * from t_employee order by sal desc;
** 注意:在Mysql中,如果字段的值為null,則該值為最小值,因此在降序排序中將最后顯示,而在升序排序中則將最先顯示。**
- 多字段排序:查詢所有員工信息,并將員工按照工資的升序排列,如果工資相同,則安裝入職日期降序排序
SELECT * FROM t_employee order by sal asc,hiredate DESC;
限制數(shù)據(jù)查詢數(shù)量(分頁查詢)
- 分頁查詢全部員工信息,每頁查詢5條。
SELECT * FROM t_employee limit 0,5;-- 第一頁
select * from t_employee limit 5,5;-- 第二頁
select * from t_employee limit 10,5;-- 第三頁
** 如果客戶端發(fā)送來的數(shù)據(jù)是頁碼和每頁條數(shù),則公式爲 limit (頁碼-1)*每頁條數(shù),每頁條數(shù) **
- 查詢獎金爲null的前兩條記錄。
SELECT * FROM t_employee where comm is null limit 0,2;
統(tǒng)計函數(shù)
- 查詢公司領取獎金的人數(shù)。
select count(comm) from t_employee where comm != 0;
- 查詢員工領取獎金的平均值。
select avg(comm) from t_employee where comm != 0;
AVG 函數(shù)返回數(shù)值列的平均值。NULL 值不包括在計算中。
- 查詢所有員工工資的總和
select sum(sal) from t_employee;
- 查詢員工中最高工資和最低工資
select max(sal),min(sal) from t_employee;
分組數(shù)據(jù)查詢
- 查詢每個部門員工的工資總和
select deptno, sum(sal) from t_employee group by deptno;
- 查詢每個部門員工的人數(shù)、工資總和、平均工資、最高工資和最低工資
select deptno,count(ename),sum(sal),avg(sal),max(sal),min(sal)
from t_employee
group by deptno;
- 按照部門編號和入職日期分組,統(tǒng)計每組的工資總和、平均工資
select deptno,hiredate,sum(sal),avg(sal)
from t_employee
group by deptno,hiredate;
- 按照部門編號分組,查詢每組工資總和大於2000的部門的人數(shù)、總工資、平均工資。
select deptno,count(*),sum(sal),avg(sal)
from t_employee
group by deptno
having sum(sal) > 2000;
** where條件和having條件的區(qū)別 **
- having不能單獨使用一般和組函數(shù)一起使用,用在group by后面來完成分組的數(shù)據(jù)的篩選;where和組函數(shù)一起使用時,會在group by之前進行篩選結果,優(yōu)先級高于group by;
- where作用在硬盤上的數(shù)據(jù),having作用在內存中的數(shù)據(jù),所以where效率高,在兩者都能使用的情況下,優(yōu)先選擇where;
- where后面不能跟別名 having可以。
更新操作
- 設置SMITH員工的獎金為300
update t_employee set comm = 300 where ename = `SMITH`;
刪除操作
- 刪除姓名SMITH的記錄
delete from t_employee where ename = SMITH;
- 刪除全部數(shù)據(jù)
delete from t_employee ;
或
truncate student;
- 兩者區(qū)別:
- delete 將刪除的條數(shù)返回,truncate則返回0;
- delete 較慢,而truncate則速度快;
- delete 不會改變自增值,而truncate則會講自增值置為1從頭開始;
- drop table 表名;整個表都刪除
以上就是常用單表查詢的一些命令收集了,接下來再學習多表聯(lián)合查詢。