0

我有一个 SQL 查询,我必须查询三个表(其中一些表两次),但我没有得到我需要的结果:

我需要在“文章”中搜索搜索字符串(“网球”)并为这些文章查找第二个表“订单详细信息”。点击显示订购了多少。

现在我需要检查这些物品是否已经交付。因此,我在“orderdetails”表中查找“orders”,如果这些订单有“delivery”,则查看同一张表。'orders' 和 'delivery' 有一个字段显示它是什么类型。

所以我必须检查“交货”是否由“订单”转发。

接下来检查这个'delivery'是否包含'article'并总结交付的文章。

如果交付的文章数量低于订购的文章数量我想显示这条记录。

到目前为止,除了这些项目之外它还有效: - [已解决] 感谢 HLGEM 没有显示“交货”的“订单” - [已解决] 显示与“交货”相同数量的“订单”,但我没有希望他们。

这是我到目前为止所拥有的:

PrO:流程订单
PrD:流程交付
a:ArticleItem
p:processOrderItem
d:deliveryItem

[更新代码]

SELECT 
    a.Articlenumber AS Article,
    PrO.Number AS Order, 
    PrD.Number AS Delivery,
    p.Amount AS Orderamount,
    SUM(d.Amount) AS Deliveryamount,
    (p.Amount - Deliveryamount) AS OpenAmount

FROM Article AS a
    INNER JOIN ProcessesDetails AS p
    ON  (a.ArticleNumber = p.Article)
        AND LEFT(p.Order, 3) = 'OR-'
    INNER JOIN Processes as PrO
    ON  PrO.Number = p.Order
        AND TEXTSEARCH('Delivery:' IN PrO.Forwarded)
    LEFT JOIN Processes as PrD
    ON  PrO.Nummer = PrD.ForwardedFrom
        AND LEFT(PrD.Number,3) = 'DE-' 
    INNER JOIN ProcessesDetails as d
    ON  PrD.Number = d.Order
        AND d.Article = p.Article
WHERE (a.Categorie = 'tennis') 
GROUP BY(Article)

添加以下行解决了第二个问题:

HAVING Deliveryamount < Orderamount
4

3 回答 3

1

我不知道在 where 条件下这条线是什么:

AND TEXTSEARCH('Delivery:' IN PrO.Forwarded)

我认为textsearchmysql中不存在该函数,所以试试这个:

 SELECT 
        a.Articlenumber AS Article,        
        PrO.Number AS Order, 
        PrD.Number AS Delivery,    
        p.Amount AS Orderamount,    
        SUM(d.Amount) AS Deliveryamount,    
        (p.Amount - Deliveryamount) AS OpenAmount    
    FROM Article AS a    
        INNER JOIN ProcessesDetails AS p ON (a.ArticleNumber = p.Article)   
        INNER JOIN Processes as PrO ON PrO.Number = p.Order
        LEFT JOIN Processes as PrD ON PrO.Nummer = PrD.ForwardedFrom    
        INNER JOIN ProcessesDetails as d ON PrD.Number = d.Order
    WHERE (a.Categorie = 'tennis') 
    AND LEFT(p.Order, 3) = 'OR-'
    AND PrO.Forwarded like '%Delivery:%'
    AND LEFT(PrD.Number,3) = 'DE-' 
    AND d.Article = p.Article
    GROUP BY(Article)

尝试使用AND PrO.Forwarded like '%Delivery:%'而不是AND TEXTSEARCH('Delivery:' IN PrO.Forwarded)

于 2013-02-26T21:26:32.347 回答
1
SELECT 
    a.Articlenumber AS Article,        
    PrO.Number AS Order, 
    PrD.Number AS Delivery,    
    p.Amount AS Orderamount,    
    SUM(d.Amount) AS Deliveryamount,    
    (p.Amount - Deliveryamount) AS OpenAmount    
FROM Article AS a    
    INNER JOIN ProcessesDetails AS p ON (a.ArticleNumber = p.Article)   
    INNER JOIN Processes as PrO ON PrO.Number = p.Order
    LEFT JOIN Processes as PrD ON PrO.Nummer = PrD.ForwardedFrom AND LEFT(PrD.Number,3) = 'DE-'
    INNER JOIN ProcessesDetails as d ON PrD.Number = d.Order
WHERE (a.Categorie = 'tennis') 
AND LEFT(p.Order, 3) = 'OR-'
AND TEXTSEARCH('Delivery:' IN PrO.Forwarded)AND d.Article = p.Article
GROUP BY(Article)

这将解决您的左连接问题。

于 2013-02-26T21:42:16.843 回答
0

第一个问题可能与这两行有关:

LEFT JOIN Processes as PrD ON PrO.Nummer = PrD.ForwardedFrom

AND LEFT(PrD.Number,3) = 'DE-' 

因为如果没有交付,左连接实际上具有 NULL 值,因此 PrD.Number 无法以“DE-”开头。where 子句可能像:

AND (PrD.Number is null OR LEFT(PrD.Number,3) = 'DE-')

可能会解决您问题的第一部分。不确定问题的第二部分。我不知道你在哪里进行数字比较。

于 2013-02-26T21:29:13.650 回答