簡體   English   中英

如何使用 SQL 中的 OR/AND 子句解決此問題

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

我有一組專利,我有標題和摘要,我想以這樣一種方式進行搜索,即他們的最終搜索算法要求出現關鍵字“軟件”,而不是關鍵字“芯片”、“半導體”、出現“總線”、“電路”或“電路”。 我這樣做了:

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

但我收到此錯誤An expression of non-boolean type specified in a context where a condition is expected, near 'or'. 我應該怎么辦?

這是錯誤的:

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

這是正確的:

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

樣本數據會很有幫助,所以我可以測試我的代碼。

為了幫助將來維護此代碼的人,您最好限定您的列名。 當我無法分辨每一列來自哪個表時,很難知道發生了什么。

這相當冗長,但可能更容易理解和維護。 這行得通嗎?

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
  )

基於https://stackoverflow.com/a/1127100/9937026

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM