Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding NOT NULL constraint to one SELECT Column

I am trying to creat a view of database.

I would need to select only the values from Comments.FK_CommentID which are NOT NULL but I can’t seem to know how to solve it.

Here’s my view:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CREATE VIEW Comments_of_comments AS SELECT User.Username AS User, Comments.content AS Comment, Comments.FK_CommentID AS 'Commented on' FROM Comments INNER JOIN User ON Comments.UserID = User.UserID ORDER BY User ASC;

Here’s the output as of now:

User Comment Commented on
Anthony Hello 3001
Brian I’m glad
Charlie I’m sad 3000
Dylan All ok 3004
Ernie Let’s go

But I would like it to be WHERE Comments.FK_CommentID IS NOT NULL. (Commented on -column)

User Comment Commented on
Anthony Hello 3001
Charlie I’m sad 3000
Dylan All ok 3004

>Solution :

If you just want to view your data this way, then add a WHERE clause filtering off the null commented on records:

CREATE VIEW Comments_of_comments AS
SELECT u.Username AS User,
       c.content AS Comment,
       c.FK_CommentID AS "Commented on"
FROM Comments c
INNER JOIN User u ON c.UserID = u.UserID
WHERE c.FK_CommentID IS NOT NULL
ORDER BY User;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading