Sql Server中Fetchichg数据的问题 [英] Trouble in Fetchichg Data in Sql Server

查看:50
本文介绍了Sql Server中Fetchichg数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ive Four Tables

1 Sale(saleID int identity,AID) - 来自分配表的援助

2分配(AID int Identity,PurchaseID int) - PurchaseID从购买表

3购买(PurchaseID int,ProductID int) - 产品表中的ProductID

4产品(ProductID int Identity,ProductName)



我想从ProductTable Base获取ProductName Sale在售价ID





促销表



saleID - AID

- 1 ----- 2



分配表



AID --- PurChaseID

1 ----- 2

2 ---- -1



购买表



购买ID-ProductID

--- -1 ------- 2

---- 2 ------- 1



产品表

ProductID-ProductName

--- 1 ------- CellPhone

--- 2 -------笔记本电脑



我想结果为



SaleID - 产品名称

- -1 ----笔记本<无线电通信/>


我可以这样做使用表变量但是它会花费更多的执行时间

所以我想要一个小查询....我很安静困惑什么要做什么不做请帮助.......





逻辑我用于表变量是

首先我在Squence中从Sale Sale获得All AID

基于这些AID我将按顺序收集所有PurchaseID的分配表

基于这些PurchaseID的I从序列中收集购买表中的所有ProductID

并使用这些ProductID我在序列中得到他们的名字

之后我将所有SaleID和所有ProductName插入一个表中变量并选择它作为结果

解决方案

您需要了解 JOIN [ ^ ]



或者使用Visual Studio中提供的工具来构建查询给你。



喜欢这个

 SELECT sale.SaleID,Product.ProductName 
FROM sale
INNER JOIN分配ON Allocation.AID = sale.AID
INNER JOIN购买ON Allocation.PurChaseID = Purchase.PurchaseID
INNER JOIN产品ON Purchase.ProductID = Product.ProductID


Ive Four Tables
1 Sale(saleID int Identity,AID)--Aid From Allocation Table
2 Allocation(AID int Identity, PurchaseID int)--PurchaseID From Purchase Table
3 Purchase(PurchaseID int,ProductID int)--ProductID From Product Table
4 Prodcut(ProductID int Identity,ProductName)

I want To Get ProductName From ProductTable Base On SaleID

i.e.
sale Table

saleID--AID
--1-----2

Allocation Table

AID---PurChaseID
1-----2
2-----1

Purchase Table

PurchaseID-ProductID
----1-------2
----2-------1

Product Table
ProductID-ProductName
---1-------CellPhone
---2-------Laptop

I want Result as

SaleID--ProductName
---1----Laptop

I can do This Using Table Variable but It will Take more Execution Time
So I Want a Small query....Im Quiet Confused What to do and What to not Please Help.......


Logic I used For Table Variable is
First I took All AID From Sale Master in Squence
Based On these AID's I'm Collecting All PurchaseID's From Allocation Table in Sequence
Based On These PurchaseID's I'm Collecting All ProductID's From Purchase Table in Squence
and Using these ProductID's I get their Names in Squence
After that I'm Inserting All SaleID's and All ProductName in One Table Variable and Selecting it as a Result

解决方案

You need to learn about JOIN [^]

Or use the tools provided in Visual Studio to build queries for you.

Like this

SELECT sale.SaleID, Product.ProductName
FROM sale 
INNER JOIN Allocation ON Allocation.AID = sale.AID 
INNER JOIN Purchase ON Allocation.PurChaseID = Purchase.PurchaseID 
INNER JOIN Product ON Purchase.ProductID = Product.ProductID


这篇关于Sql Server中Fetchichg数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