UNION返回的SQL查询少于预期

时间:2014-08-28 15:00:12

标签: sql sql-server tsql

我正在使用此查询试图检索4个图库:

(SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
 WHERE a.pid = b.previewpic and galleryid = '567' limit 1) 
UNION ALL 
   (SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
    WHERE a.pid = b.previewpic and galleryid = '541' limit 1) 
UNION ALL 
   (SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
    WHERE a.pid = b.previewpic and galleryid = '484' limit 1) 
UNION ALL 
   (SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
    WHERE a.pid = b.previewpic and galleryid = '545' limit 1)

然而,它返回3.如果我复制了最后一个查询,它返回4.任何想法为什么?

这很有效,但很麻烦:

(SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
 WHERE a.pid = b.previewpic and galleryid = '567' limit 1) 
 UNION ALL 
      (SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
       WHERE a.pid = b.previewpic and galleryid = '541' limit 1) 
 UNION ALL (SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
      WHERE a.pid = b.previewpic and galleryid = '484' limit 1) 
 UNION ALL 
     (SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
      WHERE a.pid = b.previewpic and galleryid = '545' limit 1) #
 UNION ALL 
      (SELECT * FROM edgewe_ngg_pictures a, edgewe_ngg_gallery b 
      WHERE a.pid = b.previewpic and galleryid = '545' limit 1)

1 个答案:

答案 0 :(得分:1)

尝试:

SELECT * 
FROM 
edgewe_ngg_pictures p inner join
edgewe_ngg_gallery g on
p.pid = g.previewpic
WHERE  
galleryid in( '567','541','484','545' )