繁体   English   中英

在postgres中嵌套选择

[英]Nested select in postgres

我有一个bit(10)字符串,我想执行AND操作,然后得到数字1,然后将其除以位字符串的长度(在这种情况下为10)。 这是查询:

select  a.doc
       , a.sentenceid
       , b.doc
       , b.sentenceid
       , LENGTH( REPLACE( CAST( select a.tokenizedsentence & b.tokenizedsentence AS TEXT ), '0', '')) / LENGTH(a.tokenizedsentence)
from 
  nlpdata a, nlpdata b
where 
  a.sentenceid < b.sentenceid;

单独使用时,两个查询都有效,但是如何组合它们呢?

select a.sentenceid
      , b.sentenceid
      , a.tokenizedsentence & b.tokenizedsentence 
from nlpdata a, nlpdata b;

select length( replace( cast( tokenizedsentence AS TEXT ), '0', ''))
from nlpdata;

您无需在CAST Select 删除Select以修复错误

SELECT a.doc,
       a.sentenceid,
       b.doc,
       b.sentenceid,
       Length(Replace(Cast(a.tokenizedsentence & b.tokenizedsentence AS TEXT), '0', '')) / Length(a.tokenizedsentence)
FROM   nlpdata a 
INNER JOIN nlpdata b
        ON a.sentenceid < b.sentenceid; 

附带说明一下,请始终使用INNER JOIN来联接两个表,而不要使用旧式逗号分隔的联接。

在性能方面没有任何区别。 INNER JOIN是更具有可读性然后comma separated join其中两个过滤器连接条件将出现在Where子句。 Inner Join您可以保持Inner Join条件为ON子句,并将过滤器移至Where子句

暂无
暂无

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

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