mysql語句學習

mysql -uroot -p
mysql -uroot -p密碼

exit/quit/ctrl+C

show Databases;
select version();
select now();
0.註釋
--


1.建立數據庫
create database python01;
create database python02 charset=utf8;


2.刪除數據庫
#刪除數據庫
drop database python01;

撇`xxx-database`

3.使用數據庫
use 數據庫
show databases;

4.查看當前數據庫
show database();

5.查看當前數據庫中的表
show tables;

6.建立數據庫表
create table xxx(id int, name varchar(30));
create table students(
                id int unsigned primary key not null auto_increment, 
                name varchar(30),
                age tinyint unsigned default 0,
                high decimal(5,2),
                gender enum("男","女","保密") default "保密",
                cls_id int unsigned
                );

7.顯示錶
desc xxx;

8.插入
insert into students values(0, "王俊傑",18,"男", 0);
select * from students;
insert into students (name, gender) values ("大喬", 12),("小喬", 22);
insert into students values (xx),(xxx),(xxxx);

表修改
9.增長表字段
alter table 表名 add 字段名 字段類型 

10.刪除表字段
alter table 表名 drop 字段名

11.修改表字段

只修改字段類型
alter table 表名 modify 字段名 新字段類型
alter table students modify birthday date;
修改字段類型
alter table 表名 change 字段名 新字段名 新字段類型
alter table students change birthday birth date;

12.查詢表字段

13.刪除數據表
drop table 表名;
drop database 數據庫;
alter drop table 表字段名;

14.顯示建立表的方式
show create table students;
show create database ss;

15.修改表數值
update students set gender=1,age=12 where id=1;

16.查表數據
select * from students;
select * from students where name="xx";
select name,gender from student;
select name as 姓名,gender as 姓名 students;

17.刪除數據
delete from student where xx; 



00.例子
建立classed表
有id、name

create table classes(
    id int unsigned not null auto_increment primary key,
    name varchar(30) not null
);

insert into classes (0, "mysql學習班");

查詢
--使用mysql登錄
mysql -uroot -p

--表示註釋

--建立一個數據庫
create database python_test charset=utf8;

--使用一個數據庫
use python_test;

--顯示使用的當前數據是哪一個
--函數
select database();

--建立一個數據表
--學生表
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('','','中性','保密') defaule '保密',
cls_id int unsigned default 0,
is_delete bit default 0
); 

--班級表
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(30) not null
);

--查看當前表
show tables;

--查看如何建立表字段
show create tables students;

--插入字段數據
insert into students values
(0, '小明',18,180.00,2,1,0)
(0,'小月月',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'劉德華',59,175.00,1,2,1),
(0,'黃蓉',38,160.00,2,1,0),
(0,'鳳姐',28,150.00,4,2,1),
(0,'王祖賢',18,172.00,2,1,1),
(0,'周杰倫',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'劉亦菲',25,166.00,2,2,0),
(0,'金星',33,162.00,3,3,1),
(0,'靜香',12,180.00,2,4,0),
(0,'郭靖',12,170.00,1,4,0),
(0,'周杰',34,176.00,2,5,0);

insert into classes values
(0,"python_01期"),
(0,"python_02期");

--查詢全部字段
select * from student;
select* from classes;

--查詢只定字段
select id,name from student;

--別名
select name as 姓名, age as 年齡 from students;
select students.name, students.age from students;
select s.name,s.age from students as s;

--消除重複行
select distinct gender from students;

--條件查詢
select name as 名字, age as 年齡 from students where age=18 or age=34;

select name,age from students age in(12, 15, 24); 
select name,age from students age not in(12, 15, 24);

--連續的
select name,age from students age between 18 and 24;
--not between 是一個用法,不能分開not (between)
select name,age from students age not between 18 and 24;

--判斷空
--in null
--in not null
select * from students where height is null;
select * from students where height is not null;

--排序
--默認按照id排序
--查找年齡在18-24之間的男性
select * from students where (age between 18 and 34) and gender=1;
--按照age 從小到大排序,默認asc升序,能夠不寫
select * from students where (age between 18 and 34) and gender=1 order by age;
select * from students where (age between 18 and 34) and gender=1 order by age asc;
--按照age從大到小排序,desc 降序
select * from students where (age between 18 and 34) and gender=1 order by age desc;

--查詢年齡在18-24之間的女性,身高從高到矮排序
select name as 名字,age as 年齡 from students where (age between 18 and 24) and gender=2 order by heigth desc; 
--相同狀況下,按照id從大到小排
select name as 名字,age as 年齡 from students where (age between 18 and 24) and gender=2 order by height desc,id desc; 
--按照年齡從小到大
select name as 名字,age as 年齡 from students where (age between 18 and 24) and gender=2 order by height, age asc,id desc;
--按照年齡從小到大,身高從高到矮的排序
select * from students order by age asc,height desc;


