1. 程式人生 > >Mysql-基本查詢

Mysql-基本查詢

增加

insert into table_name[(column[,column...])]
    values (value [,value...]);

在查詢之前我們可以建立一張商品表並插入幾條資料.

--建立一張商品表
create table goods(
	id int unsigned primary key,
	name varchar(100) not null default '',
	price float not null default 0.0
);

插入資料

insert into goods values (1, '巧克力', '2.5');
insert into goods values (2, '棒棒糖', '0.5');
insert into goods values (3, '方便麵', '5.0');
--批量插入
insert into goods values (4, '奶寶', '1.5'), (5, '辣條', '0.5');

在插入資料時要注意資料與欄位的資料型別相同,資料大小在規定的範圍內,隱含列插入時資料的位置與列的位置相同,字元和日期應包含在單引號中。如果只想給表的某幾個欄位賦值,需要指定欄位名稱。

在插入資料時,對應主鍵已經存在,插入失敗,此時我們可以更新資料或者完全替換。

更新

insert into 表名(欄位列表) values(值列表) on duplicate key update 欄位=新值;

我們要改變方便麵的價格,直接插入會報錯

insert into goods values (3, '方便麵', 4.5);
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'

採用資料更新

insert into goods values(3, '方便麵', 5) on duplicate key update price=4.5;
Query OK, 2 rows affected (0.11 sec)

替換

replace into 表名(包含欄位) values(值列表);

更新

update tbl_name set col_name1=expr1, [, col_name2=expr2 ...] [where conditon] [limit n]

將所有產品的價格設為4元

update goods set price=4;

將id為5的產品價格改為0.5元

update goods set price=0.5 where id=5;

將id為1的產品價格增加一元

update goods set price=price+1 where id=1;

刪除

delete from tbl_name [where condition]

刪除表中id為1的資料

delete from goods where id=1;

複製表結構

create table good2 like goods;

兩張表的結構是一樣的,但是新表是沒有資料的。

複製資料,把goods的資料複製到good2

insert into good2 select * from goods;

刪除表中所有資料:

delete from goods; --刪除資料,但是表的結構還在

清空表的資料:

truncate table goods;

使用truncate和delete的效果是一樣的,但是truncate的速度較為快一些,delete返回被刪除的記錄數,truncate返回0。

查詢

select [distinct] *| {column1,column2,...} from tbl_name [where condition];

建立一張學生表

create table student (
	id int unsigned primary key,
	name varchar(20) not null default '',
	chinese float not null default 0.0 comment '語文成績',
	english float not null default 0.0 comment '英語成績',
	math float not null default 0.0 comment '數學成績'
);

插入資料:

insert into student values(1, '張三', 76, 89, 90);
insert into student values(2, '李四', 80, 59, 40);
insert into student values(3, '王五', 88, 22, 79);
insert into student values(4, '趙六', 87, 90, 67);
insert into student values(5, '田七', 30, 68, 89);
insert into student values(6, '曹八', 60, 71, 90);
insert into student values(7, '孫九', 77, 82, 83);
insert into student values(8, '秦十', 98, 99, 85);

使用*表示查詢所有列,當表中資料有很多,查詢所有會降低效率,所以一般是用哪些欄位就取哪些欄位。

distinct去重複行

select distinct math from student;

as起別名

select column as 別名 from 表;

查詢所有學生的總分:

select name, chinese+english+math as total from student;

姓張的同學查詢總分再增加60%:

select name, (chinese+english+math)*1.6 as total from student where name like '張%';

查詢姓田的同學成績

select * from student where name like '田%';

查詢英語成績大於90分的同學

select * from student where english > 90;

查詢總分大於200的同學

select name, (chinese+english+math) as total from student where  chinese+english+math > 200; --where條件的後面不能用別名total

查詢姓張且id大於5的同學

select *from student where name like '張%' and id > 5;

查詢英語成績大於語文成績的同學

select * from student where english > chinese;

查詢總分大於200並且數學成績大於語文成績的姓孫的同學

select * from student where name like '孫%' and math > chinese and chinese+english+math > 200;

查詢數學成績在60-80之間的同學

--1
select * from student where math >= 60 and math <= 80;
--between, 閉區間
select * from student where math between 60 and 80;

查詢數學成績為60, 70, 80 的同學

select * from student where math=60 or math=70 or math=80;

刪除表中重複記錄,重複資料只有一份

--建立一張新表,表結構與student相同
create table stu like student;
--把student去重後的資料插入新表
insert into stu select distinct * from student;
--刪除舊錶
drop table student;
--修改新表名稱與舊錶相同
alter table stu rename student;

order by排序

select column1,column2,... from table order by column asc|desc,...;
--order by 排序指定的列
--asc升序(預設), desc降序
--order by字句位於select語句結尾

對數學成績進行排序

select * from student order by math; --預設升序
select * from student order by math desc; --降序

對總分進行排序,降序

select id, name, (chinese+math+english) as total from student order by total desc;

對姓張的同學按成績進行從高到低排序

select id, name, (chinese+english+math) as total from student where name like '張%' order by total desc;

limit分頁

select 欄位 from 表名 where 條件 limit 起始位置 ,記錄條數
select 欄位 from 表名 where 條件 limit 記錄條數 offset 起始位置

按學生的id升序取數,每頁顯示3條記錄。

--第一頁
select * from student limit 0, 3;
--第二頁
select * from student limit 3, 3;
--第三頁
select * from student limit 6, 3;

聚合函式

count

count(列名)返回某一列,行的總數

select count(*)|count(列名) from tbl_name where condition;

統計一個班的學生總數

select count(*) as total from student;

統計數學成績大於等於90的人數

select count(*) from student where math >= 90;

統計總分大於250的人數

select count(*) from student where chinese+math+english >= 250;

sum

sum返回滿足where條件的行的和

select sum(列名) {,sum(列名)...} from tbl_name [where condition];

統計一個班的數學總成績

select sum(math) from student;

統計一個班語文, 數學, 英語各科的總成績

select sum(chinese), sum(math), sum(english) from student;

統計一個班的總成績

select sum(chinese+math+english) from student;

數學成績的平均分

select sum(math)/count(math) from student;

avg

agv函式返回滿足where條件的一列平均值

select avg(列名) [,avg(列名),...] from tbl_name [where condition];

求一個班數學平均分

select avg(math) from student;

求一個班的總分均值

select avg(chinese+math+english) from student;

max/min

返回滿足where條件的一列的最大最小值

select max(列名) from tbl_name [where condition]

求班級最高分和最低分

select max(chinese+english+math), min(chinese+english+math) from student;

group by

對指定列進行分組查詢

select column1, column2, .. from table group by column;