我有一个SQL查询,我必须查询三个表(其中一些表两次),但我得不到我需要的结果:
我需要在'文章'中搜索搜索字符串('网球'),并为这些文章查找第二个表'orderdetails'。 点击量显示了订购量。
现在我需要检查这些物品是否已经送达。 因此,我查看'orderdetails'的表'订单',如果这些订单有'交付',请查看同一个表。 'orders'adn'delivery'有一个显示它是什么类型的字段。
因此,我必须检查“交付”是否由“订单”转发。
接下来检查这个“交付”是否包含“文章”并总结交付的文章。
如果交付的文章数量低于我想要显示此记录的订购文章数量。
到目前为止,除了这些项目之外它还有效: - [已解决]感谢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
答案 0 :(得分:1)
我不知道where where条件中的这一行是什么:
AND TEXTSEARCH('Delivery:' IN PrO.Forwarded)
我不认为mysql中存在textsearch
函数,所以试试这个:
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)
答案 1 :(得分: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)
这将解决您的左连接问题。
答案 2 :(得分: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-')
可以解决问题的第一部分。不确定问题的第二部分。我无法弄清楚你在做数字比较的地方。