mysql 查询语句_SQL语言mysql基础查询语句
生活随笔
收集整理的這篇文章主要介紹了
mysql 查询语句_SQL语言mysql基础查询语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
單表查詢、條件查詢、查詢并排序、限制結果查詢、查詢并排名、分組聚合查詢、······
-- DQL操作,數據基本查詢語言使用----------------------------------------------------------------------------------------------- -- 創建數據表-- 注釋:員工編號,員工姓名,領導姓名,領導編號,入職時間,工資,獎金,部門編號CREATE TABLE `employee` ( `empid` int(11) NOT NULL, `ename` varchar(30) DEFAULT NULL, `job` varchar(30) DEFAULT NULL, `leaderid` int(11) DEFAULT NULL, `hiredate` datetime DEFAULT NULL, `wage` decimal(10,2) DEFAULT NULL, `prize` decimal(10,2) DEFAULT NULL, `deptid` int(11) DEFAULT NULL, PRIMARY KEY (`empid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 添加數據INSERT INTO `employee` VALUES ('2069', 'JALEN', 'CLERK', '7902', '2009-12-17 00:00:00', '18000.00', null, '20'),('3099', 'WANRE', 'SALESMAN', '7698', '2010-02-20 00:00:00', '18000.00', '300.00', '30'),('3021', 'FIKEN', 'SALESMAN', '7698', '2010-02-22 00:00:00', '16500.00', '500.00', '30'),('3066', 'JONES', 'MANAGER', '7839', '2011-04-02 00:00:00', '16000.00', null, '20'),('3054', 'RANKEE', 'SALESMAN', '7698', '2012-09-28 00:00:00', '16500.00', '1400.00', '30'),('3098', 'BLAKE', 'MANAGER', '7839', '2013-05-01 00:00:00', '16000.00', null, '30'),('1082', 'CALAN', 'MANAGER', '7839', '2014-06-09 00:00:00', '16000.00', null, '10'),('2088', 'SCOTT', 'ANALYST', '7566', '2015-04-19 00:00:00', '16000.00', null, '20'),('3039', 'DIVE', 'PRESIDENT', null, '2016-11-17 00:00:00', '15000.00', null, '10'),('3044', 'TURNER', 'SALESMAN', '7698', '2016-09-08 00:00:00', '15000.00', '0.00', '30'),('2076', 'JULI', 'CLERK', '7788', '2017-05-23 00:00:00', '11000.00', null, '20'),('3000', 'JAMES', 'CLERK', '7698', '2017-12-03 00:00:00', '9500.00', null, '30'),('2002', 'FAXI', 'ANALYST', '7566', '2017-12-03 00:00:00', '9000.00', null, '20'),('1034', 'MOKA', 'CLERK', '7782', '2018-01-23 00:00:00', '8800.00', null, '10'); -- 查詢語句語法: select cols #查詢并展示的數據(字段,表達式等) from tablename #查詢的數據來源(表,結果集,視圖等) where condition #條件語句 group by #分組 having #分組之后的條件判斷 order by #排序(asc升序 desc降序) limit #限制結果查詢(僅限于mysql) -- 1、查詢所有數據select * from employee; -- 2、查詢部分字段select ename,job from employee; -- 3、單一條件查詢select * from employee where ename = "FAXI";select * from employee where wage < 10000; -- 4、組合條件查詢select * from employee where wage >= 6000 and wage <= 10000;select * from employee where wage <10000 or hiredate > '2016-01-01 00:00:00'; -- 5、范圍查詢 between ... and ... | andselect * from employee where hiredate between '2015-01-01 00:00:00' and '2019-01-01 00:00:00';select * from employee where hiredate >= '2015-01-01 00:00:00' and hiredate <= '2019-01-01 00:00:00'; -- 6、集合查詢 in | orselect * from employee where deptid in (10,20);select * from employee where deptid = 10 or deptid = 20; -- 7、別名 [as]可以省略select e.ename,e.job,e.wage from employee as e;select e.ename,e.job,e.wage from employee e;select e.ename as '姓名',e.job as '崗位',e.wage '薪資' from employee as e; -- 8、去重distinctselect distinct e.deptid from employee as e;select distinct deptid from employee; -- 9 、模糊查找select * from employee where ename like "B%"; #以B開頭的,%表示通配符select * from employee where ename like "%A%"; #包含A的select * from employee where ename like "A%"; #以A結尾的select * from employee where ename like "_A%"; #第2個字母是A的,_表示占位字符select * from employee where ename like "__A%"; #第3個字母是A的,_表示占位字符 -- 10、排序order by ... desc | [asc]select * from employee order by deptid;select * from employee order by deptid asc;select * from employee order by deptid desc;select * from employee order by deptid desc ,hiredate asc ;select * from employee order by hiredate,wage; -- 11、限制結果查詢(limit:mysql專用)select * from employee limit 5; #查看前5行select * from employee limit 0,5; #查看前5行select * from employee limit 2,5; #查看從第3行開始往后數5行,即(2,2+5] -- 12、查詢并排名select @a,@b; #輸出申明的a變量和b變量,默認值都為nullselect @a := 1,@b; #select命令賦值用 := 給申明的a變量賦值,注意 := 中間不能有空格set @b = 2; #set命令給申明的變量賦值用 = select @a,@b; #此時輸出的結果應該為@a = 1 ,@b = 2, 兩個變量都已經被賦值select @a := "a" ,@b := 12.35; #聲明的變量可以賦予任意值(整數,小數,字符串,...) #案例 set @rank = 0; #申明一個rank變量,并賦予初始值0 -- 注釋:查詢表emp,并按照sal字段降序,同時輸出@rank := @rank + 1字段(表示申明的變量rank每一次都等于上一次的結果加1) select *, @rank := @rank + 1 from emp order by sal desc; -- 13、分組查詢 # 分組函數 # group by 分組字段 (先分組再聚合,每組一個結果) # having子句: 和where類似,分組之后過濾 # 分組之后,select能夠出現聚合函數,分組的字段 select deptno,avg(sal) avg from emp group by deptno; # 查詢平均工資>2000的部門的編號和平均工資 # 1.每個部門的平均工資 # 2.平均工資中再篩選>2000的 select deptno,avg(sal) avg from emp group by deptno having avg > 2000;總結
以上是生活随笔為你收集整理的mysql 查询语句_SQL语言mysql基础查询语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言数据结构、十字链表的分析及实现
- 下一篇: 深入浅出MYSQL查询索引失效