1. 程式人生 > >mysql 資料操作 增刪改查

mysql 資料操作 增刪改查

1、增
a、全列插入:
insert into 表名 values(...)
通常主鍵列是自動增長的,但是在全列插入時需要佔位,通常使用0,插入資料後以
實際使用為準
例如:insert into student values(0,"tom",19,1,0);

b、預設插入
insert into 表名(列名,列名,...) values(值1,值2,...)
例如:insert into student(name,age) values("lili",17);

c、同時插入多條資料
insert into 表名 values(),(),...
例如:insert into student values(0,"han",15,1,0),(0,"tyu",19,1,0),(0,"tfgm",29,1,0);

2、刪
delete from 表名 where 條件
例如 delete from student where id = 3;

3、改
update student set 列1=值1,列2=值2,列3=值3,...where 條件;
update student set age=16 where id =5;
沒有條件就是全部列都修改,慎用

4、查
a、基本語法
select *(列名稱) from 表名
    from關鍵字後面是表名,表名資料來源
    select後面是表中的列名,如果是*,表示在查詢結果中顯示錶的所有列
    在select後面的列名部分,可以使用as為列名起別名,這個列名顯示在結果中
    如果要查詢多個列,之間使用逗號分隔
    
b、消除重複行
在列名前面使用distinct可以消除重複行
    select distinct gender from student
    
c、條件查詢
1)語法
select *from 表名 where 條件

2)比較運算子
等於      =
大於      >
小於      <
大於等於  >=
小於等於  <=
不等於    !=
如:select * from student where id >4;

3)邏輯運算子
and     並且
or      或者
not     非
select * from student where (id>2 and id<5);

4)模糊查詢
select * from 表名 where 列名 like 表示式    
_       -->一個任意字元
%       -->任意多個任意字元

例如 select * from student  where name like "h%";

5)範圍查詢
in                  表示在一個非連續的範圍內
    select * from student where id in (1,2,5);
between...and...    表示在一連續的範圍內
    select * from student where id between 1 and 5;
    
6)空判斷
select * from 表名 where 列名 is null;
注意null與""是不同的
判斷空:is null
判斷非空:is not null
    select * from student where address is null;

d、聚合
為了快速的統計資料,提供了5個聚合函式
1)count(*)  計算總行數,()中可以寫*及列名
    select count(*) from student;
2)max(列)    求此列的最大值
    select max(id) from student where gender=0; 
3)min(列)     求此列的最小值
4)sum(列)    求此列的和
    select sum(age) from student;
5)avg(列)    求此列的平均值

e、分組
按照欄位分組,表示此欄位相同的資料會被放到一個集合中
分組後,只能查詢出相同的資料列,對於有差異的資料列無法顯示在結果集中
可以對分組後的資料進行統計,做聚合運算

語法 select 列1 列2 聚合... from 表名 group by 列1,列2,列3,...
    查詢男女生總數
    select gender,count(*) from student group by gender;
    select name,gender,count(*) from student group by gender,age;
    
    分組後的資料篩選:select 列1,列2,聚合... from 表名 group by 
    列1,列2,... having 列1,... 聚合...
    例如:select gender,count(*) from student group by gender having gender=0;
    
where與having的區別:
    where是對原始資料查詢,having是的分組的結果進行查詢
    
f、排序
語法 select * from 表名 order by 列1 asc|desc, 列2 asc|desc,...
說明:
    將資料按照列1進行排序,如果某些列1的值相同,則按照列2排序...
    預設按照從小到大的順序排序
    asc升序
    desc降序
        按照年齡排序
        select * from student  where gender=0 order by age desc;
        
g、分頁
語法:select * from 表名 limit start/起始項,count/條數
    如: select * from student limit 1,4;
        select * from student where gender=0 limit 0,1;
        
5.關聯
    建表語句
    create table class(id int auto_increment primary key,name varchar(20) not null,stuNum int not null)
    create table students(id int auto_increment primary key,name varchar(20) not null,
    gerder bit default 1 not null,classid int not null,foreign key (classid) references class(id));
    
    插入資料
    insert into class values(0,"english",23),(0,"math",53),(0,"chainese",34);
    insert into students values(0,"toma",1,1),(0,"lilei",0,2);
    
    關聯查詢:
    select students.name,class.name from class inner join students on class.id=students.classid;
    
    表A inner join 表B
        表A與表B匹配的行會出現在查詢結果中
    表A left join 表B
        表A與表B匹配的行會出現在結果集中,外加表A中獨有的資料,未對應的資料使用null填充
    表A right join 表B
        表A與表B匹配的行會出現在結果集中,外加表B中獨有的資料,未對應的資料使用null填充