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 セクションで選択された同じお気に入り ID を取得してFROM buddies b where reg_id in (select favorite_id from @favDT)使用する必要があるためです。case when 0

助けはありますか?

4

3 に答える 3

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) 
于 2013-10-31T09:59:31.223 に答える
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) 
于 2013-10-31T09:59:43.597 に答える
0

CASEなしでこれを試してください:

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)
于 2013-10-31T10:13:16.680 に答える