简体   繁体   English

如何使用 SQL 中的 OR/AND 子句解决此问题

[英]How can i fix this problem with OR/AND clause in SQL

I have a set of patents that I have titles and abstracts on and would like to search in such a way that their final search algorithm requires the keyword “software” to be present, and none of the keywords “chip”, “semiconductor”, “bus”, “circuit” or “circuit” to be present.我有一组专利,我有标题和摘要,我想以这样一种方式进行搜索,即他们的最终搜索算法要求出现关键字“软件”,而不是关键字“芯片”、“半导体”、出现“总线”、“电路”或“电路”。 i did This:我这样做了:

SELECT distinct 
  tls201_appln.docdb_family_id
, tls201_appln.appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, tls202_appln_title.appln_title

FROM tls201_appln
  INNER JOIN tls202_appln_title ON tls201_appln.appln_id = tls202_appln_title.appln_id 
  INNER JOIN tls203_appln_abstr ON tls201_appln.appln_id = tls203_appln_abstr.appln_id 

WHERE (appln_title like '%software%'
    or appln_abstract like '%software%')
  AND appln_title not like '%chip%'
                       or  '%semiconductor%'
                       or  '%circuity%'
                       or  '%circuitry%'
                       or  '%bus'%'
   or appln_abstract  not like  '%chip%'
                            or  '%semiconductor%'
                            or  '%circuity%'
                            or  '%circuitry%'
                            or  '%bus'%' 
    AND appln_filing_year between 2003 and 2008

but im getting this error An expression of non-boolean type specified in a context where a condition is expected, near 'or'.但我收到此错误An expression of non-boolean type specified in a context where a condition is expected, near 'or'. What should i do?我应该怎么办?

This is wrong:这是错误的:

and appln_title not like '%chip%' or  '%semiconductor%' or '%circuity%'

This is right:这是正确的:

and appln_title not like '%chip%' 
and appln_title not like '%semiconductor%' 
and appln_title not like '%circuity%'

Sample data would be helpful so I could test my code.样本数据会很有帮助,所以我可以测试我的代码。

To help whoever maintains this code in the future, you'd do well to qualify your column names.为了帮助将来维护此代码的人,您最好限定您的列名。 It's difficult to know what's going on when I can't tell which table each column comes from.当我无法分辨每一列来自哪个表时,很难知道发生了什么。

This is considerably more verbose, but possibly easier to understand and maintain.这相当冗长,但可能更容易理解和维护。 Will this work?这行得通吗?

with main as (
SELECT distinct 
  tls201_appln.docdb_family_id
, tls201_appln.appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, tls202_appln_title.appln_title
, appln_abstract

FROM         tls201_appln
  INNER JOIN tls202_appln_title ON tls201_appln.appln_id = tls202_appln_title.appln_id 
  INNER JOIN tls203_appln_abstr ON tls201_appln.appln_id = tls203_appln_abstr.appln_id 

WHERE appln_title + ' ' + appln_abstract like '%software%'
  AND appln_filing_year between 2003 and 2008
)

select docdb_family_id
, appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, appln_title
from main

except
select docdb_family_id
, appln_id
, [appln_auth]
, [appln_nr]
, [appln_kind]
, [appln_filing_date]
, [receiving_office]
, [earliest_publn_date]
, [granted]
, [nb_citing_docdb_fam]
, [nb_applicants]
, [nb_inventors]
, appln_title
from main
where exists (
    select 1
    from (
                  select '%chip%' as cond
            union select '%semiconductor%'
            union select '%circuity%'
            union select '%circuitry%'
            union select '%bus%'
         ) c
    where appln_title like c.cond
      or  appln_abstract like c.cond
  )

based on https://stackoverflow.com/a/1127100/9937026基于https://stackoverflow.com/a/1127100/9937026

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

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