[英]SQL Join query in SqlDataSource
I am not really sure about how to do this. 我不确定如何执行此操作。
In a table Product1
I have: 在表
Product1
我有:
Id, Code, Description, Group, Quantity, Unit, Cost, Note
In the other table Componets
I have: 在另一个表
Componets
我有:
Id, Code, Description, Unit, Cost, Note
The problem I am facing is that while table Components
has always all columns with data, table Product1
should take the data relevant "Description, Unit and Cost" from table "Components". 我面临的问题是,尽管表
Components
始终具有所有带有数据的列,但表Product1
应该从表“组件”中获取与“描述,单位和成本”相关的数据。 In this table "Product1" basically only the Code, Quantity, Group and Note are added by the user. 在此表“ Product1”中,用户基本上仅添加代码,数量,组和注释。
Is this possible to achieve this with a query made in a SqlDataSource
? 是否可以通过在
SqlDataSource
进行查询来实现此目的? I have no idea on how to do this and I would appreciate your help. 我不知道如何执行此操作,感谢您的帮助。
So far this is what I tried: 到目前为止,这是我尝试过的:
SELECT
Product1.Code, Group, Quantity, Product1.Note
FROM
dbo.Product1
INNER JOIN
dbo.Components ON Components.Code = Product1.Code
But I do not get what I need, I get only Code
, Group
, Quantity
and Note
from Product1
table 但是我没有得到我需要的东西,我只从
Product1
表中获得Code
, Group
, Quantity
和Note
It means your fields (columns) Code and Note appear in both tables; 这意味着您的字段(列)的“代码”和“注释”出现在两个表中; thus one should use a table prefix, which points out from which table one will fetch the column:
因此,应该使用一个表前缀,该前缀指出从哪个表中获取列:
SELECT Components.Code AS 'Components Code', Product1.Code AS 'Product Code', Groupp, Quantity, Components.Note AS 'Components Node', Product1.Note AS 'Product Note'
FROM
dbo.Product1
INNER JOIN dbo.Components
ON Components.Code = Product1.Code
As demonstrated above, one can include multiple columns and give them a label when fetched. 如上所示,可以包含多个列,并在提取时为它们提供标签。
First, lets understand why they are 'ambiguous'. 首先,让我们了解为什么它们“模棱两可”。
Because your join is on two tables both having the same column names. 因为您的联接位于两个具有相同列名的表上。 Since, you have not specified the table name for those columns, the SQL engine is confused which table you want those columns from?
由于您尚未为这些列指定表名,因此SQL引擎会混淆您要从哪个表中获取这些列? Hence, it says ambiguous.
因此,它含糊不清。 Just use an alias as such:
只需使用别名即可:
GENERAL IDEA
: GENERAL IDEA
:
SELECT tab.id, tab.Name
FROM TableName tab
WHERE tab is called the alias. WHERE选项卡称为别名。
With respect to you question, this the query you need: 关于您的问题,此查询您需要:
SELECT prod.Code,
prod.Groupp,
prod.Quantity,
prod.Note,
comp.Description,
comp.Unit,
comp.Cost
FROM dbo.Product1 prod
INNER JOIN dbo.Components comp
ON comp.Code = prod.Code
//Any Where conditions should follow here
Observe how prod
and comp
are used as the aliases. 观察
prod
和comp
如何用作别名。 They will not cause you any 'ambiguity'. 它们不会引起您任何“歧义”。
Hope this helps!!! 希望这可以帮助!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.