Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple WHERE conditions in one SQL statement

I am having trouble to use many WHERE conditions, as I do need to combine 8 Where conditions in a single statement.

My current SQL:

SELECT * 
FROM Table 
WHERE ID = ?

I want something like this:

SELECT * 
FROM Table 
WHERE ID = ?, WHERE COL2 = ?, WHERE COL3 = ?, ... WHERE COL8 = ?

How can I achieve this? I'm stuck.

like image 750
Compiler v2 Avatar asked Apr 20 '26 16:04

Compiler v2


1 Answers

You cannot have multiple WHERE at the same level of SQL query. You need to use AND:

SELECT * FROM Table 
WHERE ID = ? 
  AND COL2 = ? 
  AND COL3 = ?
  -- ... 
  AND COL8 = ?

or use nesting:

SELECT *
FROM (SELECT *
      FROM Table
      WHERE ID = ?) s
WHERE COL1 = ?
...

It is actually an interesting question. For instance KQL(Kusto query language) allows to chain multiple WHERE:

 Tab
 | where col = ?
 | where col2 = ?

Sample:

let t1 = datatable(key:long, value:string)  
[1, "a",  
2, "b",
3, "c"];

t1 
| where key in (1,2)
| where value == "b"
like image 190
Lukasz Szozda Avatar answered Apr 22 '26 05:04

Lukasz Szozda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!