MySQL选择查询 - 添加两行并将其显示为一行?

时间:2012-01-24 08:07:33

标签: mysql select

SELECT        receive.RECEIPT_ID, receive.RECEIPT_DATE, supplier.NAME, item.NAME AS Expr2, receive_item.PRICE, receive_item.QTY, (receive_item.QTY - IFNULL(delivery_item.QTY, 0)) AS 'REMAINED QTY.'
FROM            receive_item INNER JOIN
                         receive ON receive_item.RECEIPT_ID = receive.RECEIPT_ID INNER JOIN
                         supplier ON receive.SUPPLIER_ID = supplier.ID INNER JOIN
                         item ON receive_item.ITEM_ID = item.ID INNER JOIN
                         po ON receive.PO_NUMBER = po.PO_ID LEFT OUTER JOIN
                         delivery_item ON receive_item.ITEM_ID = delivery_item.ITEM_ID AND receive_item.RECEIPT_ID = delivery_item.RECEIVE_ID

下表来自上面非常复杂的选择查询

ID      RECEIPT_DATE  SUPP_NAME      ITEM                      PRICE  QTY  QTY_REMAINED 

1   2012-01-22  MANCO   COPPER PIPE 42MM                       245.00   1   0
2   2012-01-22  MANCO   COPPER PIPE 15MM                       350.00   1   0
3   2012-01-22  MANCO   "RUBBER INSULLATION HOSE 35mm x 1"""   478.00   1   0
4   2012-01-22  MANCO   DEFROST HEATER 9mmx215cm 220Vx800W     5000.00  10  1
5   2012-01-22  MANCO   DEFROST HEATER 9mmx215cm 220Vx800W     5000.00  10  9
6   2012-01-22  MANCO   COPPER ELBOW 90  42MM                  100.00   1   0
7   2012-01-22  MANCO   COPPER ELBOW 90  12MM                  200.00   3   0
8   2012-01-22  MANCO   COPPER ELBOW 45  28MM                  3000.00  4   0
9   2012-01-22  MANCO   COPPER PIPE 15MM                       4.00 3   2   0

我需要一个结合ID 4和5的选择查询,因此表格将如下所示;

ID      RECEIPT_DATE  SUPP_NAME      ITEM                      PRICE  QTY  QTY_REMAINED 

1   2012-01-22  MANCO   COPPER PIPE 42MM                       245.00   1   0
2   2012-01-22  MANCO   COPPER PIPE 15MM                       350.00   1   0
3   2012-01-22  MANCO   "RUBBER INSULLATION HOSE 35mm x 1"""   478.00   1   0
4   2012-01-22  MANCO   DEFROST HEATER 9mmx215cm 220Vx800W     5000.00  10  0
5   2012-01-22  MANCO   COPPER ELBOW 90  42MM                  100.00   1   0
6   2012-01-22  MANCO   COPPER ELBOW 90  12MM                  200.00   3   0
7   2012-01-22  MANCO   COPPER ELBOW 45  28MM                  3000.00  4   0
8   2012-01-22  MANCO   COPPER PIPE 15MM                       4.00 3   2   0

因此,在所需的表格中,QTY_REMAINED的{​​{1}}显示为ID 4而不是1和9

我认为查询必须执行类似0的操作才能生成值10 - ( 10- 1 ) + ( 10 - 9)

有可能吗?或者我应该在程序中这样做?

1 个答案:

答案 0 :(得分:0)

我假设您想要获得剩余的物品总数。 您不应该左键加入delivery-item,而是应该按照ITEM_IDRECEIVE_ID对其进行左键加入,每个组的总和为QTY

SELECT        receive.RECEIPT_ID, receive.RECEIPT_DATE, supplier.NAME,
              item.NAME AS Expr2, receive_item.PRICE, receive_item.QTY,  
              (receive_item.QTY - IFNULL(delivery_group.QTY, 0)) AS 'REMAINED QTY.'
FROM            
    receive_item 
INNER JOIN
    receive ON receive_item.RECEIPT_ID = receive.RECEIPT_ID 
INNER  JOIN
    supplier ON receive.SUPPLIER_ID = supplier.ID 
INNER JOIN
    item ON receive_item.ITEM_ID = item.ID 
INNER JOIN
    po ON receive.PO_NUMBER = po.PO_ID 
LEFT OUTER JOIN
  ( SELECT ITEM_ID, RECEIVE_ID, SUM(QTY) AS QTY
    FROM delivery_item
    GROUP BY ITEM_ID, RECEIVE_ID ) delivery_group
ON receive_item.ITEM_ID = delivery_group.ITEM_ID 
AND receive_item.RECEIPT_ID = delivery_group.RECEIVE_ID