简体   繁体   English

当数据存在时,内连接返回空集

[英]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?从技术上讲,我不应该拿回 1 行吗?

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".

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM