單表查詢,多表查詢

單表查詢

單字段去重 “DISTINCT”
select distinct 字段 from 表名;

通過四則運算查詢
select 字段*n(或者字段相加) from 表名;

定義顯示格式
concat() 函數用於連接字符串(鏈接拼接的作用,並且可以添加想要的字符串)
select concat(字段, ' 的年薪是: ', 字段*14) as 別名 from 表名;

多條件查詢(and 和 or)
select 字段 from 表名 where 字段>5000 and 字段<6000;
select 字段 from 表名 where not 字段>5000; (取反)

關鍵字between and & not between(兩者之間) and
select 字段,字段 from 表名 where 字段 between 5000 and 15000;
select 名字,工資 from employee5 where 工資 not netween 5000 and 15000; (取反)

按時間段查詢

字段 BETWEEN '開始時間' AND '截止時間'

該字段本就是時間類型,如DATETIME,例:

SELECT tel,ip,logintime FROM log_logins,userinfo WHERE logintime BETWEEN '2019-12-25 00:00:00' AND '2019-12-27 14:01:00' AND log_logins.`account`=userinfo.`account`;

這是用的一個內鏈接的多表查詢,需要log_logins和userinfo兩張表內的手機號,IP和訪問時間。



關鍵字is null
select 字段,字段 from 表名 where 字段 is null; (只顯示空)
select 字段,字段 from 表名 where 字段 is not null;(只顯示非空)
NULL說明:
1、等價於沒有任何值、是未知數。
2、NULL與0、空字符串、空格都不同,NULL沒有分配存儲空間。
3、對空值做加、減、乘、除等運算操作,結果仍爲空。
4、比較時使用關鍵字用“is null”和“is not null”。
5、關鍵字IN集合查詢排序時比其他數據都小(索引默認是降序排列,小→大),所以NULL值總是排在最前。


關鍵字in集合查詢(等同多個or一起使用)
select 字段, 字段 from 表名 where 字段 in (4000,5000,6000,9000) ;
select 字段, 字段 from 表名 where 字段 not in (4000,5000,6000,9000) ; (取反)

排序查詢 (order by , desc, asc)
select 字段 from 表名 order by 字段 desc; (降序,從大到小)
select 字段 from 表名 order by 字段 asc; (升序,從小到大)
select * from biao where UID >1000 order by UID asc limit 10;
限制查詢的記錄數 limit :(限制,控制)
select * from 表名 order by 字段 desc limit 5;
(默認初始位置爲0,只顯示5行)
select * from 表名 order by 字段 desc limit 3,5;
(3行以後的5行顯示,即第4行開始只顯示5行)

集合函數
count() 計數(統計)
max() 最大
min() 最小
avg() 平均
sum() 總和
now() 當前時間

模糊查詢(通配符)
_ 任意單個字符 (不包括零個字符)
% 所有字符,注意與*的區別 (*用於前面,%用於條件後)
select * from 表名 where 字段 like 'al%';

正則查詢(REGEXP):regular expression 
SELECT * FROM employee5 WHERE name regexp '^ali';
SELECT * FROM employee5 WHERE name REGEXP 'lan$';
SELECT * FROM employee5 WHERE name REGEXP 'm{2}';

子查詢
select * from 表名 where 字段 = (字段 max(字段) from 表名);
(將等號後的內容等同條件後的字段使用)
例: select 名字,語文 from 表名 where 語文=(select max(語文) from 表名); (查詢語文最高分)

分組查詢
group by和group_concat()函數一起使用
select 字段1,group_concat(字段) from 表名 group by 字段1;
(GROUP BY :以什麼條件分組
GROUP_CONCAT():分組後的數據,顯示那個字段 )
例:select 辦公室,sum(工資)from 表名 group by 辦公室;
(查詢辦公室工資總和)
例2: select 辦公室,group_concat(名字) from 表名 group by 辦公室; (查詢辦公室有哪些員工)


 

多表查詢

內連接: 只連接匹配的行(只顯示符合匹配條件的行)
外連接
左連接:會顯示左邊表內所有的值,不論在右邊表內匹不匹配
右連接:會顯示右邊表內所有的值,不論在左邊表內匹不匹配

內連接語法:
select 字段1,字段2, ..... from 表1,表2,..... where 表1.字段=表2.字段; (條件後接兩張表相同的字段數據,如id,等號鏈接,不顯示空行)

外連接語法:
select 字段列表 from 表1 left|right join 表2 ON 表1.字段 = 表2.字段;
(先用誰,誰就是左,不顯示右邊空行)

複合條件連接查詢
select 字段,字段.. from 表名1,表名2 where 表1.字段=表2.字段 and age > 25||order by age asc;(查詢兩表內大於25或以年齡排序)

子查詢
1.帶IN關鍵字的子查詢
select * from 表1 where 字段1 in (select 字段1 from 表2);
(查詢表1,但條件後字段必須在表2中出現過)


2.帶比較運算符的子查詢
=、!=、>、>=、<、<=、<>
select 字段.. from 表1 where 字段1 in (select distinct 字段1 from 表2 where age>=25);

3. 帶EXISTS(存在)關鍵字的子查詢
EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄,而是返回一個真假值。
Ture或False,當返回Ture時,外層查詢語句將進行查詢;當返回值爲False時,外層查詢語句不進行查詢

department表中存在id=203,Ture
select * from 表1 where exists (select * from 表2 where id=203);

department表中存在id=300,False
select * from 表1 where exists (select * from 表2 where id=300);
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章