连接两个包含许多列的表但是在sql中提供了许多重复的结果

时间:2015-10-17 08:38:49

标签: sql

我正在使用此查询来加入表格请给我正确的解决方案 有600行,它给出了1938个结果。

select 
    b.FValue as title, k.FValue as subtitle, c.FValue as isbn, 
    d.FValue as issn, E.FValue as Author, f.FValue as roleofauthor, 
    g.FValue as editor, 
    h.FValue as publisher, i.FValue as publisherPlace, 
    j.FValue as pyhsicaldescription, m.FValue as Subject , 
    n.FValue as Note , l.p852 as AccessionNo, 
    l.DateofAcq as AccessionDate,
    l.a852 as location,l.Budget as Budget, l.k852 as ClassNo, 
    l.m852 as bookNo, l.Price as Price, l.t852 as CopyNo,
    l.Supplier as supplier, l.b852 as collectionttype,
    l.Material As materials, l.status 
from 
    Biblidetails b
left join 
    Biblidetails c on b.RecID = c.RecID and c.Tag = 020 and c.SbFld = 'a' 
left join 
    Biblidetails d on b.RecID = d.RecID and d.tag = 022 and d.SbFld = 'a' 
left join 
    Biblidetails e on b.RecID = e.RecID and e.Tag = 100 and e.SbFld = 'a' 
left join 
    Biblidetails f on b.RecID = f.RecID and f.Tag = 100 and f.SbFld = 'e' 
left join 
    Biblidetails g on b.RecID = g.RecID and g.Tag = 250 and g.SbFld = 'a'
left join 
    Biblidetails h on b.RecID = h.RecID and h.Tag = 260 and h.SbFld = 'b'
left join 
    Biblidetails i on b.RecID = i.RecID and i.Tag = 260 and i.SbFld = 'a'
left join 
    Biblidetails j on b.RecID = j.RecID and j.Tag = 300 and j.SbFld = 'a' 
left join 
    Biblidetails k on b.RecID = k.RecID and k.Tag = 245 and k.SbFld = 'b' 
left join 
    Biblidetails m on b.RecID = m.RecID and m.Tag like '6%' and m.SbFld = 'a'
left join 
    Biblidetails n on b.RecID = n.RecID and n.tag like '5%' and n.SbFld = 'a'
left join 
    location l on b.RecID = l.RecID where b.Tag = 245 and b.SbFld = 'a'

1 个答案:

答案 0 :(得分:0)

您必须在RecID上执行分组操作。尝试

select b.FValue as title, k.FValue as subtitle, c.FValue as isbn, d.FValue as issn,E.FValue as Author, f.FValue as roleofauthor, g.FValue as editor, 
h.FValue as publisher, i.FValue as publisherPlace, j.FValue as pyhsicaldescription, m.FValue as Subject , 
n.FValue as Note , l.p852 as AccessionNo, l.DateofAcq as AccessionDate,
l.a852 as location,l.Budget as Budget, l.k852 as ClassNo, l.m852 as bookNo ,l.Price as Price,l.t852 as CopyNo,l.Supplier as supplier ,
l.b852 as collectionttype,l.Material As materials,l.status from Biblidetails 
b left join Biblidetails c on b.RecID = c.RecID and c.Tag = 020 and c.SbFld = 'a' 
left join Biblidetails d on b.RecID = d.RecID and d.tag = 022 and d.SbFld = 'a' 
left join Biblidetails e on b.RecID = e.RecID and e.Tag = 100 and e.SbFld = 'a' 
left join Biblidetails f on b.RecID = f.RecID and f.Tag = 100 and f.SbFld = 'e' 
left join Biblidetails g on b.RecID = g.RecID and g.Tag = 250 and g.SbFld = 'a'
left join Biblidetails h on b.RecID = h.RecID and h.Tag = 260 and h.SbFld = 'b'
left join Biblidetails i on b.RecID = i.RecID and i.Tag = 260 and i.SbFld = 'a'
left join Biblidetails j on b.RecID = j.RecID and j.Tag = 300 and j.SbFld = 'a' 
left join Biblidetails k on b.RecID = k.RecID and k.Tag = 245 and k.SbFld = 'b' 
left join Biblidetails m on b.RecID = m.RecID and m.Tag like '6%' and m.SbFld = 'a'
left join Biblidetails n on b.RecID = n.RecID and n.tag like '5%' and n.SbFld = 'a'
left join location l 
on b.RecID = l.RecID where b.Tag = 245 and b.SbFld = 'a'
GROUP BY b.RecID;