博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
T-Sql(四)表关联和视图(view)
阅读量:7125 次
发布时间:2019-06-28

本文共 1328 字,大约阅读时间需要 4 分钟。

 表关联(join)  

  下面列一些示例代码,帮助大家理解。

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

  视图的关键字是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,如需转载请自行联系原作者

你可能感兴趣的文章
华佗教你睡觉 一定要看
查看>>
我的友情链接
查看>>
Linux中设置服务自启动的三种方式
查看>>
windows应用程序框架及实例
查看>>
frida Hook 重载方法
查看>>
【CentOS 7.1】 mount 3T
查看>>
iOS 性能优化:Instruments 工具的救命三招
查看>>
数据中心那些常见的问题
查看>>
Linux服务器关闭/开启ICMP协议(ping)
查看>>
我的友情链接
查看>>
Linux基础
查看>>
Linux逻辑卷快照及ssm的使用
查看>>
关于工资的三个秘密
查看>>
NFS服务器配置
查看>>
APUE读书笔记-05标准输入输出库(2)
查看>>
mysql中replace into、replace into、insert ignore用法区别
查看>>
Sql Server系列:数据库组成及系统数据库
查看>>
***之open***
查看>>
tengine+uwsgi+django
查看>>
Django 模板语法取值
查看>>