0

因此,我有两个查询 - 一个当前创建报告,另一个查询是我编写的用于将更多数据附加到报告中。

我想合并新查询,以便当我在 ASP 中调用它时数据显示在同一数据集中。

我在放置新的 where 子句时遇到了麻烦,以便它只给我想要的东西。这些查询可以自行工作,但我似乎无法弄清楚如何在不给我大量我不想要的额外数据的情况下将它们组合在一起。

这是原始查询:

SELECT 
transactions.date,
transactions.Pcode,
transactions.L,
transactions.iL,  
transactions.q,
transactions.mTo
FROM
transactions, 
productlist
WHERE
transactions.Pcode=productlist.Pcode 
AND productlist.client=transactions.client 
AND (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage') 
AND transactions.client=232 AND ( tophysical = 'Place1' OR fromphysical = 'Place1' ) 
AND transactions.Date>= '2012-10-01' 
AND transactions.Date <= '2012-10-31 11:59:59 PM ' 
ORDER BY
mTo,
transactions.date,
transactions.pCode

我要集成的查询是:

SELECT 
transactions.date,
transactions.pcode,
transactions.L,
transactions.iL,  
transactions.qty,
transactions.mTo,
FROM
transactions, 
productlist,
productarchive,
Shipping
WHERE 
transactions.pcode=productlist.pcode 
AND productlist.client=transactions.client 
AND transactions.mFrom='NotAccepted'
AND ProductArchive.TrID=Transactions.ID
AND ProductArchive.StID = Shipping.ID
AND Shipping.SID LIKE 'BAG%'
AND transactions.Date>= '2012-10-01' 
AND transactions.Date <= '2012-10-31 11:59:59 PM ' 

我想出的最终查询给了我比我想要的多万条记录。

SELECT 
transactions.date,
transactions.pCode,
transactions.L,
transactions.iL,  
transactions.q,
transactions.mTo
FROM
transactions, 
productlist, 
shipping,
ProductArchive
WHERE 
transactions.pCode=productlist.pCode 
AND productlist.client=transactions.client 
AND (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage' OR transactions.mFrom='NotAccepted') 
AND transactions.client=232 
AND 
( tophysical = 'Place1' OR fromphysical = 'Place1' OR 
    ( ProductArchive.TransID=Transactions.ID
        AND ProductArchive.StID = Shipping.ID
        AND Shipping.SID LIKE 'BAG%') 
)
AND transactions.Date>= '2012-10-01' 
AND transactions.Date <= '2012-10-31 11:59:59 PM ' 
ORDER BY
transactions.mTo,
transactions.date,
transactions.pCode

感谢您的时间、精力和帮助!深表感谢:)

4

2 回答 2

0
SELECT 
transactions.date,
transactions.Pcode,
transactions.L,
transactions.iL,  
transactions.q,
transactions.mTo
FROM
transactions, 
productlist
WHERE
transactions.Pcode=productlist.Pcode 
AND productlist.client=transactions.client 
AND (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage') 
AND transactions.client=232 AND ( tophysical = 'Place1' OR fromphysical = 'Place1' ) 
AND transactions.Date>= '2012-10-01' 
AND transactions.Date < '2012-11-01' 
ORDER BY
mTo,
transactions.date,
transactions.pCode

union all

SELECT 
transactions.date,
transactions.pcode,
transactions.L,
transactions.iL,  
transactions.qty,
transactions.mTo,
FROM
transactions, 
productlist,
productarchive,
Shipping
WHERE 
transactions.pcode=productlist.pcode 
AND productlist.client=transactions.client 
AND transactions.mFrom='NotAccepted'
AND ProductArchive.TrID=Transactions.ID
AND ProductArchive.StID = Shipping.ID
AND Shipping.SID LIKE 'BAG%'
AND transactions.Date>= '2012-10-01' 
AND transactions.Date < '2012-11-01' 
于 2012-10-31T19:23:33.510 回答
0

根据我的评论,它可能更容易使用UNION ALL,我还更新为使用 ANSI JOIN 语法:

SELECT transactions.date,
    transactions.Pcode,
    transactions.L,
    transactions.iL,  
    transactions.q,
    transactions.mTo,
    'query 1' source
FROM transactions
INNER JOIN productlist
    ON transactions.Pcode=productlist.Pcode 
    AND productlist.client=transactions.client 
WHERE (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage') 
    AND transactions.client=232 AND ( tophysical = 'Place1' OR fromphysical = 'Place1' ) 
    AND transactions.Date>= '2012-10-01' 
    AND transactions.Date <= '2012-10-31 11:59:59 PM ' 
UNION ALL
SELECT transactions.date,
    transactions.pcode,
    transactions.L,
    transactions.iL,  
    transactions.qty,
    transactions.mTo,
    'query 2' source
FROM transactions
INNER JOIN productlist
    ON transactions.pcode=productlist.pcode 
    AND productlist.client=transactions.client 
INNER JOIN productarchive
    ON ProductArchive.TrID=Transactions.ID
INNER JOIN Shipping
    ON ProductArchive.StID = Shipping.ID
WHERE transactions.mFrom='NotAccepted'
    AND Shipping.SID LIKE 'BAG%'
    AND transactions.Date>= '2012-10-01' 
    AND transactions.Date <= '2012-10-31 11:59:59 PM ' 
于 2012-10-31T19:25:28.570 回答