简体   繁体   中英

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.

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"

We are not allowed to use the multiple where condition in the same statement. For that you need to approach by using and. For example:

SELECT * FROM Table 
where col='a' and col2='c' and col'3' 

This will work perfectly fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM