sql中的join

---------------------------------题记

在使用hibernate的hql区分隐式(implicit)与显式(explicit)关联的时候,根据show_sql的语句,在数据库做了一下简单的测试,以加深印象。其中如下查询语句中1为隐式查询(如:hql 'from student s where s.class.id=s.id'),2、3、4为显式查询(如:hql 'from student s join class c on c.id=s.id')。在hibernate3.2.3以后的版本不支持1-n、n-n,单个实体和组件依然有效。

---------------------------------代码

create table t_temp_student(
  pk_id int primary key not null,
  f_name varchar(20),
  f_age int,
  fk_class_id int
);

create table t_temp_class(
  pk_id int primary key not null,
  f_name varchar(20)
);

insert into t_temp_class values(1,'一班');
insert into t_temp_class values(2,'二班');
insert into t_temp_class values(3,'三班');

insert into t_temp_student values(1,'张学友',10,1);
insert into t_temp_student values(2,'刘德华',11,1);
insert into t_temp_student values(3,'黎明',10,2);
insert into t_temp_student values(4,'郭富城',9,2);
insert into t_temp_student values(5,'陈冠希',5,4);

-- 下列1、2、3句效果相同(除cross join,其他必须携带on,即join是inner join的简写)
select * from t_temp_student s cross join t_temp_class c where s.fk_class_id = c.pk_id;	-- 关联属性只能携带在where后面,不能有on,也可以没有where
select * from t_temp_student s inner join t_temp_class c on s.fk_class_id = c.pk_id; -- 不能没有on
select * from t_temp_student s join t_temp_class c on s.fk_class_id = c.pk_id;	-- 不能没有on
select * from t_temp_student s left join t_temp_class c on s.fk_class_id = c.pk_id; -- 不能没有on,且left join左侧数据全部列出,若不满足on,则left join左侧数据对应的右侧数据为null


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值