开发者

SQL Join query help

开发者 https://www.devze.com 2022-12-28 22:14 出处:网络
I have 2 tables A and B with the following columns Table A - id,bId,aName,aVal Table B - id,bName where A.bId is the same as B.id. I want a result set from a query to get

I have 2 tables A and B with the following columns Table A - id,bId,aName,aVal Table B - id,bName

where A.bId is the same as B.id. I want a result set from a query to get

A.id, A.aName, B.bName where A.bId=B.id OR A.id, A.aName, "" when A.bId=0.

In both cases, only those records should be considered where A.aVal LIKE "aVal"

Can someone please help me with the query? I can use left join but how do I get the blank string if bId=0 and B.bName otherwise开发者_开发知识库?

Thanks


SELECT  a.id, a.aname, COALESCE(b.bname, '')
FROM    a
LEFT JOIN
        b
ON      b.id = NULLIF(a.bld, 0)
WHERE   a.aval LIKE 'aval'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号