数据库SQL基础知识点
**
1、 多表查詢
**
– 創建teacher 表
create table teacher( id int, name VARCHAR(20) not null, gender CHAR(5) not null, primary key(id) );– 創建學生表格
create table student( id int, name VARCHAR(20) not NULL, age int not null ); alter table student add primary key(id);– 第三張關系表
create table tea_std( id int PRIMARY key auto_increment, teacher_id int, student_id int, CONSTRAINT fk_teacher FOREIGN KEY(teacher_id) REFERENCES teacher(id), CONSTRAINT fk_student FOREIGN key(student_id) REFERENCES student(id) );– 一對一
create table users( id int primary key auto_increment, name varchar(20), age int );create table card( id int PRIMARY key, num VARCHAR(20) not null, address VARCHAR(100) not null,CONSTRAINT user_card_fk FOREIGN KEY(id) REFERENCES users(id) )drop table card; drop table users;– 多表查詢
– 交叉連接,笛卡爾積, 注意 結果不正確。
– 隱式
– 顯式、使用關鍵字
select * from users CROSS JOIN card;– 內連接
– 查詢出每個用戶及身份證信息顯示出來。
– 表格使用別名
select * from users u ,card as c where u.id = c.id;/**
u.* 表示 users 表格中所有的字段,
c.num、c.address 表示 card 表中的字段。
**/
– 查詢用戶對應的所有訂單信息以及個人信息。
select * from customers as c,orders as o where c.id=o.customer_id;– 使用關鍵字 INNER JOIN
select * from customers as c INNER JOIN orders as o ON c.id=o.customer_id;– 左外連接
select * from customers as c LEFT JOIN orders as o ON c.id= o.customer_id;– 右 外連接
select * from customers as c RIGHT JOIN orders as o ON c.id=o.customer_id;select * from orders as o RIGHT JOIN customers as c ON c.id=o.customer_id;– 多對多
create table teacher( id int primary key auto_increment, name varchar(20) not null, sub VARCHAR(20) not null);create table student( id int primary key auto_increment, name varchar(20) not null, gender varchar(5) not null );create table tea_std( id int primary key auto_increment, t_id int, s_id int,score int,CONSTRAINT teacher_fk FOREIGN KEY(t_id) REFERENCES teacher(id), CONSTRAINT student_fk FOREIGN KEY(s_id) REFERENCES student(id) );– 查詢出id為1的老師教過的所有學生。
– 步驟1,先從第三張表中查詢出教過的學生的id
select s_id from tea_std where t_id = 1;– 步驟2:根據id 去學生表中查找學生的信息。
select * from student where id in(1,2);– 使用嵌套子查詢
select * from student where id in(select s_id from tea_std where t_id = 1);– 相關子查詢
– 求:每一科考試成績大于平均分的學生的分數。
-- select * from tea_std; -- select avg(score) from tea_std where t_id=1;select * from tea_std as t1 where t1.score>(select avg(t2.score) from tea_std t2 where t1.t_id=t2.t_id);– 分析步驟:
– 先執行 外部查詢 拿到第一條記錄
– 1 1 1 60
– 執行內部查詢(子查詢)
-- select avg(t2.score) from tea_std t2 where t2.t_id=1– 1 1 1 60
– 2 1 2 90
– 75
– 比較結果 ,每一天記錄的分數 和 平均分進行比較。
– select * from tea_std as t1 where t1.score>75; 60>75
- 聚合函數
count() 計數 ,統計所有的記錄。
– (1)、統計一個班級共有多少學生?
– (2)、統計數學ui大于60的學生有多少個?
select COUNT(*) as total from score where ui>60;– (2)、統計總分大于200的人數有多少?
select count(*) from score where(python+mysql+ui)>200;select * from score where(python+mysql+ui)>200;– count(*) 不會忽略掉 null。 使用其他字段 會自動忽略null。
select count(*) from score; select count(python) from score;– sum() 求和函數
– (1)、統計一個班級mysql總成績?
– (2)、統計一個班級python、mysql、ui各科的總成績
select sum(python),sum(mysql),sum(ui) from score;– (3)、統計一個班級每一個人的 python、mysql、ui的成績總和。
– `
– (4)、統計一個班級python成績平均分
select AVG(python) from score; select sum(python)/COUNT(python) from score; select sum(python)/COUNT(*) from score;– avg(): 求平均數。
– (2)、求一個班級總分平均分
– SELECT AVG(chinese+english+math) FROM student
– min() 求最小 、max() 求最大
– 1.對訂單表中商品歸類后,顯示每一類商品的總價
select product, SUM(price) from t_order GROUP BY product;– 2.對訂單表中商品歸類后,查詢每一類商品總價格大于3000的商品
select product, SUM(price) as total from t_order GROUP BY product HAVING total>3000;select product, SUM(price) from t_order GROUP BY product HAVING SUM(price)>3000;總結
以上是生活随笔為你收集整理的数据库SQL基础知识点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库SQL基础语法
- 下一篇: 数据库外键约束的几种方法及区别