2014-08-28 26 views
2

我使用这个查询试图检索4个画廊:SQL查询随着UNION返回一个小于预期

(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) 
+0

谢谢你的编辑迈克尔! – 2014-08-28 15:07:43

+2

为什么不使用IN(567,484,...,545)?并且如果你有欺骗,那么你会看清楚吗 – Bulat 2014-08-28 15:08:45

+0

我是一名SQL新手,所以我需要更多解释你的意思。 – 2014-08-28 15:09:26

回答

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')