Rick
Rick

Reputation: 79

SQL Server : changing a temp column value

I have been trying to create something of which I am not even sure if it's even possible, I have looked for similar things on Stackoverflow and Google, but with no results.

What am I trying to do?

I got two tables, one is called Book and the other one is called UserBook. The Book table stores different information about a book and the UserBook has a columns like UserId and ISBN and DateCreated.

Currently I am trying to write a SQL query which returns all the books, and adds a temp column which is called IsVisible (default value is set to True) but when the ISBN number and UserId are found in the UserBook it will change the temp value of IsVisible to false in the final result.

My current query so far is this:

SELECT 
    Book.ISBN, Book.Title, Book.ImagePath, Book.Summary, Book.AmountAvailable, 'True' AS IsVisible 
FROM 
    Book;

Is what I am trying to do even possible?

Upvotes: 1

Views: 429

Answers (1)

logixologist
logixologist

Reputation: 3834

I think you are trying to do something like this:

SELECT b.ISBN, 
b.Title, 
b.ImagePath, 
b.Summary, 
b.AmountAvailable,
CASE when ub.isbn is NOT NULL then 'False' else 'True' END AS IsVisible FROM Book b
LEFT OUTER JOIN USERBook ub on ub.isbn = b.isbn

Upvotes: 2

Related Questions