mysql 查看表格scott_mysql查询学习第一天,针对scott
終于把牛老師的那個項目退了,以后有時間去學習自己喜歡的東西了。
我喜歡寫點東西,也喜歡胡扯,哈哈。
算了,開始正題。scott表是Oracle官網的Demo現在學習一個新知識,學習他的Demo最好不過了。
select * from emp;
select empno ,ename from emp;
select ename,sal*12 as "年薪" from emp;
-- as 可以省略,
select ename as "姓名",sal*12 as "年薪"? ,sal as"月薪" ,job from emp;
select 5 from emp;
/*
distinct[不允許重復的]
*/
select deptno from emp;-- 14行記錄
select? distinct deptno from emp;
select distinct comm from emp;--空也也是個唯一的數值,也可以過濾掉。只剩下唯一的一個。
select distinct comm,deptno from emp;
-- 這行是把他們這一組的組合進行過濾了。
select comm, distinct deptno from emp;
--這一組有錯,是因為前面的com是14行,
--然而后面的過濾了之后是三行。明顯出現錯誤了。
/*
between
的用法,在某個范圍之間。
*/
--查找工資在1500和3000之間所有員工之間的信息
select * from emp
where sal>=1500 and sal<=3000;
select * from emp
where sal between 1500 and 3000;
--和上面的相同查詢到的記錄都是7個。
--查詢相反的區間。
select * from emp
where sal <1500 or sal >3000
order by sal desc; --這里面的or和and的區別,否的否是并且
select * from emp
where sal not between 1500 and 3000
order by sal asc;
--h和上面的寫法是一樣的,但是這里面需要注意or和and的用法就KO了
/*
in的用法[屬于若干個孤立的數值。]
*/
select * from emp
where sal in (1500,3000);
--這樣查詢只有1500和3000的工資的記錄被查找了出來了,
--等價于下面的。
select * from emp
where sal =1500 or sal =3000;
--有in就有not in
select * from emp
where sal not in(1500,3000);
--下面和上面的是等價的。
select * from emp
where sal !=1500 and sal !=3000;
select * from emp
where sal <>1500 and sal <>3000;
--<>這個也是不等于。
--下面是top的用法。
select * from emp;
select top(2) * from emp;--把前面查詢到的兩行進行輸出
select top(30) percent * from emp;--把30%輸出,14行記錄輸出了5行,所以不夠的話是往上面走的。
select top(10) * from emp
where sal between 1500and 3000
order by sal desc
select * from emp
where comm !=null;
select * from emp
where comm <>null;
-- 總結NULL不能參與!= <>運算
select * from emp
where comm =null;
-- 也不能參與=運算。
select * from emp
where comm is null;
select * from emp
where comm is not null;
--總結,null可以參與的運算有 is 還有is not
--任何類型的數據都可以為NULL;
create table t1 (name varchar(20) ,cnt int ,riqi datetime);
insert into t1 values(null,null,null);
select * from t1;
--所有的數據類都可以為NULL
--輸出每個員工的姓名,年薪,(年薪包括獎金)
select ename as "姓名", sal*12+comm as "年薪" from emp;--錯誤哦哦。
-- null不能參與一個具體值進行運算,最終結果就是沒有結果。
-- 本程序證明了NULL不能參與任何算術運算,否則還是沒有結果的NULL
-- 百度了一下
select ename as"姓名",sal *12+isnull(comm,0) as"年薪" from emp;
-- 現在就是KO了,? isNull函數可以解決這些問題、
--isNull 如果comm是NILL就返回0 否則就返回comm
/*
下面是order by[以某個字段進行排序]
*/
select * from emp order by sal;
-- 這個默認是升序
select * from emp order by sal desc;
--這樣子寫是降序
select * from emp order by deptno ,sal ;
--這樣子是先按deptno 排序,然后發現deptno相同的時候在按照sal排序。
-- 模糊查詢。
select * from emp where ename like '%a%';--把含有A的字母的名字給查詢出來
select * from emp where ename like 'a%'-- 首字母是A的咱們就輸出
select * from emp where ename like '%a'-- 只要是末尾的字母是A的就打印
select * from emp where ename like '_a%' -- 只要第二個字母是A就打印
select * from emp where ename like '_[a-f]%'--只要是第二個字符是A-F的人打印
select * from emp where ename like '_[^a-f]%' -- 把上面的取反得到的
-- 加入我這里插入了這么一句,那么該如何查詢到%的姓名呢,百度了一下得到了下面的結果
insert into emp values (0001,'c%d',NULL,0001,null,0,null,20);
select * from emp where ename like '%\%%' escape '\'
--看,這樣就查詢出來了答案了,關鍵是把\當成了特殊字符對待,后面的是一個普通字符。
-- 通配符
/*
和regex正則表達式一模一樣? 學過正則這些就不用再看了
-- attintion 唯一需要注意的就是把like后面的加上單引號,代表區分關鍵字,
說到了單引號,那么雙引號在我們這里的使用代表的是一個對象的名稱或者是別名。
% 任意0個或者多個字符
_ 下劃線,代表的是任意的單個字符
[a-f] 代表的是a-f的任意的單個字符
[a,z] 代表的是a或者z
[^a-f]代表的是取反,不是a-z的任意字符
*/
/*
模糊查詢完成。大概也就這么多東西了。2016年2月11日16:37:21
*/
/*
聚合函數
單行函數,每一行返回一個數值。
多行函數,多行返回一個數值.
聚合函數是多行的函數。
*/
select lower(ename)? from emp; --大寫換成lower寫。單行函數,每一行都返回一個數值
select max(sal) from emp; --max是多行函數,因為多行返回的是一個數值。
/*
聚合函數的分類
max()
min()
avg()
count()
*/
select count(*) from emp --返回emp表里面所有記錄的個數
select count(deptno) from emp;-- 有重復的but還是15這說明deptno重復的記錄還是被當成有效的了
select count(distinct deptno) from emp; --掛上distinct 這樣重復的記錄就被過濾了。
select count(comm) from emp; -- null不參與count的計算。返回值是4個,然而不是14個
--------------------------------------------------------
select max(sal) as "最高工資" ,min(sal) as "最低工資" ,count(*) "員工人數" from emp; -- 正確
select max(sal) ,lower(ename) from emp;--返回的行數不一樣,肯定是ERROR
--
總結
以上是生活随笔為你收集整理的mysql 查看表格scott_mysql查询学习第一天,针对scott的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux ubuntu php,lin
- 下一篇: linux cmake编译源码,linu