【问题标题】:SQL WHERE statement not working 100% [closed]SQL WHERE 语句不能 100% [关闭]
【发布时间】:2011-01-07 07:53:51
【问题描述】:

我想返回 Worldwide = yesvisible = yes 的值State = Florida 但这并没有返回 Worldwideyes

的所有其他值
select  * from Table1
where (visible = 'yes' and State = 'Florida') or Worldwide= 'yes'
order by ID DESC

编辑:我的坏事

对不起,男孩/女孩,这句话确实有效!我的声明中有 Select TOP 8 * 这就是为什么它没有返回所有记录!当我拿出前 8 名时,它起作用了!我的错!

【问题讨论】:

  • 从 Table1 中执行 SELECT DISTINCT Worldwide 以确保没有其他值。
  • 您可能可以添加一些样本记录,这些记录没有被获取,但您期望它们在输出中。
  • 我看不出你的 where 子句有什么问题,我非常怀疑 SQL Server 会评估这个错误,所以正如 nonnb 所暗示的,“错误”可能在数据中。
  • select count(1) where Worldwide= 'yes' 返回什么?
  • 显示简单的示例数据,你在哪里弄错了,你期望什么。对我来说应该做你写的。

标签: asp.net sql sql-server sql-server-2005


【解决方案1】:

使用以下脚本,查询输出预期内容。

创建数据

DECLARE @Table1 TABLE (
  ID INTEGER IDENTITY(1, 1)
  , State VARCHAR(32)
  , Visible VARCHAR(32)
  , WorldWide VARCHAR(32)
)

INSERT INTO @Table1
SELECT 'Florida', 'Yes', 'Yes'  
UNION ALL SELECT 'Florida', 'Yes', 'No'  
UNION ALL SELECT 'Florida', 'No', 'Yes'  
UNION ALL SELECT 'Florida', 'No', 'No'  
UNION ALL SELECT 'Other State', 'Yes', 'Yes'  
UNION ALL SELECT 'Other State', 'Yes', 'No'  
UNION ALL SELECT 'Other State', 'No', 'Yes'  
UNION ALL SELECT 'Other State', 'No', 'No'  

选择

SELECT  *
FROM    @Table1
WHERE   (Visible = 'Yes' AND State = 'Florida') OR WorldWide = 'Yes'

输出

ID  State           Visible WorldWide
1   Florida          Yes    Yes
2   Florida          Yes    No
3   Florida          No     Yes
5   Other State      Yes    Yes
7   Other State      No     Yes

【讨论】:

    【解决方案2】:

    试试这个:

    where State = 'Florida' AND (visible = 'yes' or Worldwide= 'yes')
    

    另一个用语法覆盖所有可能情况的变体:

    where UPPER([State]) LIKE '%FLORIDA%' AND 
    ((UPPER(visible) LIKE '%YES%') OR (UPPER(Worldwide) LIKE '%YES%'))
    

    【讨论】:

    • 奇怪。检查 cmets 到您的问题。那里可能是一个答案。此外,char 类型是否区分大小写?
    【解决方案3】:

    也许你有一些空值,这可能会给你带来一些麻烦。

    select  * from Table1
    where (IsNull(visible,'') = 'yes' and IsNull(State,'') = 'Florida') 
    or IsNull(Worldwide,'')= 'yes'
    order by ID DESC
    

    还要检查排序规则是否区分大小写。排序规则可以在服务器、数据库或列级别设置,因此您需要检查以下内容:

    服务器排序

    SELECT SERVERPROPERTY('COLLATION')
    

    数据库整理

    SELECT DATABASEPROPERTYEX('DATABASENAME', 'Collation') SQLCollation;
    

    列排序规则

    Select table_name, column_name, collation_name
    From information_schema.columns
    Where table_name = @table_name
    

    【讨论】:

      【解决方案4】:

      您最多有 4 个问题

      • 区分大小写
      • 空格
      • 空值
      • 意外数据

      可以组合的变体

      --case
      (LOWER(visible) = 'yes' and LOWER(State) = 'Florida') or LOWER(Worldwide) = 'yes'
      
      --spaces
      (RTRIM(LTRIM(visible)) = 'yes' and RTRIM(LTRIM(State)) = 'Florida') or RTRIM(LTRIM(Worldwide)) = 'yes'
      
      --nulls
      (visible = 'yes' and State = 'Florida') or ISNULL(Worldwide, 'yes') = 'yes'
      
      --unexpected data: need samples
      

      【讨论】:

        猜你喜欢
        • 2021-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多