我有以下查询:
SELECT favorite_id,MO,name,b.image_id,image_path FROM buddies b,images i where reg_id=@regID and b.image_id=i.image_id
在从表格图像获取image_path之前,应该测试好友中的字段b.image_id ...如果image_id为0,则必须从表格注册中获取image_id并从表格图像中获取image_path,如果image_id不同于0我直接从表格图像中获取image_id ...所以当从表伙伴中选择favorite_id和image_id时,我需要在每个行上循环并测试image_id
任何人都可以帮助我吗?
答案 0 :(得分:2)
SELECT favorite_id,MO,name,
b.image_id,
image_path = case when b.image_id ='0' then i1.image_path else i.image_path end
FROM buddies b, registration r
left join images i on b.image_id=i.image_id
left join images i1 on r.image_id=i1.image_id
where b.reg_id=@regID and r.reg_id=@regID
答案 1 :(得分:0)
SELECT favorite_id,MO,name,b.image_id,
case b.image_id when 0 then i.image_path else b.image_path end as image_path
FROM buddies b,images i
where reg_id=@regID and b.image_id=i.image_id
像这样的Smth ......
如果您需要额外的条件选择 - 那么它就在这里:SELECT CASE WHEN THEN (SELECT)