SQL查询问题,请大家指教.(50分)

  • 主题发起人 主题发起人 Clearboy
  • 开始时间 开始时间
C

Clearboy

Unregistered / Unconfirmed
GUEST, unregistred user!
A <br>&nbsp; fstockid fitemid fbatchno num demo<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp; A/C &nbsp; &nbsp; 1<br><br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; &nbsp; A/c &nbsp; &nbsp; 2 &nbsp; <br><br>B fstockid fitemid fbatchno unum demo<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp;A/C &nbsp; &nbsp; &nbsp;T &nbsp; &nbsp;2<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp;A/C &nbsp; &nbsp; &nbsp;U &nbsp; &nbsp;3<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; &nbsp;A/B &nbsp; &nbsp; &nbsp;Y &nbsp; &nbsp; 3<br><br>result:<br>&nbsp; fstockid fitemid fbatchno &nbsp;num &nbsp; &nbsp; unum demo<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; A/C &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; T &nbsp; &nbsp;2<br><br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; A/C &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp; Y &nbsp; &nbsp;3 <br><br><br>就是A表中的所有记录,不过,A表中没有字段是从B中取的,取B中的第一条。
 
A中的数据量1w多,<br>B中的数据量120w多,用子查询出问题了,超时.<br><br>想用left join 请大家帮忙,用left join的话,出现重复的记录
 
用 Select distinct 去掉重复的
 
关键没有这么简单,现在B表中<br>fstockid fitemid fbatchno unum demo<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp;A/C &nbsp; &nbsp; &nbsp;T &nbsp; &nbsp;2<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp;A/C &nbsp; &nbsp; &nbsp;U &nbsp; &nbsp;3<br>&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; &nbsp;A/B &nbsp; &nbsp; &nbsp;Y &nbsp; &nbsp; 3<br>假如按select distinct fstockid,fitemid,fbatchno,unum from B<br>1 2 A/c t 2<br>1 3 a/b y 3这种结果<br>但我需要的是这样的结果
 
一、如果是超时,那么请加索引。。<br>二、兄弟,我认为你表有问题,如果是主从关系,那么在A表中没有的数据,则在B表中也不应该出现,这样就违反了约束。<br>三、left join左连接,可以这样<br>SELECT distinct b.fstockid, b.fitemid, b.fbatchno, a.num, b.nnum, b.demo<br>from b<br>left join a on a.fstockid = b.stockif and a.fitemid = b.fitemid
 
是不是要这个样子的<br>select distinct A.fstockid,A.fitemid,A.fbatchno,B.unum,B.demo from A inner Join b on (A.fstockid=B.fstockid and A.fitemid=B.fitemid and A.fbatchno=B.fbatchno)
 
表是固定的,不是我创建的,二次开发嘛<br>后来是这样解决的,先把数据处理成没有重复的(临时表),然后再left join<br>结了,给你们加分了,
 
后退
顶部