空行仅在PDO上消失

时间:2019-03-15 09:48:33

标签: php sql-server pdo

寻求优化SQL请求以加快20秒的加载页面。我们必须减少所拥有的请求数量。这些是当前的请求:

                SELECT
                    P.[Starting Date] as current_promo
                FROM [Sales Price] P
                WHERE P.[Item No_] = :id
                  AND P.[Ending Date] >= getdate()
                  AND P.[Starting Date] <= getdate()
              ORDER BY P.[Starting Date] ASC;

                SELECT
                    P.[Starting Date] as current_promo
                FROM [Purchase Line Discount] P
                WHERE P.[Item No_] = :id
                  AND (P.[Line Discount_] > 0 OR P.[Line Discount_2] > 0)
                  AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
                  AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
              ORDER BY P.[Starting Date] ASC;

                SELECT
                    P.[Lot Quantity] as lot,
                    P.[Free Quantity] as free
                FROM [Purchase Price] P
                WHERE P.[Item No_] = :id
                  AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
                  AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
              ORDER BY P.[Ending Date] ASC;

                SELECT
                    P.[Starting Date] as next_promo
                FROM [Purchase Price] P
                WHERE P.[Item No_] = :id
                  AND P.[Minimum Quantity] = 1
                  AND P.[Ending Date] > P.[Starting Date]
                  AND P.[Starting Date] > getdate()
              ORDER BY P.[Starting Date] ASC;

我们只需要每个查询的第一个结果。好的,我可以做一个TOP 1,没问题。但是我仍然需要将这4个请求串联起来。这是我的草稿之一:

SELECT
       sales_promo,
       purchase_promo,
       lot, free,
       next_promo
FROM
     (
       SELECT TOP 1 P.[Starting Date] as sales_promo
       FROM [Purchase Line Discount] P
       WHERE P.[Item No_] = :id
         AND (P.[Line Discount _] > 0 OR P.[Line Discount _ 2] > 0)
         AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
         AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
       ORDER BY P.[Starting Date] ASC
       ) salesPromo FULL OUTER JOIN
     (
       SELECT TOP 1 P.[Starting Date] as purchase_promo
       FROM [Purchase Line Discount] P
       WHERE P.[Item No_] = :id
         AND (P.[Line Discount _] > 0 OR P.[Line Discount _ 2] > 0)
                 AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
         AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
       ORDER BY P.[Starting Date] ASC
     ) purchasePromo ON 1=1 FULL OUTER JOIN
     (
       SELECT TOP 1 P.[Lot Quantity] as lot, P.[Free Quantity] as free
       FROM [Purchase Price] P
       WHERE P.[Item No_] = :id
         AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
         AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
       ORDER BY P.[Ending Date] ASC
     ) lotandfee ON 1=1 FULL OUTER JOIN
     (
       SELECT TOP 1 P.[Starting Date] as next_promo
       FROM [Purchase Price] P
       WHERE P.[Item No_] = :id
         AND P.[Minimum Quantity] = 1
         AND P.[Ending Date] > P.[Starting Date]
         AND P.[Starting Date] > getdate()
       ORDER BY P.[Starting Date] ASC
     ) nextPromo ON 1=1

完美,可以正常工作!

问题

在某些子请求中,我得到0个结果。当我直接在数据库上执行此请求时,没有问题,null没有任何问题,而子请求却没有结果,而第二子请求也没有结果。但是,当我使用PDO进行此操作时,每当有一个结果为0的子请求时,我最终都会导致整行消失!

这是另一个草稿,给出相同的结果:

SELECT
       sales_promo
       purchase_promo,
       lot, free,
       next_promo
FROM
     (
       SELECT q.* FROM (SELECT NULL) AS d (dummy) LEFT JOIN (
         SELECT TOP 1 P.[Starting Date] as sales_promo
         FROM [Purchase Line Discount] P
         WHERE P.[Item No_] = :id
           AND (P.[Line Discount _] > 0 OR P.[Line Discount _ 2] > 0)
           AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
           AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
         ORDER BY P.[Starting Date] ASC
       ) AS q ON 1=1
       ) salesPromo,
     (
       SELECT q.* FROM (SELECT NULL) AS d (dummy) LEFT JOIN (
       SELECT TOP 1 P.[Starting Date] as purchase_promo
       FROM [Purchase Line Discount] P
       WHERE P.[Item No_] = :id
         AND (P.[Line Discount _] > 0 OR P.[Line Discount _ 2] > 0)
                 AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
         AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
       ORDER BY P.[Starting Date] ASC
       ) AS q ON 1=1
     ) purchasePromo,
     (
       SELECT q.* FROM (SELECT NULL) AS d (dummy) LEFT JOIN (
       SELECT TOP 1 P.[Lot Quantity] as lot, P.[Free Quantity] as free
       FROM [Purchase Price] P
       WHERE P.[Item No_] = :id
         AND (P.[Ending Date] >= getdate() OR P.[Ending Date] = '1753-01-01 00:00:00.000')
         AND (P.[Starting Date] <= getdate() OR P.[Starting Date] = '1753-01-01 00:00:00.000')
       ORDER BY P.[Ending Date] ASC
       ) AS q ON 1=1
     ) lotandfee,
     (
       SELECT q.* FROM (SELECT NULL) AS d (dummy) LEFT JOIN (
       SELECT TOP 1 P.[Starting Date] as next_promo
       FROM [Purchase Price] P
       WHERE P.[Item No_] = :id
         AND P.[Minimum Quantity] = 1
         AND P.[Ending Date] > P.[Starting Date]
         AND P.[Starting Date] > getdate()
       ORDER BY P.[Starting Date] ASC
       ) AS q ON 1=1
     ) nextPromo

0 个答案:

没有答案