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 部分中选择的相同的最喜欢的 idFROM 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 回答