高薪:數據庫優化,線程,算法,框架源碼,設計模式
pagesize:頁面大小(每頁顯示的數據條數)
pageno:當前頁(頁碼)
SELECT <字段名列表>FROM <表名或視圖>[WHERE <查詢條件>][GROUP BY <分組的字段名>]
[ORDER BY <排序的列名>[ASC 或 DESC]]
[LIMIT (pageno-1)*pagesize,pagesize];
5.切換數據庫
use holly;
6.查看數據庫表
show tables;
7.查看錶結構
desc student;
8.添加數據
insert into student
(sname,sex,age,studytime,subject,score) values
('超羣','女',18,'2017-01-01','java',85.6);
insert into student
(sname,sex,age,studytime,subject,score) values
('晶晶','女',18,'2017-01-02','sql',96),
('奔奔','男',28,'2017-01-03','sql',96);
commit;
9.查看數據
select * from student;
+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | ? | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超羣 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |
| 4 | 奔奔 | 男 | 28 | 2017-01-03 | sql | 96.00 |
+—–+——-+——+——+————+———+——-+
10.修改數據
update student set sex='女' where sid=1;
commit;
11.查看數據
select * from student;
+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | 女 | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超羣 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |
| 4 | 奔奔 | 男 | 28 | 2017-01-03 | sql | 96.00 |
+—–+——-+——+——+————+———+——-+
12.複製表結構(列名)和表數據:不復制序列和主外鍵
create table newstudent(select * from student);
13.刪除某一條表數據:序列不變
delete from student where sid=4;
commit;
14.查看數據
select * from student;
+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | 女 | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超羣 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |
15.插入數據
insert into student
(sname,sex,age,studytime,subject,score) values
('奔奔','女',18,'2017-01-01','java',85.6);
commit;
- 查看數據庫表
select * from student;
+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | 女 | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超羣 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |
| 5 | 奔奔 | 男 | 28 | 2017-01-03 | sql | 96.00 |
+—–+——-+——+——+————+———+——-+
–18.刪除數據表
drop table student;
–19.如果有兩個表,這個兩個表有關聯,先查找一方和多方,分清主表,從表
– 一方:主表,多方:從表
– 創建表時創建主表再創建從表
–創建主表classes表
create table classes
(
cid int unsigned auto_increment primary key,
cname varchar(20) not null
);
insert into classes(cname) values('TB113'),('TB116');
commit;
–創建從表student表
create table student
(
sid int unsigned auto_increment primary key,
sname varchar(20) unique,
sex char(4) default '男',
age int(10) default 18,
studytime date,
cid int references classes(cid)
);
insert into student(sname,studytime,cid) values
('張三','2017-01-01',1),
('李四','2017-01-02',2),
('魏巍','2017-01-03',2),
('王五','2017-01-04',2),
('王二','2017-01-05',2);
commit;
–20.兩邊聯查加別名
select s.cid scid,c.cid as ccid from student s,classes c
where c.cid=s.cid;
+——+——+
| scid | ccid |
+——+——+
| 1 | 1 |
| 2 | 2 |
| 2 | 2 |
| 2 | 2 |
| 2 | 2 |
+——+——+
21.統計所有的學生人數
select count(*) from student;
select count(1) from student; √
22.獲取最大的學生編號
select max(sid) from student;
23.獲取最小的學生編號
select min(sid) from student;
23.獲取編號的平局值
select avg(sid) from student;
24.獲取年齡值的總和
select sum(age) from student;
25.排序升序
select cid from classes order by cid;
select cid from classes order by cid asc;
25.排序降序
select cid from classes order by cid desc;
26.按照cid分組分組(只能顯示分組字段和聚合函數)
–統計每個班級下的學生人數
select cid,count(cid) from student group by cid;
+——+————+
| cid | count(cid) |
+——+————+
| 1 | 1 |
| 2 | 4 |
+——+————+
–對分組再次篩選建議使用having 而不是where
select cid,count(cid) from student group by cid having cid>1;
27.先分組再排序
select cid,count(cid)
from student
group by cid
order by cid desc;
28.分頁查詢,每頁顯示3條數據,查詢第一頁,第二頁
select * from student limit 0,3;
+—–+——-+——+——+————+——+
| sid | sname | sex | age | studytime | cid |
+—–+——-+——+——+————+——+
| 1 | 張三 | 男 | 18 | 2017-01-01 | 1 |
| 2 | 李四 | 男 | 18 | 2017-01-02 | 2 |
| 3 | 魏巍 | 男 | 18 | 2017-01-03 | 2 |
+—–+——-+——+——+————+——+
select * from student limit 3,3;
+—–+——-+——+——+————+——+
| sid | sname | sex | age | studytime | cid |
+—–+——-+——+——+————+——+
| 4 | 王五 | 男 | 18 | 2017-01-04 | 2 |
| 5 | 王二 | 男 | 18 | 2017-01-05 | 2 |
+—–+——-+——+——+————+——+
–29.查詢班級編號爲2的第一頁數據,每頁顯示3條數,
select * from student where cid=2 limit 0,3;
–30.查詢和李四同班(cid一樣)的所有的學生信息
–查詢李四同學的班級編號
select cid from student where sname='李四'
–查詢和李四同班的所有的學生信息
select * from student
where cid=(select cid from student where sname='李四');
–31.統計和李四同班的所有的學生人數
select count(*) from student
where cid=(select cid from student where sname='李四');
–32.查詢學生編號在1,3,5,7,9範圍內的學生信息
–in返回範圍內存在的數據,
select * from student where sid in(1,3,5,7,9);
–33.查詢班級編號爲2,並且姓名存在與所有學生姓名中的學生信息
select * from student
where cid=2
and sname in (select sname from student);
–34 查詢學生編號不在1,3,5,7,9範圍內的學生信息
select * from student where sid not in(1,3,5,7,9);