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

标签 mysql

我有两个表,suppliers 和 products_suppliers。

供应商:

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")

谢谢

最佳答案

如果我准确理解你的问题并使用你想要的输出作为引用,你想实现这个:

对于每个产品 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_products_supplier) 进行内部连接后,从供应商表 (tbl_suppliers) 中选择必要的字段。它根据供应商的优先级和产品 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'选择语句使用。然后按 ProductID 对行进行分组。

关于Mysql:如果join中存在特定值,则获取其他列的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47601539/

相关文章:

php - 检查数组以查找公共(public)数据

export - 将数据和结构从一个实时数据库复制到测试数据库而不丢失数据

mysql asp.net 未知失败

Php 简单错误

python - 如何在web.py/python中检查sql查询结果是否为空

php - 全文搜索非常慢

php - 计算 PDO 准备语句中的行数

mysql - 在 MySql 中,如何将数据插入到引用其他三个表的表中,然后选择该数据?

PHP 表单输入不会给我文本字段.. [http-auth login for security]

MySql - 从 2 个表中选择 *,但在结果集中添加表名前缀?