简体   繁体   English

MySQL从表1中选择所有行并从表2中选择匹配的行或返回null

[英]Mysql select all rows from table1 and matching rows from table2 or return null

I need to get all products columns plus the column unit_price from price_list where all the conditions match. 我需要从所有条件匹配的price_list中获取所有产品列以及unit_price列。

The problem is my select statement gets only the first 2 products, id: 1, 2, I need to get product with id 3,4 and so on, but with unit_price null because there is no match in price_list for that quantity. 问题是我的select语句仅获取ID分别为1、2、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

I would like my select statement to get me something like this: 我希望我的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

Current select gets only id 1 and 2. 当前选择仅获得ID 1和2。

Your possible solution is 您可能的解决方案是

    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

It will be better if you have more data with sqlfiddle link. 如果您使用sqlfiddle链接获得更多数据,那就更好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM