MYSQL如何使用条件语句对字段进行分组?

时间:2014-12-02 17:46:22

标签: php mysql

我有以下SQL,我怎么能只为Category = X分组By ItemID,并让所有其他的一样。

例如,如果我有JrnlHdr.Reference ='15027'&& LineItem.Category =激光并且匹配LineItem.ItemID = X1,X2,X3我希望按ItemID对它们进行分组。

   $result = mysqli_query($con,"SELECT Customers.CustomerID,
                Customers.Customer_Bill_Name, 
                Customers.WhichShipVia, 
                group_concat(DISTINCT LineItem.ItemID) as ItemIDgroup,
                JrnlHdr.INV_POSOOrderNumber,
                JrnlHdr.CustomerInvoiceNo, 
                JrnlHdr.Comment, 
                JrnlHdr.ShipByDate, 
                JrnlHdr.GoodThruDate, 
                JrnlHdr.CustomerSONo, 
                JrnlHdr.Reference, 
                JrnlHdr.TransactionDate,
                JrnlRow.DistNumber, 
                JrnlRow.Quantity, 
                LineItem.ItemID, 
                LineItem.SalesDescription, 
                LineItem.PartNumber,
                LineItem.Category, 
                LineItem.ItemClass, 
                LineItem.ItemDescription 
    FROM   Customers
           INNER JOIN JrnlHdr
           ON Customers.CustomerRecordNumber = JrnlHdr.CustVendId
           LEFT OUTER JOIN Address
           ON Customers.CustomerRecordNumber =    Address.CustomerRecordNumber

          INNER JOIN JrnlRow
          ON JrnlHdr.PostOrder = JrnlRow.PostOrder
          LEFT OUTER JOIN BOMItems
          ON JrnlRow.ItemRecordNumber = BOMItems.AssemblyRecordNo
          INNER JOIN LineItem 
          ON JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber

       WHERE  JrnlHdr.JrnlKey_Journal = 11 
              AND LineItem.ItemID = '600331' 
              AND JrnlHdr.Reference ='15027'
              AND TransactionDate = '2014-12-1' 
              AND JrnlHdr.PostOrder = JrnlRow.PostOrder 
              AND JrnlHdr.CustVendId = Customers.CustomerRecordNumber 
              AND JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber
              AND JrnlHdr.POSOisClosed = 0 
      GROUP BY LineItem.ItemID");

0 个答案:

没有答案