简体   繁体   English

SQL 如何在 WHERE 子句中添加 3 个或更多条件?

[英]SQL how do I add 3 or more conditions into the WHERE clause?

I thought by simply adding AND to the WHERE clause, I can infinitely add conditions as long as the selected columns match the WHERE clause.我想只要将 AND 添加到 WHERE 子句,我就可以无限添加条件,只要所选列与 WHERE 子句匹配即可。 However, the following query:但是,以下查询:

No matching signature for operator AND for argument types: BOOL, BOOL, STRING.参数类型的运算符 AND 没有匹配的签名:BOOL、BOOL、STRING。 Supported signature: BOOL AND ([BOOL, ...]) at [8:3]支持的签名:[8:3] 处的 BOOL AND ([BOOL, ...])

SELECT
  DISTINCT state,
  country,
  city
FROM
  customer_data.customer_address
WHERE
  TRIM(state) = 'OH' 
  AND LENGTH(country) > 2
  AND SUBSTR(city,1,5)

If I were to remove the last line, it shows no error and I can run the query but why would adding another condition return such error?如果我要删除最后一行,它不会显示任何错误并且我可以运行查询但是为什么添加另一个条件会返回这样的错误?

Yes, you can combine so many logic operations with the WHERE clause but, Your SUBSTR is returning a string so you have to compare it with something, SQL Server in my case tries to act like `Bool so it will show the error message meaning the result is not boolean, but non-boolean:是的,您可以将如此多的逻辑操作与WHERE子句组合在一起,但是,您的SUBSTR正在返回一个字符串,因此您必须将它与某些东西进行比较,SQL 在我的例子中,服务器试图像 `Bool 这样它会显示错误消息,意思是结果不是 boolean,而是非布尔值:

Msg 4145, Level 15, State 1, Line 13 An expression of non-boolean type specified in a context where a condition is expected, near ')'.消息 4145,级别 15,State 1,第 13 行在“)”附近预期条件的上下文中指定的非布尔类型的表达式。

to fix it, you have to compare it with something, let's say the return string is equal to 'abcde'要修复它,您必须将它与某些东西进行比较,假设返回字符串等于“abcde”

here is my query using SQL Server:这是我使用 SQL 服务器的查询:

SELECT
    DISTINCT state,
    country,
    city
FROM
    tbl_Test2
WHERE
    TRIM(state) = 'OH'
    AND  LEN(country) > 2
    AND SUBSTRING(city,1,5) = 'abcde'

我的本地结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 BigQuery SQL 中添加 arrays? - How do I add arrays in BigQuery SQL? 如何调用 where 子句中的前一行? - How do you call previous row in a where clause? 如何创建一个 SQL 查询,该查询返回超过一周的列过滤条目? - How do I create a SQL Query that returns a column filtering entries that are more than a week old? 如何在 bash 中为我的 jq 响应添加条件? - How can I add conditions on my jq response in bash? SQL Having/Where 子句比较当前/另一个表中的 MAX - SQL Having/Where clause to compare MAX from current/another table 在 Data Studio SQL 数据源中使用带有“WHERE”子句的过滤来引用 function - Usings filtering with "WHERE" clause in Data Studio SQL datasource to reference a function 如何添加小吃店? - How do I add snackbar? 如何在 VSCode 的“在 Cloud Run 模拟器上运行/调试”构建设置中添加 SQL 连接 - How do I add an SQL connection in VSCode's "Run/Debug on Cloud Run Emulator" Build Settings 如果有声明,我如何向我的 Lambda 添加更多政策? - How can i add more policies to my Lambda if there is a Statement? 如何在 Flutter Firestore 中使用“where”过滤 CollectionGroup 查询 - How do I filter CollectionGroup Query with 'where' in Flutter Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM