MySQL多表查詢

2021-07-31 08:24:46 字數 3857 閱讀 9845

//方式一

create

table stu(sid int

primary

key,sname varchar(20),age int);

//方式二

create

table stu(sid int,sname varchar(20),age int, primary

key(sid));

//方式三

create

table stu(sid int,sname varchar(20),age int);

alter

table stu add

primary

key(sid);

//刪除主鍵

alter

table stu drop

primary

key;

//1.從表

create table dept(

deptno int primary key,

dname varchar(20)

);insert into dept value(10,"研發部");

insert into dept value(20,"財務部");

insert into dept value(30,"運營部");

//2.主表

create table emp(

empno int primary key,

ename varchar(20),

deptno int,

constraint fk_emp_dept foreign key (deptno) references dept(deptno)

);

//1.從表

create table hasband(

hid int primary key,

hname varchar(20)

);select * from hasband;

insert into hasband value(1,"劉備");

insert into hasband value(2,"關羽");

insert into hasband value(3,"張飛");

//2.主表(主表的主鍵作為外來鍵)

create table wife(

wid int primary key,

wname varchar(20),

constraint fk_wife_hasband foreign key(wid) references hasband(hid)

);insert into wife value(1,"小喬");

insert into wife value(2,"貂蟬");

insert into wife value(3,"吳氏");

//學生表

create table student(

sid int primary key,

sname varchar(20)

);//老師表

create table teacher(

tid int primary key,

tname varchar(20)

);//關聯表

create table stu_tea(

sid int,

tid int,

constraint fk_stu foreign key(sid) references student(sid),

constraint fk_tea foreign key(tid) references teacher(tid)

);insert into student value(1,"老大");

insert into student value(2,"老二");

insert into student value(3,"老三");

insert into student value(4,"老四");

insert into student value(5,"老五");

insert into teacher value(1,"***");

insert into teacher value(2,"張老師");

insert into teacher value(3,"劉老師");

insert into teacher value(4,"武老師");

insert into stu_tea value(1,1);

insert into stu_tea value(2,1);

insert into stu_tea value(3,1);

insert into stu_tea value(4,1);

insert into stu_tea value(5,1);

insert into stu_tea value(1,2);

insert into stu_tea value(2,2);

insert into stu_tea value(3,2);

insert into stu_tea value(2,3);

insert into stu_tea value(3,3);

insert into stu_tea value(4,3);

insert into stu_tea value(5,3);

insert into stu_tea value(3,4);

insert into stu_tea value(4,4);

insert into stu_tea value(5,4);

合併結果集

連線查詢

內連線左外連線

右外連線

子查詢

create table aa(a int,b varchar(20));

create table bb(c int,d varchar(20));

//1.查詢全部

select * from aa

union all

select * from bb;

//2.去重複查詢

select * from aa

union all

select * from bb;

//1.方言

select *

from emp,dept

where emp.deptno = dept.deptno;

//2.標準

select *

from emp inner join dept

on emp.deptno = dept.deptno

//3.自然

select *

from emp natural join dept

select * 

from emp left outer join dept

on emp.deptno = dept.deptno

select * 

from emp right outer join dept

on emp.deptno = dept.deptno

左外連線與右外連線合併結果集去重複就是全外連線
//查詢工資高於諸葛亮的員工資訊

select *

from emp

where sal >(select sal from emp where ename="諸葛亮");

mysql多表 MySQL 多表查詢

多表查詢 select listname from tablename1,tablename2 笛卡爾積 多表查詢中,如果沒有連線條件,則會產生笛卡爾積 數學中的定義 假設集合a 集合b 則兩個集合的笛卡爾積為 實際執行環境下,應避免使用笛卡爾積 解決方案 在where加入有效的連線條件 等值連線 ...

mysql多表查詢方式 MySQL多表查詢方式問題

你的 sql 沒有用到任何索引,對 a b 兩個表都是全表掃瞄,在資料量小的時候是沒有問題的,但是如果資料量超過 100 萬,效能問題就會突顯出來。這裡不清楚你的 created at 欄位是什麼型別,不過從你的 date format created at,y m d 看來,應該是 datetim...

mysql 多表查詢or MySQL 多表查詢

前期準備 建表create table dep id int,name varchar 20 create table emp id int primary key auto increment,name varchar 20 enum male female not null default ma...