I am trying to accomplish this on SQL Server. The simplest table structure with data is shown below.
Table: Blog
BlogID | Title
-------+--------
1 | FirstBlog
23 | Pizza
Table: User
UserID | Name
-------+-----------
123 | james
444 | John
Table: UserBlogMapping
UserBlogMappingID | BlogID | UserID
------------------+--------+------
1 | 1 | 123
I want to get FormID
and UserBlogMappingID
in one SQL query. If provided UserID
is not in the mapping table, return ZERO otherwise return the valid userBlogMappingID
. I am trying to run the below query but its not correct.
SELECT
B.BlogID,
BUM.BlogUserMappingID
FROM
Blog AS B
LEFT JOIN BlogUserMapping AS BUM ON B.BlogID = BUM.BlogID
WHERE
(B.BlogID = 23) -- it exists in the table
AND BUM.userID = 444 -- it is NOT in the mmaping table but I want a ZERO return in such case
Assumption:
We can assume that the UserID
provided in the WHERE clause is always a valid UserID
and is present in the User
table.