返回左表中的顶行数,同时返回右连接表中的所有行

时间:2014-03-25 19:42:40

标签: sql vbscript

我试图限制返回的数量或订单,但不是总行数。我想从orders表中选择前15行,但返回连接表中的所有相关行。

下面的语句总共返回15行,而不是前15个订单中的所有数据。感谢您的帮助。

sSQL2 = "SELECT TOP 15 orders.oID
    , orders.oName
    , cartoptions.coCartOption
    , cart.cProdID
    , cartoptions.coCartID
    , cart.cProdName
    , cart.cQuantity
FROM orders
LEFT JOIN cart
    ON orders.oid = cart.corderid
LEFT JOIN cartoptions
    ON cart.cid = cartoptions.cocartid
WHERE orders.oComplete = 1
    AND (
        cartoptions.coOptGroup = 'Logo Style'
        AND cartoptions.coCartOption = 'KGM'
        )
ORDER BY orders.oDate"

2 个答案:

答案 0 :(得分:0)

快速,肮脏,未经测试,但这可能有效:

sSQL2 = "SELECT orderList.oName
              , cartoptions.coCartOption
              , cart.cProdID
              , cartoptions.coCartID
              , cart.cProdName
             , cart.cQuantity
           from (Select TOP 15 oID
                             , oName
                  from orders
                  order by oDate desc) orderList

LEFT JOIN cart
    ON orderList.oid = cart.corderid
LEFT JOIN cartoptions
    ON cart.cid = cartoptions.cocartid
WHERE orderList.oComplete = 1
    AND (
        cartoptions.coOptGroup = 'Logo Style'
        AND cartoptions.coCartOption = 'KGM'
        )"

答案 1 :(得分:0)

试试这个......

sSQL2 = "SELECT orders.oID
    , orders.oName
    , cartoptions.coCartOption
    , cart.cProdID
    , cartoptions.coCartID
    , cart.cProdName
    , cart.cQuantity
FROM (
    SELECT TOP 15 *         
    FROM orders
    Where oid is not null
    )orders
LEFT JOIN cart
    ON orders.oid = cart.corderid
LEFT JOIN cartoptions
    ON cart.cid = cartoptions.cocartid
WHERE orders.oComplete = 1
    AND (
        cartoptions.coOptGroup = 'Logo Style'
        AND cartoptions.coCartOption = 'KGM'
        )"