问题描述:
一个大表A,另一个中表B(估计7000条),
两个表通过同一个字段cat_id匹配关联,
数据现状是A表可能匹配到B表多条记录,
而要的结果是A表匹配B表任意一条记录,
因此是否有办法在A表匹配到B表的一条记录后,停止搜索?
多谢指教!!!

解决方案 »

  1.   

    可以这么认为,
    查询出来的结果是A表的所有字段,再加上B表的几个字段。
    目前的问题是A表可能匹配到B表的多条记录,会有多趟B表的遍历,
    能否匹配到一条B表记录后,立即停止本趟B表的继续遍历。
      

  2.   

    加个 rownum < 2 行吗??
      

  3.   

    既然通过cat_id关联 `` 那就在关联之前先把B表的重复数据去掉再关联
     (SELECT DISTINCT CATID FROM B)DISTINCT不好用的话就用
    select * from (
    select B.*, ROW_NUMBER() OVER (PARTITION BY cat_id ORDER BY cat_id) as rn
    from B 
    ) where rn = 1; 然后再关联 这样就只有一条数据匹配了
      

  4.   

    不好意思,怪我没说清楚,
    不能去重的,因为cat_id只是关联的条件之一,
    B表不会有完全相同的重复数据,
    比如有这样的数据,
    A表:
    cat_id  title
    1512    AA
    1513    BB
    B表
    cat_id  cat1
    1512    A
    1512    B
    1512    C
    1513    B
    1513    D
    关联条件是
    A.cat_id1 = B.cat_id1 and a.title like '%'||b.cat1||'%'
    A表只要匹配到B表的一条记录时,能否停止本趟继续搜索B表?
      

  5.   

    select a.* from a,
    (
      select b.* from
       (
          select b.*,row_number()over(partition by b.cat_id order by b.cat_id) rn 
          from b
        )     
      where rn=1
    ) c
    where a.cat_id=c.cat_id and a.title like '%'||b.cat1||'%'
      

  6.   

    select a.* from a,
    (
      select b.* from
       (
          select b.*,row_number()over(partition by b.cat_id order by b.cat_id) rn 
          from b
        )     
      where rn=1
    ) c
    where a.cat_id=c.cat_id and a.title like '%'||b.cat1||'%'这样在B表的只取cat_id下的一条记录,那么A表有可能会匹配不上B表的记录啊。。
      

  7.   

    看起来只是为了验证存在性吧
    这个应该能满足
    select * from a
    where exists(select 1 from b
      where cat_id=a.cat_id
        and instr(a.title,cat1)>0)