瞭解:Mysql 帳號相關mysql
建立帳號:正則表達式
權限:user(全部庫的權限)-->db(某個庫的權限)-->table_priv(某張表的權限)sql
-->columns_oriv(某個字段的權限)ide
建立帳號:create user ‘tom’(帳號)@’客戶端ip(%表明全部ip均可)’ identified by ‘123’(密碼)函數
登陸:mysql -utom -p’123’ -h 服務端ip -P3306post
建立帳號並受權:只有root帳號能爲其它帳號受權regexp
grant all(不包括grant權限) on *.* to ‘tom’@’%’ identified by ‘123’排序
其中:ip
*.*表明user權限資源
db.*表明db權限
db.t1表明table_priv權限
grant select(id) on db.t1 表明columns_oriv權限
# 修改完權限必定要
flush privileges;
單表查詢
完整語法
select distinct 字段1,字段2,字段3,... from 庫名.表名
where 約束條件
group by 分組依據
having 過濾條件
order by 排序的字段
limit 限制顯示的條數
關鍵字執行優先級:
from where group by having distinct order by limit
字符拼接:
select concat('名字: ',name) as new_name,concat("年齡: ",age) as new_age from emp;
select concat(name,":",age,":",sex) from emp;
select concat_ws(":",name,age,sex) as info from emp;
瞭解:流程控制
SELECT (
CASE
WHEN NAME = 'egon' THEN
NAME
WHEN NAME = 'alex' THEN
CONCAT(name,'_BIGSB')
ELSE
concat(NAME, 'SB')
END
) as new_name
FROM
emp;
where關鍵字
_表明任意單個字符
%表明任意無窮個字符
select * from emp where name like "__";
select * from emp where name like "jin%";
select * from emp where id not in (6,9,12);
group by分組
什麼是分組:按照全部記錄相同的部分進行歸類,必定區分度低的字段
爲什麼要分組:當咱們要以組爲單位進行統計時就必須分組,分組的目的是爲了以組爲單位進行統計的,再去考慮單條記錄毫無心義
set global sql_mode="strict_trans_tables,only_full_group_by"; 設置嚴格模式
注意:分組以後,只能查到分組的字段以及組內多條記錄聚合的成果
select * from emp group by post;
聚合函數
max
min
avg
sum
count
having 過濾條件
where是在分組以前的過濾,即在分組以前作了一次總體性的篩選
having是在分組以後的過濾,即在分組以後專門針對聚合的結果進行進一步的篩選
order by排序
select * from emp order by age asc; # 默認asc升序-》從小到大
select * from emp order by age desc;# desc降序-》從大到小
select * from emp order by age asc,salary desc; # 先按照age升序排列,若是age相同則按照salary降序排
limit 限制顯示的條件
select * from emp limit 3;
分頁顯示
select * from emp limit 0,5; # 從0開始日後取5條
select * from emp limit 5,5; #從5開始日後取5條
正則表達式
select * from emp where name regexp "^jin.*(g|n)$";
多表查詢
一、笛卡兒積
select * from emp,dep;
select * from emp,dep where emp.dep_id = dep.id;
select * from emp,dep where emp.dep_id = dep.id and dep.name = "技術";
二、內鏈接:只取兩張表有對應關係的記錄
select * from emp inner join dep on emp.dep_id = dep.id;
select * from emp inner join dep on emp.dep_id = dep.id
where dep.name = "技術";
三、左鏈接: 在內鏈接的基礎上保留左表沒有對應關係的記錄
select * from emp left join dep on emp.dep_id = dep.id;
四、右鏈接: 在內鏈接的基礎上保留右表沒有對應關係的記錄
select * from emp right join dep on emp.dep_id = dep.id;
五、全鏈接:在內鏈接的基礎上保留左、右面表沒有對應關係的的記錄
select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;
子查詢:就是將一個查詢語句的結果用括號括起來看成另一個查詢語句的條件去用
select * from emp where dep_id in (select id from dep where name = "技術" or name = "人力資源");