Mysql增刪改查基礎語法

SQL

簡單的增刪改查

不區分大小寫, 表名和字段名可不加引號

查詢語句

SELECT * FROM `table_name`;

– 註釋 CTRL+/ : 註釋 CTRL+/ :

取消註釋 /* */ : 多行註釋

創建表和字段

CREATE TABLE `table_name`(
    `name` VARCHAR(20),
    `age` int,
    `sex` VARCHAR(10)
);

創建表和字段(添加ID字段,加主鍵)

CREATE TABLE `table_name`(
    `id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(20),
    `age` INT,
    `height` DECIMAL(5,2)
);

在表中插入行

alter table 表名 add column 字段名 varchar(50) after/before 字段名;

刪除表

1. DROP TABLE `table_name`;  -- 如果表不存在語句會報錯
2. DROP TABLE IF EXISTS `table_name`;  -- 表不存在也不會報錯

添加一行數據

1. 所有字段都設置值  
INSERT INTO `table_name` VALUES (,,);

2. 單獨字段設置值
INSERT INTO table_name(col_name) VALUES ();  
INSERT INTO table_name(col_name, col_name) VALUES (,);

3. 插入多行
insert into table_name values (...), (...), (...)...

修改數據

update table_name set col_name=value, col_name=value, col_name=value where 條件;  

刪除數據

1. delete from table_name where 條件; -- 不加條件刪除所有數據, 保留表結構
2. truncate table table_name;  -- 刪除表中所有數據, 保留表結構
  1. truncate和delete刪除所有數據時, 當id自增時, 繼續添加數據, delete刪除的數據從之前的自增繼續自增, truncate刪除的數據從1開始自增
  2. 速度上, drop > truncate > delete
  3. 邏輯刪除: 設置標誌位, 不實際刪除數據, 只是標誌該數據被刪除

數據操作

查詢所有字段

selete * from 表名;

查詢部分字段

select 字段1, 字段2, 字段3 from 表名;

起別名

1. 給表起別名  
select 別名.字段1, 別名.字段2 from 表名 as 別名;  
2. 給字段起別名  
select 字段1 as 別名,  字段2 as 別名 from 表名;

起別名時as可以省略, 但是不建議省略, 別名只在查詢結果中起作用, 表本身不受影響

去重

select distinct 字段1, 字段2 ... from 表名;
eg: 
select count(distinct 字段) from 表名; -- 去重後統計數量

結果中一行記錄另一行記錄完全一樣, 纔算是重複數據

條件查詢

使用where子句對錶中的數據進行篩選

1. select 字段 from 表名 where 條件;

條件運算符: =, <, >, >=, <=, !=, <>
邏輯運算符: and, or, not

模糊查詢

關鍵字: like , %代表多個任意字符, _代表一個任意字符

select * from 表名 where 字段名 like 條件;

範圍查詢

關鍵字: in, between…and…

1. select * from 表名 where 字段名 in(條件,條件,條件);  
2. select * from 表名 where 字段名 between 條件1 and 條件2;
3. select * from 表名 where 字段名 not in(條件,條件,條件);  
4. select * from 表名 where 字段名 not between 條件1 and 條件2;

between…and… -> 條件1要小於等於條件2

空判斷

關鍵字: is , null是空,’'是空字符串

select * from 表名 where 字段名 is null;
select * from 表名 where 字段名 is not null;

排序

1. select * from 表名 order by 字段 asc | desc, 字段 asc | desc;  
2. select * from 表名 order by convert(字段 using gbk);  

asc可以省略, 默認升序, 多個字段排序按順序排序
默認排序是排序數字和字母, 中國默認按照文字編碼排序, 可使用convert(), 按開頭字母排序

聚合函數

常用聚合函數: count(), max(), min(), sum(), avg()
count函數內可以寫*, 也可以寫某一列, 如果有null, 不會統計在內
聚合函數不能用在where子句後面

分組

目的: 對每一組數據進行統計

select 字段,聚合函數() from students group by 字段;
eg:  
select class, max(age), min(age), avg(age) from students group by class;

按照某個字段分組後, select後只能寫聚合函數和分組那個字段的名稱

分組後的數據篩選

select 字段,聚合函數() from students group by 字段 having 條件(聚合);
eg:
select sex, count(*) from students group by sex having count(*)>5;

where和having對比

  • where是對from後面指定的表進行數據篩選, 屬於對原始數據的篩選
  • having是對group by的結果進行篩選
  • having後面的條件中可以使用聚合函數, where不可以

獲取部分行(分頁)

關鍵字: limit , 當數據量過大時, 需要分頁查詢

select * from 表名 limit start,count;

start(索引) : 從0開始, 拿count條, start可省略, 默認0, count可超過實際數量

select .... from 表名 .....;
組合查詢使用順序:
where  
group by  
order by  
limit

連接查詢

內連接

select * from1 inner join2 on1.=2.;
擴展語法(不建議使用, 效率低):
select * from1,2 where1.=2.;
eg:
select * from students stu inner join scores sc on stu.studentno = sc.studentno inner join courses cs on sc.courseno=cs.courseno;
eg:
select stu.name 姓名, cs.name 課程, score 成績 from students stu
inner join scores sc on stu.studentno = sc.studentno
inner join courses cs on sc.courseno=cs.courseno
where stu.name='諸葛亮' order by score desc;

連接條件最重要的是on後面的條件, 內連接只顯示匹配成功(交集)的結果

左連接

select * from1 left join2 on1.=2.;

左連接是以左表爲主表, 裏面所有數據全部顯示, 右表沒有對應表數據時以null補全

右連接

select * from1 right join2 on1.=2.;

右連接是以右表爲主表, 裏面所有數據全部顯示, 左表沒有對應表數據時以null補全

自關聯

自己連接自己, 數據擁有層級關係時使用

select * from 表 別名 inner join 表 別名 on 條件;
eg:
select * from areas a1
inner join areas a2 on a1.aid=a2.pid
where a1.atitle='河南省';

子查詢

一個select語句裏嵌套一個select語言, 主查詢, 子查詢

  • 子查詢嵌入到主查詢中
  • 子查詢是輔助主查詢的, 充當條件或者充當數據源
  • 子查詢是可以獨立存在的語句, 是一條完整的select語句

標量子查詢: 子查詢結果只有一行一列
列子查詢: 子查詢結果有一列多行
行子查詢: 子查詢結果有一行多列
表子查詢: 充當數據源, 充當數據源時, 必須要起別名

eg:
select * from students where age=(select min(age) from students);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章