2014-08-28 32 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')