【问题标题】:MYSQL Distict value from many tables with smallerst valueMYSQL 来自许多具有最小值的表的不同值
【发布时间】:2018-08-12 23:16:19
【问题描述】:

我有三个产品表:

table1:producer_code、价格、公司

table2:producer_code、价格、公司

table3:producer_code、价格、公司

现在我想列出产品(与所有表格不同),我可以这样做:

select distinct producer_code, price, company
from (
    select producer_code, price, company FROM table1
    union all
    select producer_code, price, company FROM table2
    union
    select producer_code, price, company FROM table3
) tmp_table

但是如何只列出最便宜的产品?示例:如果同一产品将出现在三个表中,我希望它只听一次,但来自最便宜的来源(公司列)。 我应该使用联合还是加入?

【问题讨论】:

    标签: mysql sql database select


    【解决方案1】:

    我认为 OP 需要各种公司中最便宜的价格,而且这些价格也存储在多个表中。

    Ex 架构:

    create table table1(producer_code varchar(50), price decimal(18,2), company varchar(100));
    create table table2(producer_code varchar(50), price decimal(18,2), company varchar(100));
    create table table3(producer_code varchar(50), price decimal(18,2), company varchar(100));
    
    insert into table1
    select 'ITEM001', 45, 'Company A' 
    UNION ALL
    select 'ITEM002', 200, 'Company B'
    UNION ALL
    select 'ITEM003', 150, 'Company B';
    
    insert into table2
    select 'ITEM001', 50, 'Company B'
    UNION ALL
    select 'ITEM002', 300, 'Company C'
    UNION ALL
    select 'ITEM003', 55, 'Company D';
    
    insert into table3
    select 'ITEM001', 190, 'Company F'
    UNION ALL
    select 'ITEM002', 78, 'Company G'
    UNION ALL
    select 'ITEM003', 100, 'Company A';
    

    我们需要在这里加入 2 个集合。一个是实际的组合集合 A,另一个是包含 producer_code 和最低价格的集合。

    select 
    A.* from 
    (
    SELECT * FROM table1
    UNION 
    SELECT * FROM table2
    UNION 
    SELECT * FROM table3
    )A
    INNER JOIN
    (
        SELECT producer_code, min(price) AS price FROM (
        SELECT * FROM table1
        UNION ALL
        SELECT * FROM table2
        UNION ALL
        SELECT * FROM table3
        )B
        GROUP BY producer_code
    )CHEAP ON A.producer_code = CHEAP.producer_code AND A.price = CHEAP.price;
    

    结果:

    +---------------+---------+-------------+
    | producer_code |  price  |   company   |
    +---------------+---------+-------------+
    | 'ITEM001'     | '45.00' | 'Company A' |
    | 'ITEM003'     | '55.00' | 'Company D' |
    | 'ITEM002'     | '78.00' | 'Company G' |
    +---------------+---------+-------------+
    

    【讨论】:

    • 谢谢。这是感兴趣的解决方案,但其他答案也有效且更简单
    【解决方案2】:

    您可以通过以下方式获取它 usinf Min:

    select producer_code, Min(price), company
    from (
        select producer_code, price, company FROM table1
        union all
        select producer_code, price, company FROM table2
        union
        select producer_code, price, company FROM table3
    ) tmp_table
    group by tmp_table.producer_code, tmp_table.company
    

    【讨论】:

      【解决方案3】:

      使用min可以得到最小值。

      我想你可能会使用UNION ALL

      select producer_code, MIN(price), company
      from (
          select producer_code, price, company FROM table1
          union all
          select producer_code, price, company FROM table2
          union all
          select producer_code, price, company FROM table3
      ) tmp_table
      GROUP BY producer_code,company
      

      【讨论】:

        【解决方案4】:
        select distinct producer_code, min(price), company
        from (
            select producer_code, price, company FROM table1
            union all
            select producer_code, price, company FROM table2
            union all
            select producer_code, price, company FROM table3
        ) tmp_table
        group by producer_code,company
        

        【讨论】:

        • 这里不需要区分,因为你已经有一个 group by。
        猜你喜欢
        • 2014-07-01
        • 2020-10-13
        • 2012-11-23
        • 2012-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多