select table1.单号,table1.发货省份,table1.发货城市,table1.收货省份,table1.收货城市 from table1,table2 where table1.发货省份=table.2发货省份 and table1.发货城市=table2.发货城市 and table1.收货省份=table2.收货省份 and table1.收货城市=table2.收货城市 表1有60多万条数据,表2 有6百多条,最后才查到7000条,distinct一下单号就剩6000多了,感觉应该不止的。这写法有问题吗?
本身多表的查询就是先做迪卡尔集,然后根据where条件进行筛选,但是非主键的关联where筛选是不可能排除重复值的。
from table1,table2
where table1.发货省份=table.2发货省份
and table1.发货城市=table2.发货城市
and table1.收货省份=table2.收货省份
and table1.收货城市=table2.收货城市
表1有60多万条数据,表2 有6百多条,最后才查到7000条,distinct一下单号就剩6000多了,感觉应该不止的。这写法有问题吗?
怕是对笛卡尔积有什么误解。
而且oracle不会这么来做连接,如果任何表连接先做笛卡尔积然后再来过滤,数据量稍微大点,库可以分分钟搞趴下。
如果表1只有这样的问题,那么连接条件可以用substr截取表2的收货省份字段的前两位再与表1的收货省份字段做比较