2013-10-31 67 views
1
SELECT image_id, 
     CASE image_id 
     WHEN 0 THEN (SELECT image_path 
         FROM images 
         WHERE image_id IN (SELECT default_image 
              FROM registration 
              WHERE reg_id IN (SELECT favorite_id 
                  FROM @favDT))) 
     ELSE (SELECT image_path 
       FROM images 
       WHERE image_id = b.image_id 
         AND active = 1) 
     END AS image_path 
FROM buddies b 
WHERE reg_id IN (SELECT favorite_id 
        FROM @favDT) 

我現在面臨一個問題,這個查詢,因爲select favorite_id from @favDT回報許多喜愛ID的情況下,但我需要得到在選擇的部分FROM buddies b where reg_id in (select favorite_id from @favDT)和同樣喜愛ID使用它case when 0多的情況下選擇在存儲過程中

任何幫助嗎?

回答

1

也許這樣的:

SELECT image_id, 
     CASE image_id 
     WHEN 0 THEN (SELECT image_path 
         FROM images 
         WHERE image_id IN (SELECT default_image 
              FROM registration 
              WHERE reg_id IN (SELECT favorite_id 
                  FROM @favDT 
                  WHERE favorite_id = b.reg_id 
                  ) 
             ) 
        ) 
     ELSE (SELECT image_path 
       FROM images 
       WHERE image_id = b.image_id 
         AND active = 1) 
     END AS image_path 
FROM buddies b 
WHERE reg_id IN (SELECT favorite_id 
        FROM @favDT) 
+0

我怎樣才能增加的情況下,如果沒有行選擇? – User7291

+0

從圖像中選擇沒有行?以及在這種情況下你想要哪個image_path? – mucio

+0

在這種情況下,在buddies中沒有行選擇需要相同的操作,當image_id爲0時 – User7291

0

你需要編寫條件如下圖所示:

SELECT image_id, 
     CASE image_id 
     WHEN 0 THEN (SELECT image_path 
         FROM images 
         WHERE image_id IN (SELECT default_image 
              FROM registration 
              WHERE reg_id = b.reg_id)) 
     ELSE (SELECT image_path 
       FROM images 
       WHERE image_id = b.image_id 
         AND active = 1) 
     END AS image_path 
FROM buddies b 
WHERE reg_id IN (SELECT favorite_id 
        FROM @favDT) 
0

試試這個沒有消協:

SELECT b.image_id, 
     im.image_path as image_path 
FROM buddies b 
JOIN @favDT fdt on b.reg_id=fdt.favorite_id 
JOIN images im on (b.image_id<>0 and b.image_id=im.image_id and active=1) 
        OR 
        (b.image_id=0 and r.default_image=im.image_id) 

LEFT JOIN registration r on (b.image_id=0 and fdt.favorite_id=r.reg_id) 
相關問題