Search code examples
sqldatabasepostgresqlinner-join

Inner join returns empty set when data exists


Here is my query:

SELECT * 
FROM comments 
  INNER JOIN posts ON 'comments.postPostId' = 'posts.post_id';

All comments:

blog=# select * from comments;
              comment_id              |                 uid                  |         content         |              postPostId              
--------------------------------------+--------------------------------------+-------------------------+--------------------------------------
 49104d66-aebf-48f5-b816-46d9def8edc2 | 5ef3d422-8b98-4509-a752-5ac8d1aee40d | oh man this game is lit | 1c756322-0042-4d8e-bab8-e04a9ceb981e
(1 row)

All posts:

blog=# select * from posts;
               post_id                |                 uid                  | title |   sub_title    | content |              userUserId              
--------------------------------------+--------------------------------------+-------+----------------+---------+--------------------------------------
 1c756322-0042-4d8e-bab8-e04a9ceb981e | 2e83d249-7c7c-43c0-8e3d-854ddac7c1b8 | bf1   | love this game | bla bla | 6cc903a7-11bf-43a4-a75a-3e1456404525
(1 row)

the result:

blog=# SELECT * FROM comments INNER JOIN posts ON 'comments.postPostId' = 'posts.post_id';
 comment_id | uid | content | postPostId | post_id | uid | title | sub_title | content | userUserId 
------------+-----+---------+------------+---------+-----+-------+-----------+---------+------------
(0 rows)

Why is it an empty set? Shouldn't I technically get 1 row back?

EDIT:

Without using single quotes (as suggested), I get this error:

ERROR:  column comments.postpostid does not exist
LINE 1: SELECT * FROM comments INNER JOIN posts ON comments.postPost...
                                                   ^
HINT:  Perhaps you meant to reference the column "comments.postPostId".

Solution

  • By wrapping column names in ' you're comparing those literals, which always yields false. Simply drop the ':

    SELECT * FROM comments INNER JOIN posts ON comments."postPostId" = posts.post_id;