mysql資料庫外連線,內連線,自然連線

2022-08-30 02:27:11 字數 2089 閱讀 9622

create table join_teacher(

id int primary key auto_increment,

t_name varchar(10) not null,

gender enum('male','female','secret') not null

)engine innodb character set utf8;

insert into join_teacher values

(1,'韓信','male'),

(2,'李白','male'),

(3,'韓非子','secret');

create table join_class(

id int primary key auto_increment,

c_name varchar(10) not null,

root int not nclass values

(1,'php0115',207),

(2,'php0115',207),

(3,'php0115',207);

create table join_day(

id int primary key auto_increment,

t_id int not null,

c_id int not null,

days tinyint not null,

begin_date date not null,

end_date  date not null

)engine innodb character set utf8;

insert into join_day values

(1,1,1,15,20130115,20130220),

(2,2,2,20,20130222,20130325),

(3,3,3,15,20130327,20130418);

tbl_left inner join tbl_right on 連線條件

inner join

select join_teacher.t_name,join_teacher.gender,join_day.begin_date,join_day.end_date

from join_teacher inner join join_day on

join_teacher.id=join_day.t_id;

select * from join_teacherinner joinjoin_day on

join_teacher.id=join_day.t_id;

leftouter join

select * from join_teacher left outer join join_day on

join_teacher.id=join_day.t_id;

join 連線查詢:將多個表的記錄連線起來

分類:內連線,外連線,自然連線

內連線:資料內部的連線,要求:連線的多個資料必須存在才能連線

外連線:如果負責連線的乙個或多個資料不真實存在,則稱之為外連線

select s.*,si.* from info_class as cleft joininfo_student as s on c.id=s.class_id

left joininfo_student_info as si on s.id=si.id where c.class_name='php0331';

select child.* from hd_cate as parent left join hd_cate as child on child.pid=parent.id where parent.name='mysql';

格式:select * from table1

left join table2 on 條件

left join table3 on 條件

......

按列往後加資訊ull

)engine innodb character set utf8;

insert into join_

資料庫04去重,內連線,外連線,自連線

去重 select distinct job from emp distinct只能出現在所以欄位最前面。selsect distinct deptno,job from emp 兩個字段一起去重 統計崗位數量 select count distinct job from emp 連線查詢 實際開發...

資料庫 連線(自然連線 外連線 內連線)

1 自然連線 只考慮那些在兩個關係模式中都出現的屬性上取值相同的元組對natural join join.using select a1,a2,an from r1 natural join r2 natural join natural join rn where p select name1,c...

資料庫 左外連線 右外連線 內連線

資料庫 左外連線 1.左外連線的主表在左邊,即連線兩個表時,保留左表中的不匹配部分,右表的相應項用null 或0 值表示。如圖 表ax 表bx from bx left outer join ax 左表,是指from 句中的左邊的表 bx.on ax.id bx.id 結果如圖 2.右外連線 右表的...