title: SQL query to match a list of values with a list of fields in any order without repetition
body: <p>I recently had to wrote a query to filter some specific data that looked like the following:</p>
  
  <p>Let's suppose that I have 3 distinct values that I want to search in 3 different fields of one of my tables on my database, they must be searched in all possible orders without repetition.</p>
  
  <p>Here is an example (to make it easy to understand, I will use named queries notation to show where the values must be placed):</p>
  
  <p>val1 = "a", val2 = "b", val3 = "c"</p>
  
  <p>This is the query I've generated:</p>
  
  <pre><code>SELECT * FROM table WHERE
  (fieldA = :val1 AND fieldB = :val2 AND fieldC = :val3) OR
  (fieldA = :val1 AND fieldB = :val3 AND fieldC = :val2) OR
  (fieldA = :val2 AND fieldB = :val1 AND fieldC = :val3) OR
  (fieldA = :val2 AND fieldB = :val3 AND fieldC = :val1) OR
  (fieldA = :val3 AND fieldB = :val1 AND fieldC = :val2) OR
  (fieldA = :val3 AND fieldB = :val2 AND fieldC = :val1)
  </code></pre>
  
  <p>What I had to do is generate a query that simulates a permutation without repetition. Is there a better way to do this type of query?</p>
  
  <p>This is OK for 3x3 but if I need to do the same with something bigger like 9x9 then generating the query will be a huge mess.</p>
  
  <p>I'm using MariaDB, but I'm okay accepting answers that can run on PostgreSQL.
  (I want to learn if there is a smart way of writing this type of queries without "brute force")</p>