SQL中的外部选择返回不同的结果

时间:2017-08-29 21:45:36

标签: sql oracle

如果标题有误导性,我很抱歉,我不确定如何总结这一点。我试图从具有匹配列的表中收集数据,但我无法使其工作。我认为它可能是工会,所以我将整个事情包装在一个选择中,并在最后添加了位置。当我运行它们时,我得到两组完全不同的数据。这两个查询如下。

SELECT distinct "London" origin, id, name, units_sold, units_bought
    from
    ((SELECT DISTINCT * FROM a_table ) res
        JOIN (select distinct id, name from ref_table) tm  
                ON tm.id = res.id)
union all                       
SELECT distinct "New York" origin, id, name, units_sold, units_bought
    from
    ((SELECT DISTINCT * FROM b_table) res    
        JOIN (select distinct id, name from ref_table) tm  
                ON tm.id = res.id)
union all                      
SELECT distinct "Montreal" origin, id, name, units_sold, units_bought
    from
    ((SELECT DISTINCT * FROM c_table ) res
        JOIN (select distinct id, name from ref_table) tm  
                ON tm.id = res.id)

select * from
    (SELECT distinct "London" origin, id, name, units_sold, units_bought
        from
        ((SELECT DISTINCT * FROM a_table ) res
            JOIN (select distinct id, name from ref_table) tm  
                    ON tm.id = res.id)
    union all                       
    SELECT distinct "New York" origin, id, name, units_sold, units_bought
        from
        ((SELECT DISTINCT * FROM b_table) res    
            JOIN (select distinct id, name from ref_table) tm  
                    ON tm.id = res.id)
    union all                      
    SELECT distinct "Montreal" origin, id, name, units_sold, units_bought
        from
        ((SELECT DISTINCT * FROM c_table ) res
            JOIN (select distinct id, name from ref_table) tm  
                    ON tm.id = res.id))
where name='A Product Name'

在第一个查询中,如果我查看' A产品名称'的行,则units_soldunits_bought分别为16和20。在第二个查询中,他们是7和10.为什么这两个查询会返回不同的结果?难道他们不一样吗?

0 个答案:

没有答案