Mysql:如果join中存在特定值,则获取其他列的最小值

时间:2017-12-01 21:09:55

标签: mysql

我有两张桌子,供应商和产品供应商。

生产商:

ID | Name     | Priority
 1 | Orange   | 1
 2 | Vodafone | 1
 3 | Telekom  | 2
 4 | Else     | 3

在products_supplier中,我的产品数量为

ID_product | Supplierid | Quantity
    100 |        1 | 12
    100 |        2 | 14
    100 |        3 | 10
    100 |        4 | 15
    120 |        3 | 15
    120 |        4 | 10 

我希望直接在SQL中使用这样的东西

if exists "priority=1" in suppliers then return min(qty) where supplier is priority=1
else if exists "priority=2"in suppliers then return min(qty) where supplier is priority=2
else if exists "priority=3" in suppliers then return min(qty) where supplier is priority=3

输出应该类似

ID_product | Supplierid | Quantity
       100 |        1 | 12
       120 |        3 | 15  
  //even if exists supplier 4 with qty=10,   
    supplier 3 have priority because Red is in front of Blue

我做了这段代码,但我确信还有更好的空间:

SELECT pps.ID_product, min(qty), name, priority
FROM products_suppliers pps
LEFT JOIN suppliers ps on pps.supplierid=ps.id
WHERE 
  productid=100
  AND
  priority = if (exists(SELECT priority FROM suppliers WHERE priority ="1" limit 1), "1", 
        if (exists(SELECT priority FROM suppliers WHERE priority ="2" limit 1), "2", "3")

由于

2 个答案:

答案 0 :(得分:0)

我设法用Php解决了它更优雅。由于优先级是有限数(比如说小于10),我确实喜欢这个:

$priority1 = $database->query("
                                SELECT min(qty) as qty
                                FROM products_suppliers pps
                                  LEFT JOIN pin_suppliers ps ON pps.supplierid = ps.id
                                WHERE priority = '1';" )->fetch();
$priority2 = $database->.......WHERE priority = '2';" )->fetch();
 ....
$priorityn = $database->.......WHERE priority = 'n';" )->fetch();

if(!empty($priority1)) do stuff
elseif(!empty($priority2)) do stuff
  ....
elseif(!empty($priorityn)) do stuff
else do stuff

如果有人拥有MySql独有的解决方案,请发布。感谢

答案 1 :(得分:0)

如果我准确理解您的问题并使用您想要的输出作为参考,您希望实现此目的:

对于每个产品ID,请返回第一个和最高优先级供应商的产品ID,供应商ID和数量。

<强>输出:

ProductID | SupplierID | Quantity
---------------------------------
100       | 1          | 12
---------------------------------
120       | 3          | 15

<强>代码:

# Return product_id, supplier_id, and quantity based on supplier's priority (1 is highest).
SELECT  ProductID,
        SupplierID,
        Quantity
FROM
(
    SELECT      s.supplier_priority SupplierPriority,
                ps.product_id ProductID,
                s.supplier_id SupplierID,
                ps.quantity Quantity
    FROM        tbl_suppliers s
    INNER JOIN  tbl_products_supplier ps
    ON          s.supplier_id = ps.supplier_id
    GROUP BY    s.supplier_priority,
                ps.product_id
) tbl_sample
GROUP BY ProductID;

试一试: http://SQLFiddle.com/#!9/ed63b2/3

<强>要点:

sub-from语句在将其与产品供应商表(tbl_suppliers)内部连接后,从供应商表(tbl_products_supplier)中选择必要的字段。它根据供应商的优先级和产品ID对行进行分组。这些行的组织方式使其显示从最小到最大SupplierPriority的行。结果如此:

SupplierPriority | ProductID | SupplierID | Quantity
----------------------------------------------------
1                | 100       | 1          | 12
----------------------------------------------------
1                | 120       | 2          | 40
----------------------------------------------------
2                | 100       | 3          | 10
----------------------------------------------------
2                | 120       | 3          | 15
----------------------------------------------------
3                | 100       | 4          | 15
----------------------------------------------------
3                | 120       | 4          | 10
----------------------------------------------------

然后它被'main'select语句使用。然后按ProductID分组行。