简体   繁体   English

内部联接失败-不返回任何数据

[英]Inner join failure - returns no data

I am encountering some problems in inner joining a table and a view . 我在内部联接表和视图时遇到了一些问题。 The view is pageviewforum with a column forumn_no varchar2 and a column of totalcount which is the count of total pageviews in each forum. 该视图是pageviewforum其中具有forumn_no varchar2列和totalcount列, totalcount是每个论坛中总浏览量的计数。

Another table is forum : forum_no char(6), forumname varchar2(50) 另一个表是“ forumforum_no char(6), forumname varchar2(50)

I want to output the totalcount with the forum_no and its forum name but it doesn't work. 我要输出的totalcountforum_no及其论坛的名称,但它不工作。

select forum.forumname, totalcount from pageviewforum 
inner join forum on pageviewforum.forum_no= forum.forum_no; 

The above query returns the error message of no data. 上面的查询返回没有数据的错误消息。 I tried to see what comes out of 我试图看看会发生什么

select forum.forumname, totalcount from pageviewforum 
inner join forum on pageviewforum.forum_no > forum.forum_no;

and data comes out. 数据就出来了。 I tried to use like but it says no data as well. 我尝试使用like,但它也没有数据。 What can I do? 我能做什么?

Since you are joining a varchar2 column of indeterminate size with a char(6) column, it may be that you are running into differences because of that. 由于您varchar2大小不确定的varchar2列与char(6)列连接在一起,因此可能会遇到差异。 If those columns hold a number, why not use a numeric type (like INTEGER) to hold the values? 如果这些列包含数字,为什么不使用数字类型(如INTEGER)来保存值? It will be more reliable in the long run. 从长远来看,它将更加可靠。 Generally, joining columns work most reliably when they're of the same type. 通常,连接列属于同一类型时,它们最可靠地工作。 When they're of different types, you have to worry about whether the two types are compatible enough and whether they compare sensibly. 当它们属于不同类型时,您必须担心这两种类型是否足够兼容以及它们是否比较明智。

Hypothesis : In particular, a CHAR(6) column is blank-padded to full length; 假设 :特别是,将CHAR(6)列空白填充为全长; unless the DBMS strips those trailing blanks off, you may never get a CHAR(n) and a VARCHAR(n) to compare equal unless all n characters are in use. 除非DBMS消除了这些尾随的空白,否则除非使用了所有n个字符,否则您将永远无法获得CHAR(n)和VARCHAR(n)进行比较。 The > comparison works because the blanks matter for equality, but the greater than or less than can still come up with an answer. >比较有效,因为空格对平等很重要,但是大于或小于仍然可以得出答案。

Observations: 观察:

  • Column types in a database do matter. 数据库中的列类型很重要。
  • Consistency is important. 一致性很重要。

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

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