本文共 1328 字,大约阅读时间需要 4 分钟。
下面列一些示例代码,帮助大家理解。
select t2.* --表自关联from Tree t1inner join Tree t2on t1.NO=t2.ParentNowhere t1.Name='Node1_2';select * from tree --子嵌套查询where ParentNo = (select NO from tree where Name = 'Node1_2');select * from tree where parentNo in (select no from tree where Name = 'Node1_2');
上面是表自关联,关键字inner,就是返回t1.NO=t2.ParentNo条件的所有t2表的数据,
有表自关联,当然还有左关联,右关联,示例代码:
select t2.* --左关联from Tree t1left join Tree t2on t1.NO=t2.ParentNowhere t1.Name='Node1_2';select t2.* --右关联from Tree t1right join Tree t2on t1.NO=t2.ParentNowhere t1.Name='Node1_2';
视图的关键字是View,就是一个查询集合,方便我们去查询数据,视图其实就是表,多表连接的表,我们查询的时候不需要反复的去拼接语句,直接查询视图就可以,方便我们的操作,当然一些简单的查询操作就没必要去创建视图了。
示例代码:
create view Production.vw_Productas select t1.* from Production.Product t1 left outer join Production.ProductModel t2 on t2.ProductModelID=t1.ProductModelID left outer join Production.ProductSubcategory t3 on t3.ProductSubcategoryID=t1.ProductSubcategoryID left outer join Production.UnitMeasure t4 on t4.UnitMeasureCode=t1.SizeUnitMeasureCode and t4.UnitMeasureCode=t1.SizeUnitMeasureCode --order by t1.ProductID
查询视图:
select *from Production.vw_Product
表关联和视图都是比较简单的数据库操作,但也是比较常用的。大家好好练习。
还有一些相关编程知识的整理,希望大家关注下。。。
本文转自田园里的蟋蟀博客园博客,原文链接:http://www.cnblogs.com/xishuai/p/3373562.html,如需转载请自行联系原作者