简体   繁体   English

在sql中选择Query并对每行进行测试

[英]Select Query in sql with testing on each row

i have the following query: 我有以下查询:

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

the field b.image_id in buddies should b tested before getting the image_path from the table images... if the image_id is 0 i have to the the image_id from a table registration and the get the image_path from the table image and if the image_id is different than 0 i directly get the image_id from the table images... so when selecting the favorite_id and the image_id from table buddies i need to loop on this table for every row and test the 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

can anyone help me? 谁能帮我?

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 
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 like that... 这样的......

If you need additional conditional select -- then it's here: SELECT CASE WHEN THEN (SELECT) 如果你需要额外的条件选择 - 那么就在这里: SELECT CASE WHEN THEN(SELECT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM