<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
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 | +-------+-------+-------+
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)
在查詢的過程中,我們還可以選擇給列起一個別名:
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)
在所有的資料庫中,字串統一使用單引號,這是一個標準
在查詢的時候,我們也可以直接進行列運算操作:
比如,我們想計算員工的年薪:
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)
範例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資料庫 查詢內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45