--聚合函數
--總數
--查詢男性有多少人,女性有多少人
select * from students where gender=1;
select count(*) as 男性總數 from students where gnder=1;

--最大值
select max(age) from students;
select max(height) from students where gender=2;

--最小值
--min
select min(age) from students where gender=1;

--求和
--sum
select sum(age) from students;


--平均值
-avg
select avg(age) from students;
select sum(age)/count(*)from students;

--四捨五入
--round
--存小數的時候要*100
--由於小數存儲的時候是約等於,會有偏差
--取得時候,再除以100
--好比須要存入3.14,存的時候是314
select round(sum(age)/count(*),2) from students;

select round(avg(height),2) from students where gender=1;


--分組,把數據先分組,而後從分組裏面的數據取數據
--group by
--按性別分組,實際當中多是根據部門等分組
select gender from students group by gender;
select gender,count(*) from students group by gender;
select gender,avg(age) from students group by gender;

--group_concat()
--分組中顯示須要的字段
select gender,group_concat(name) from students group by gender;

--group by,查詢結果過濾
--gender=1,只能寫在group前面
select gender,count(*) from students where gender=1 group by gender; 
select gender,count(*),group_concat(name,age,id) from students where gender=1 group by gender; 
select gender,count(*),group_concat(name,"_",age,"",id) from students where gender=1 group by gender; 

--having
--分組結果過濾
--條件通常都寫在後面
--查詢平均年齡超過30歲的性別,以及姓名 having avg(age)>30;
select gender,group_concat(name) from students group by gender having avg(age)>30;
--where 和 having 的區別
--where是放在group前面,having是放在後面
--where是對原始表的過濾,having是對查詢結果臨時表的過濾

--查詢每種性別中的人數多於2個的信息
--having count(*)>2
select gender,group_concat(name) from students group gender having count(*)>2;


--分頁
--限制查詢結果的個數
--limit start,count 
--limit 要寫在句子最後面
--where group (having) limit
--start起始位置,count總個數
--最多顯示兩個
select * from students gender=1 limit=2;

--查詢前5個,第一組5個數據
select * from students limit 0,5;
select * from students limit 5,5;

--查詢每頁顯示的個數,根據每頁顯示
--limit (N-1)*每一個的個數,每頁的個數
--顯示第6頁的數據,每組2個,(6-1)*2=10;
--根據10能夠猜到第幾頁,(10/2)+1=6,說明是第6頁
select * from students order by age asc limit 10,2;

--查詢身高從高到矮,每頁只顯示兩個
select * from student where gender=2 order by height desc limit 0,2;


--連接查詢
--多表查詢
--inner join,多表數據鏈接,取表的交集
--select ... from tableA inner join tableB;

--將學生中的id和classes中id相等的信息顯示出來
select * from students inner join classer on students.cls_id=classed.id;

--按照要求顯示名字和班級
select students.*,class.name from students inner join classes on students.cls_id=classes.id;
select students.name,classes.name from students inner join classes on students.cls_id=classes.id;
select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;

--查詢 有可以對應班級的學生以及班級信息,顯示學生的全部信息,只顯示班級名稱
select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id;

--在以上的查詢中,將班級姓名顯示在第1列
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id;

--查詢 有可以對對應班級的學生以及班級信息,按照班級進行排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;

--當時同一個班級的時候,按照學生的id進行從小到大排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;


--外連接
--left join,之後左表爲基準,找不到顯示爲null
--查詢每位學生對應班級信息
select * from students as s left join classes as c on s.cls_id=c.id;

--查詢沒有對應班級信息的學生
--複習,where是對原始的表進行過濾、having是對查詢後的哦、臨時表進行過濾
select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;
select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;

--right join
--通常用left join代替
--將數據表名字互換位置,用left join完成


--自關聯
--同一張表中的字段關聯另外一個字段
source areas.sql

--省級聯動
--查詢一張表中某個市、或者省的下級單位
--查詢山東省中的下級城市
select * from areas as a  inner join areas as b on a.pid=c.pid having a.name="山東省";

--子查詢
--select語句中還有其餘select查詢,先執行子查詢select
--標量子查詢

--查詢出高於平均值身高的信息
select * from students height = (select max(height) from students);
select * from areas where pid = (select aid from areas where name="河北省");

--查詢最高的男生信息

--列級子查詢
--查詢學生的班級號可以對應的學生信息
 
 

 mysql新版修改密碼html

use mysql;
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密碼’; FLUSH PRIVILEGES;
8.0的詳細設置參考 :
http://www.javashuo.com/article/p-vjohmhwu-dc.html
相關文章
相關標籤/搜索