MySQL數據庫學習之查詢操作詳解
1.示例表內容
dept表:
+--------+------------+----------+ | DEPTNO | DNAME | LOC | +--------+------------+----------+ | 10 | ACCOUNTING | NEW YORK | | 20 | RESEARCH | DALLAS | | 30 | SALES | CHICAGO | | 40 | OPERATIONS | BOSTON | +--------+------------+----------+
emp表:
+-------+--------+-----------+------+------------+---------+---------+--------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +-------+--------+-----------+------+------------+---------+---------+--------+ | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 | | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 | | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 | | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 | +-------+--------+-----------+------+------------+---------+---------+--------+
salgrade表:
+-------+-------+-------+ | GRADE | LOSAL | HISAL | +-------+-------+-------+ | 1 | 700 | 1200 | | 2 | 1201 | 1400 | | 3 | 1401 | 2000 | | 4 | 2001 | 3000 | | 5 | 3001 | 9999 | +-------+-------+-------+
2.簡單查詢
MySQL 數據庫使用SQL SELECT語句來查詢數據。
例如我們要查詢一個表的全部信息,可以這樣做:
當然,這種方式進行查詢的效率較低,我們更推薦您使用多列查詢的方式:
mysql> select * from dept; +--------+------------+----------+ | DEPTNO | DNAME | LOC | +--------+------------+----------+ | 10 | ACCOUNTING | NEW YORK | | 20 | RESEARCH | DALLAS | | 30 | SALES | CHICAGO | | 40 | OPERATIONS | BOSTON | +--------+------------+----------+ 4 rows in set (0.00 sec)
那麼,如果想查詢單個列的信息呢?
可以指定列名進行查詢:
mysql> select DNAME from dept; +------------+ | DNAME | +------------+ | ACCOUNTING | | RESEARCH | | SALES | | OPERATIONS | +------------+ 4 rows in set (0.00 sec)
查詢多個列的信息,可以這樣寫:
mysql> select DNAME , LOC from dept; +------------+----------+ | DNAME | LOC | +------------+----------+ | ACCOUNTING | NEW YORK | | RESEARCH | DALLAS | | SALES | CHICAGO | | OPERATIONS | BOSTON | +------------+----------+ 4 rows in set (0.00 sec)
3.給列起別名
在查詢的過程中,我們還可以選擇給列起一個別名:
mysql> select DNAME as NAME from dept; +------------+ | NAME | +------------+ | ACCOUNTING | | RESEARCH | | SALES | | OPERATIONS | +------------+ 4 rows in set (0.00 sec)
也可以不加 as 關鍵字:
mysql> select DNAME NAME from dept; +------------+ | NAME | +------------+ | ACCOUNTING | | RESEARCH | | SALES | | OPERATIONS | +------------+ 4 rows in set (0.00 sec)
在所有的數據庫中,字符串統一使用單引號,這是一個標準
4.列運算
在查詢的時候,我們也可以直接進行列運算操作:
比如,我們想計算員工的年薪:
mysql> select ename,sal*12 from emp; +--------+----------+ | ename | sal*12 | +--------+----------+ | SMITH | 9600.00 | | ALLEN | 19200.00 | | WARD | 15000.00 | | JONES | 35700.00 | | MARTIN | 15000.00 | | BLAKE | 34200.00 | | CLARK | 29400.00 | | SCOTT | 36000.00 | | KING | 60000.00 | | TURNER | 18000.00 | | ADAMS | 13200.00 | | JAMES | 11400.00 | | FORD | 36000.00 | | MILLER | 15600.00 | +--------+----------+ 14 rows in set (0.00 sec)
5.條件查詢
示例1:查詢工資大於等於3000的信息:
mysql> select empno,ename from emp where sal >= 3000; +-------+-------+ | empno | ename | +-------+-------+ | 7788 | SCOTT | | 7839 | KING | | 7902 | FORD | +-------+-------+ 3 rows in set (0.00 sec)
示例2:查詢工資在2000到3000(包含2000和3000)的信息:
mysql> select empno,ename from emp where sal between 2000 and 3000; +-------+-------+ | empno | ename | +-------+-------+ | 7566 | JONES | | 7698 | BLAKE | | 7782 | CLARK | | 7788 | SCOTT | | 7902 | FORD | +-------+-------+ 5 rows in set (0.00 sec)
示例3:查詢員工補助為空的(不為空為is not null):
mysql> select empno,ename from emp where comm is null; +-------+--------+ | empno | ename | +-------+--------+ | 7369 | SMITH | | 7566 | JONES | | 7698 | BLAKE | | 7782 | CLARK | | 7788 | SCOTT | | 7839 | KING | | 7876 | ADAMS | | 7900 | JAMES | | 7902 | FORD | | 7934 | MILLER | +-------+--------+ 10 rows in set (0.00 sec)
Mysql比較NULL值不能使用=號
示例4:查詢崗位為MANAGER並且工資大於等於2500的信息:
mysql> select * from emp where JOB = "MANAGER" and SAL >= 2500; +-------+-------+---------+------+------------+---------+------+--------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +-------+-------+---------+------+------------+---------+------+--------+ | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | +-------+-------+---------+------+------------+---------+------+--------+ 2 rows in set (0.00 sec)
示例5:查詢崗位為MANAGER或者SALESMAN的員工:(使用關鍵字in)(不在某幾個值之間使用not in)
mysql> select empno,ename,job from emp where job in ('MANAGER','SALESMAN'); +-------+--------+----------+ | empno | ename | job | +-------+--------+----------+ | 7499 | ALLEN | SALESMAN | | 7521 | WARD | SALESMAN | | 7566 | JONES | MANAGER | | 7654 | MARTIN | SALESMAN | | 7698 | BLAKE | MANAGER | | 7782 | CLARK | MANAGER | | 7844 | TURNER | SALESMAN | +-------+--------+----------+ 7 rows in set (0.00 sec)
示例6:模糊查詢,找出名字中含有字母o的:
mysql> select ename from emp where ename like '%o%'; +-------+ | ename | +-------+ | JONES | | SCOTT | | FORD | +-------+ 3 rows in set (0.00 sec)
示例7:模糊查詢,找出名字以T結尾的:
mysql> select ename from emp where ename like '%T'; +-------+ | ename | +-------+ | SCOTT | +-------+ 1 row in set (0.00 sec)
示例8:模糊查詢,找出名字以K開頭的:
mysql> select ename from emp where ename like 'K%'; +-------+ | ename | +-------+ | KING | +-------+ 1 row in set (0.00 sec)
示例9:模糊查詢,找出名字第二個字母是A的:
mysql> select ename from emp where ename like '_A%'; +--------+ | ename | +--------+ | WARD | | MARTIN | | JAMES | +--------+ 3 rows in set (0.00 sec)
示例10:模糊查詢,找出名字第三個字母是A的:
mysql> select ename from emp where ename like '__A%'; +-------+ | ename | +-------+ | BLAKE | | CLARK | | ADAMS | +-------+ 3 rows in set (0.00 sec)
到此這篇關於MySQL數據庫學習之查詢操作詳解的文章就介紹到這瞭,更多相關MySQL數據庫 查詢內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- MySQL數據庫學習之排序與單行處理函數詳解
- MySQL數據庫學習之去重與連接查詢詳解
- MySQL數據庫之union,limit和子查詢詳解
- MySQL中給定父行找到所有子行的解決方案
- Scott 數據 映射 MySQL代碼實現分享