title: SQL: SELECT where one of many columns contains 'x' and result is not "NULL"
body: <p>I have a piece of code that I realized is probably quite inefficient, though I'm not sure how to improve it.</p>
  
  <p>Basically, I have a database table like this:</p>
  
  <p><a href="https://i.stack.imgur.com/DXMD0.png" rel="nofollow noreferrer">Example DB table</a></p>
  
  <p>Any or several of columns A-G might match my search query. If that is the case, I want to query <code>VALUE</code> from that row. I need <code>VALUE</code> not to equal <code>NULL</code> though, so if that's the case, it should keep looking. If my query were <code>abc</code>, I'd want to obtain <code>correct</code>.</p>
  
  <p>Below is my current code, using a database named <code>db</code> with a table <code>table</code>.</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>I think that the fact that this performs 8 queries is likely very inefficient.</p>