SQL语句..具有相同多个Id的多个表

时间:2012-05-25 04:58:31

标签: sql sql-server sql-server-2008

enter image description here我正在使用VB.Net连接到SQL Server 2008

此作品 - > Dim query =“SELECT wo.WorkOrderID,wo.WorkOrderNo FROM dbo.WorkOrder wo”

这不起作用 - > Dim query =“SELECT wo.WorkOrderID,wo.WorkOrderNo,wod.LineNo FROM dbo.WorkOrder wo,dbo.WorkOrderDetails wod”

这也不起作用 - > Dim query =“SELECT wo.WorkOrderID,wo.WorkOrderNo,wod.LineNo FROM dbo.WorkOrder wo,dbo.WorkOrderDetails wod WHERE wo.WorkOrderID = wod.WorkOrderID“

我假设它,因为在WorkOrderDetails中,WorkOrderId具有多个相同的ID

  • 在WorkOrder中,WorkOrderID似乎是增量的...... 1,2,3等..
  • 在WorkOrderDetails中,WorkOrderID就像这样.. 1,1,1,1,1,1,2,2,2,2,3,3,3,3,3等..

有关如何使其发挥作用的任何想法?我无法理解......

  • 关键字“LineNo”附近的Incorrent语法。是我得到的错误

我刚试过这个 - > “SELECT WorkOrderID,WorkOrderNo,LineNo FROM dbo.WorkOrder JOIN dbo.WorkOrderDetails ON dbo.WorkOrder.WorkOrderID = dbo.WorkOrderDetails.WorkOrderID”

似乎没有工作..同样的错误

2 个答案:

答案 0 :(得分:4)

LineNoreserved keyword。使用Delimited Identifier

SELECT WorkOrderID, WorkOrderNo, [LineNo]
FROM dbo.WorkOrder
  JOIN dbo.WorkOrderDetails
    ON dbo.WorkOrder.WorkOrderID = dbo.WorkOrderDetails.WorkOrderID

答案 1 :(得分:0)

使用join:

Dim query = 
"SELECT wo.WorkOrderID,
       wo.WorkOrderNo, 
       wod.[LineNo] FROM dbo.WorkOrder wo
                 JOIN dbo.WorkOrderDetails wod 
                 ON wo.WorkOrderID = wod.WorkOrderID"

此网站将帮助您开始:

sql tutorial

最佳观点