0

如何正确编写我的 sql 查询?我想在查询中使用 WHERE 子句,但我不知道如何正确。这是我的错误查询:

**SELECT pil.[Buy-from Vendor No_],  pil.No_, pil.Amount, pil.Quantity 
FROM dbo.[„blk“ 2011$Purch_ Inv_ Line]AS pil
WHERE pil.Type=5
LEFT JOIN dbo.[„blk“ 2011$Purch_ Inv_ Header] AS pih
ON pil.[Document No_]=pih.No_
ORDER BY pil.amount**
4

6 回答 6

1

WHERE子句移到ORDER BY.

以下是定义SELECT语句外观的文档:http: //msdn.microsoft.com/en-us/library/ms189499.aspx

于 2012-11-07T09:32:32.680 回答
1

这是您处理左连接的方式

SELECT 
    pil.[Buy-from Vendor No_] 
  ,  pil.No_, pil.Amount
  , pil.Quantity 
FROM dbo.[UAB „Arvi cukrus“ 2011$Purch_ Inv_ Line] pil
LEFT JOIN dbo.[„blk“ 2011$Purch_ Inv_ Header] Pih ON pil.[Document No_]=pih.No_
WHERE pil.Type=5
ORDER BY pil.amount
于 2012-11-07T09:37:47.287 回答
1

Where出现在From子句之后。正确的查询是

   SELECT pil.[Buy-from Vendor No_],  pil.No_, pil.Amount, pil.Quantity 
    FROM dbo.[„blk“ 2011$Purch_ Inv_ Line]AS pil
    LEFT JOIN dbo.[„blk“ 2011$Purch_ Inv_ Header] AS pih
    ON pil.[Document No_]=pih.No_
    WHERE pil.Type=5
    ORDER BY pil.amount;
于 2012-11-07T10:28:02.973 回答
0

在所有连接之后放置 where:

SELECT pil.[Buy-from Vendor No_],  pil.No_, pil.Amount, pil.Quantity 
FROM dbo.[„blk“ 2011$Purch_ Inv_ Line]AS pil
LEFT JOIN dbo.[„blk“ 2011$Purch_ Inv_ Header] AS pih
ON pil.[Document No_]=pih.No_
WHERE pil.Type=5
ORDER BY pil.amoun
于 2012-11-07T09:32:15.357 回答
0

放在WHERE前面ORDER BY

于 2012-11-07T09:32:16.527 回答
0

在子句和其他表引用WHERE之后移动子句,如下所示:FROM

SELECT 
  pil.[Buy-from Vendor No_], 
  pil.No_, 
  pil.Amount, 
  pil.Quantity 
FROM ...
...
WHERE pil.Type=5
ORDER BY pil.amount
于 2012-11-07T09:32:38.477 回答