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
任何帮助?
答案 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)
答案 1 :(得分: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)
答案 2 :(得分: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)