Mysql选择table1中的所有行并匹配table2中的行或返回null

时间:2016-06-18 09:12:31

标签: mysql

我需要获取所有产品列以及所有条件匹配的price_list列unit_price。

问题是我的select语句只获得前两个产品,id:1,2,我需要获得id为3,4的产品,依此类推,但是unit_price为null,因为price_list中没有匹配的数量

products
--------------------------------------------
id  product_code    purchase_type   quantity
1   106             new             5
2   107             renew           26
3   107             renew           101
4   107             renew           150


price_list
----------------------------------------------------------------------
id  product_code    purchase_type   minimum     maximum     unit_price
1   106             new             1           25          20
2   106             new             26          50          16
3   106             new             51          100         14



SELECT
    `products`.`id` AS `product_id`,
    `products`.`product_code` AS `product_product_code`,
    `products`.`purchase_type` AS `product_purchase_type`,
    `products`.`update_type` AS `product_update_type`,
    `products`.`quantity` AS `product_quantity`,
    `price_list`.`product_code` AS `price_list_product_code`,
    `price_list`.`purchase_type` AS `price_list_purchase_type`,
    `price_list`.`update_type` AS `price_list_update_type`,
    `price_list`.`minimum` AS `price_list_minimum`,
    `price_list`.`maximum` AS `price_list_maximum`,
    `price_list`.`unit_price` AS `price_list_unit_price`
FROM
    `products`
INNER JOIN `price_list` ON `products`.`product_code` = `price_list`.`product_code`
WHERE
    `products`.`product_code` = price_list.product_code
AND `products`.`purchase_type` = price_list.purchase_type
AND `products`.`update_type` = price_list.update_type
AND `products`.`quantity` >= price_list.minimum
AND `products`.`quantity` <= price_list.maximum
ORDER BY
    `products`.`id` ASC

我希望我的select语句能让我这样:

    Select Result:
    -------------------------------------------------------
    id  product_code    purchase_type   quantity   unit_price
    1   106             new             5          20
    2   107             renew           26         16
    3   107             renew           101        null
    4   107             renew           150        null

当前选择只获得id 1和2。

1 个答案:

答案 0 :(得分:2)

您可能的解决方案是

    SELECT
    `products`.`id` AS `product_id`,
    `products`.`product_code` AS `product_product_code`,
    `products`.`purchase_type` AS `product_purchase_type`,
    `products`.`quantity` AS `product_quantity`,
    `price_list`.`product_code` AS `price_list_product_code`,
    `price_list`.`purchase_type` AS `price_list_purchase_type`,
    `price_list`.`minimum` AS `price_list_minimum`,
    `price_list`.`maximum` AS `price_list_maximum`,
    `price_list`.`unit_price` AS `price_list_unit_price`
FROM
    `products`
LEFT JOIN `price_list` ON `products`.`product_code` = `price_list`.`product_code`
AND `products`.`purchase_type` = price_list.purchase_type
AND `products`.`quantity` >= price_list.minimum
AND `products`.`quantity` <= price_list.maximum
ORDER BY
    `products`.`id` ASC

如果你有更多的数据与sqlfiddle链接会更好。