多表操作

阅读更多

1、left join和 多表查询 

select t1.cardtype,t1.description,t1.enabled,t1.service,t2.CARD_TYPE_GROUP_NAME 
from prepaid_remit_pccw_cardtype t1 left join PREPAID_REMIT_CARDTYPE_GROUP t2 on t1.card_type_group_id = t2.card_type_group_id order by t1.service,t1.cardtype

 

区别:
left join 所有的工作以left为准 以左表为准,所以 
1 如果左表中有空,那么就会显示一条空记录 
2 如果左表中有一条记录在右表中找不到,同样这条记录也会显示出来 

多表查询则不然,只要where中不匹配则不显示该条记录

select t1.cardtype,t1.description,t1.enabled,t1.service,t2.CARD_TYPE_GROUP_NAME 
from prepaid_remit_pccw_cardtype t1 ,PREPAID_REMIT_CARDTYPE_GROUP t2 where t1.card_type_group_id = t2.card_type_group_id order by t1.service,t1.cardtype

 

2.多表插入

使用符合语句:

insert into prepaid_remit_pccw_cardtype t (t.cardtype,t.enabled,t.description,t.service,t.card_type_group_id)
values (1,'Y','a','b',(select g.card_type_group_id from PREPAID_REMIT_CARDTYPE_GROUP g
where g.card_type_group_name='ab'))

 

你可能感兴趣的:(DB,多表)