0

MySQLテーブルに対する次のクエリは、deliveryorderテーブルに対応するエントリを持つpurchaseorderテーブルからの行を返します。対応する行がdeliveryorderテーブルに存在しない場合でも、purchaseorderテーブルから行を取得するようにこのクエリを作成するにはどうすればよいですか?ユーザーがSQLテーブルのCREATEステートメントを見たい場合は、それらを投稿できますが、質問が大きくなりすぎるため、今は投稿しません。

SELECT
    `purchaseorder`.`id` AS `po_id`,
    `purchaseorder`.`order_quantity` AS `po_order_quantity`,
    `purchaseorder`.`applicable_approved_unit_rate` AS `po_unit_rate`,
    `purchaseorder`.`applicable_sales_tax_rate` AS `po_tax_rate`,
    `purchaseorder`.`order_date` AS `po_order_date`,
    `purchaseorder`.`remarks` AS `po_remarks`,
    `purchaseorder`.`is_open` AS `po_is_open`,
    `purchaseorder`.`is_active` AS `po_is_active`,
    `purchaseorder`.`approved_rate_id` AS `po_app_rate_id`,

    `supplier`.`name` AS `sup_name`,

    SUM(`deliveryorder`.`quantity`) AS `total_ordered`

    FROM `purchaseorder`
    LEFT JOIN `deliveryorder` ON (`deliveryorder`.`purchase_order_id` = `purchaseorder`.`id`)
    INNER JOIN `approvedrate` ON (`purchaseorder`.`approved_rate_id` = `approvedrate`.`id`)
    INNER JOIN `supplier` ON (`approvedrate`.`supplier_id` = `supplier`.`id`)

    WHERE (
            `purchaseorder`.`is_active` = 1
            AND `purchaseorder`.`is_open` = 1
            AND `deliveryorder`.`is_active` = 1
            AND `approvedrate`.`material_id` = 2
           )
    HAVING `purchaseorder`.`order_quantity` >= `total_ordered` + 1
4

2 に答える 2

0

集計関数はありますが、奇妙なGROUP BY句はありませんが、とにかく-このようなものですか?おっと-編集...

SELECT po.id po_id
     , po.order_quantity po_order_quantity
     , po.applicable_approved_unit_rate po_unit_rate
     , po.applicable_sales_tax_rate po_tax_rate
     , po.order_date po_order_date
     , po.remarks po_remarks
     , po.is_open po_is_open
     , po.is_active po_is_active
     , po.approved_rate_id po_app_rate_id
     , s.name sup_name
     , SUM(do.quantity) total_ordered
  FROM purchaseorder po
  LEFT 
  JOIN deliveryorder do 
    ON do.purchase_order_id = po.  
   AND do.is_active = 1
  LEFT
  JOIN approvedrate ar
    ON ar.id = po.approved_rate_id 
   AND ar.material_id = 2
  LEFT
  JOIN supplier s
    ON s.id = ar.supplier_id 
 WHERE po.is_active = 1
   AND po.is_open = 1
HAVING po.order_quantity >= total_ordered + 1
于 2013-03-25T10:05:05.847 に答える
0

1つのクエリで目的の結果をすべて取得する方法を理解できませんでしたが、要件を満たすために次の2つのクエリを使用することになりました。

最初のクエリ

        SELECT
            pot.`id` AS `po_id`,
            pot.`order_quantity` AS `po_order_quantity`,
            pot.`applicable_approved_unit_rate` AS `po_unit_rate`,
            pot.`applicable_sales_tax_rate` AS `po_tax_rate`,
            pot.`is_open` AS `po_is_open`,
            pot.`is_active` AS `po_is_active`,

            st.`id` AS `sup_id`,
            st.`name` AS `sup_name`,

            SUM(dot.`quantity`) AS `total_ordered`
        FROM `purchaseorder` pot
            INNER JOIN `deliveryorder` dot ON (dot.`purchase_order_id` = pot.`id`)
            INNER JOIN `approvedrate` art ON (pot.`approved_rate_id` = art.`id`)
            INNER JOIN `supplier` st ON (art.`supplier_id` = st.`id`)
        WHERE (
                pot.`is_active` = 1
                AND pot.`is_open` = 1
                AND art.`material_id` = @materialid
                AND art.`in_effect` = 1
                AND art.`is_active` = 1
                AND dot.`is_active` = 1
                AND st.`is_active` = 1
               )
        HAVING pot.`order_quantity` >= `total_ordered` + @materialquantity

2番目のクエリ

        SELECT
            pot.`id` AS `po_id`,
            pot.`order_quantity` AS `po_order_quantity`,
            pot.`applicable_approved_unit_rate` AS `po_unit_rate`,
            pot.`applicable_sales_tax_rate` AS `po_tax_rate`,
            pot.`is_open` AS `po_is_open`,
            pot.`is_active` AS `po_is_active`,

            st.`id` AS `sup_id`,
            st.`name` AS `sup_name`,

            0 AS `total_ordered`
        FROM `purchaseorder` pot
            INNER JOIN `approvedrate` art ON (pot.`approved_rate_id` = art.`id`)
            INNER JOIN `supplier` st ON (art.`supplier_id` = st.`id`)
        WHERE (
                pot.`is_active` = 1
                AND pot.`is_open` = 1
                AND art.`material_id` = @materialid
                AND art.`in_effect` = 1
                AND art.`is_active` = 1
                AND st.`is_active` = 1
                AND pot.`order_quantity` >= @materialquantity
                AND pot.`id` NOT IN
                    (
                      SELECT dot.`purchase_order_id`
                      FROM `deliveryorder` dot
                      WHERE dot.is_active = 1
                    )
               )
于 2013-03-26T04:23:47.673 に答える