在where/having子句之外使用布尔表达式。

huangapple go评论92阅读模式
英文:

Using boolean expression outside of a where/having clause

问题

SQL Server returns the result rowlett@bikes.shop for the expression email=rowlett@bikes.shop because it is treating the expression as a comparison, not a mathematical operation. In SQL, when you use the = operator in a SELECT statement, it is used for comparison, not for mathematical equality.

So, when you write email=rowlett@bikes.shop, SQL Server interprets it as "Is the email column equal to the string 'rowlett@bikes.shop'?" Since it's a comparison, it returns the value of the email column for each row where the condition is true, which in this case is just 'rowlett@bikes.shop'.

SQL Server doesn't convert it to something like 1 because it doesn't have a boolean type, and it treats the result as a string literal that matches the condition you specified.

英文:

I have the following data:

  1. store_id store_name phone email street city state zip_code
  2. 1 Santa Cruz Bikes (831) 476-4321 santacruz@bikes.shop 3700 Portola Drive Santa Cruz CA 95060
  3. 2 Baldwin Bikes (516) 379-8888 baldwin@bikes.shop 4200 Chestnut Lane Baldwin NY 11432
  4. 3 Rowlett Bikes (972) 530-5555 rowlett@bikes.shop 8000 Fairway Avenue Rowlett TX 75088

If I write a query such as:

  1. select * from sales.stores where email='rowlett@bikes.shop'
  2. -- store_id store_name phone email street city state zip_code
  3. -- 3 Rowlett Bikes (972) 530-5555 rowlett@bikes.shop 8000 Fairway Avenue Rowlett TX 75088

I get the expected result with one row. However, if I use the exact same expression in the select list, I get what seems like gibberish:

  1. SELECT email='rowlett@bikes.shop' from sales.stores where email='rowlett@bikes.shop'
  2. -- email
  3. -- rowlett@bikes.shop

Why does SQL Server return the result rowlett@bikes.shop for the expression email=rowlett@bikes.shop ? I understand it doesn't have the boolean type, but I'd think it would be converted to something like 1. Why does that occur?

答案1

得分: 2

根据@Martin Smith在评论中指出的,无论您使用:

  1. SELECT email = 'rowlett@bikes.shop' ...

还是

  1. SELECT 'rowlett@bikes.shop' AS email ...

您都会得到相同的查询结果 - 即,在您的查询结果集中将会有一个名为'email'的列,每一行的值都是'rowlett@bikes.shop'。

  1. |email |column2|column3|
  2. -------------------- ------- -------
  3. |'rowlett@bikes.shop'|value1 |another value1|
  4. |'rowlett@bikes.shop'|value2 |another value2|
  5. 等等。

如果您想进一步回答您的问题(假设您实际上希望在查询结果中返回一个真/假值),那么您可以使用CASE表达式来实现 - 例如:

  1. SELECT
  2. CONVERT(bit,
  3. CASE
  4. WHEN email = 'rowlett@bikes.shop'
  5. THEN 1
  6. ELSE 0
  7. END
  8. )
  9. FROM sales.stores;

并扩展到查看一些潜在的结果:

  1. SELECT
  2. email,
  3. CONVERT(bit,
  4. CASE
  5. WHEN email = 'rowlett@bikes.shop'
  6. THEN 1
  7. ELSE 0
  8. END
  9. ) AS EmailCheck
  10. FROM sales.stores;

您将会看到类似以下的结果:

  1. |email |EmailCheck |
  2. |'rowlett@bikes.shop' |1 |
  3. |'someone@xyz.com' |0 |
  4. |'someoneelse@xyz.com'|0 |
英文:

As @Martin Smith pointed out in the comments, whether you use:

  1. SELECT email = 'rowlett@bikes.shop' ...

or

  1. SELECT 'rowlett@bikes.shop' AS email ...

you'll get the same query result - that is, you'll get a column named 'email' in your query resultset, with 'rowlett@bikes.shop' as the value for each row.

  1. |email |column2|column3|
  2. -------------------- ------- -------
  3. |'rowlett@bikes.shop'|value1 |another value1|
  4. |'rowlett@bikes.shop'|value2 |another value2|
  5. etc.

To go one step further beyond your question (making the assumption that you actually do want to have a true/false value returned in your query results), then you can use a CASE expression to achieve that - eg.

  1. SELECT
  2. CONVERT(bit,
  3. CASE
  4. WHEN email = 'rowlett@bikes.shop'
  5. THEN 1
  6. ELSE 0
  7. END
  8. )
  9. FROM sales.stores;

And expanding that to see some potential results:

  1. SELECT
  2. email,
  3. CONVERT(bit,
  4. CASE
  5. WHEN email = 'rowlett@bikes.shop'
  6. THEN 1
  7. ELSE 0
  8. END
  9. ) AS EmailCheck
  10. FROM sales.stores;

you'd see something like:

  1. |email |EmailCheck |
  2. |'rowlett@bikes.shop' |1 |
  3. |'someone@xyz.com' |0 |
  4. |'someoneelse@xyz.com'|0 |

huangapple
  • 本文由 发表于 2023年6月29日 12:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定