SQL:SELECT 其中一列包含 'x' 而结果不是 "NULL"

SQL: SELECT where one of many columns contains 'x' and result is not "NULL"

我有一段代码,我意识到它可能效率很低,但我不确定如何改进它。

基本上,我有一个这样的数据库table:

Example DB table

A-G 列中的任何一个或多个列可能与我的搜索查询匹配。如果是这样,我想从该行查询 VALUE 。不过,我需要 VALUE 不等于 NULL,所以如果是这种情况,它应该继续寻找。如果我的查询是 abc,我想获得 correct.

下面是我当前的代码,使用名为 db 的数据库和 table table.

cur=db.cursor()

data="123"
fields_to_check=["A","B","C","D","E","F","G"]

for field in fields_to_check:

    "SELECT Value FROM table WHERE {}='{}'".format(field,data)
    query=cur.fetchone()
    if query and query !="NULL":
        break

db.close()

我认为执行 8 次查询的效率可能非常低。

cur=db.cursor()

data="123"
fields_to_check=["A","B","C","D","E","F","G"]
sub_query = ""

for field in fields_to_check:
    sub_query = sub_query + "or {}='{}' ".format(field,data)

if sub_query:
    query = "SELECT Value FROM table WHERE ("+ str(sub_query[2:]) +") and value IS NOT NULL;"

if query:
    cur.execute(query)
    rows = cur.fetchall()
    if rows:
      for row in rows:
        print(row)