根据字段的开头选择记录?

时间:2011-05-31 19:24:21

标签: sql ms-access ms-access-2007

我有一个数据库,其表格包​​含标识符c1,c2,c3..etc ..

如何在其中编写包含OR串的查询,而不是使用能够捕获以某个字母开头的所有记录的内容来修改下面的查询?

SELECT 
    Person.spineinjuryAdmit, 
    tblComorbidity.comorbidityexplanation,
    Count(tblComorbidity.comorbidityexplanation) AS CountOfcomorbidityexplanation

FROM tblKentuckyCounties 
INNER JOIN (tblComorbidity 
            INNER JOIN (Person 
                        INNER JOIN tblComorbidityPerson 
                            ON Person.PersonID = tblComorbidityPerson.personID) 
                ON tblComorbidity.ID = tblComorbidityPerson.comorbidityFK) 
    ON tblKentuckyCounties.ID = Person.County
GROUP BY    Person.spineinjuryAdmit, 
            tblComorbidity.comorbidityexplanation
HAVING (((Person.spineinjuryAdmit)="c1" Or 
         (Person.spineinjuryAdmit)="c2" Or 
         (Person.spineinjuryAdmit)="c3"));

6 个答案:

答案 0 :(得分:14)

您是否尝试过使用LIKE?举个例子:

SELECT * FROM patients WHERE lastName LIKE 'm%';

这将返回patients.lastName以'm'开头的记录。 '%'字符可能是'*'用于访问,我不记得了。在某些数据库中,您还可以使用“_”来匹配单个字符(或者您添加的许多下划线)。

答案 1 :(得分:3)

    SELECT Person.spineinjuryAdmit, tblComorbidity.comorbidityexplanation, Count(tblComorbidity.comorbidityexplanation) AS CountOfcomorbidityexplanation
FROM tblKentuckyCounties INNER JOIN (tblComorbidity INNER JOIN (Person INNER JOIN tblComorbidityPerson ON Person.PersonID = tblComorbidityPerson.personID) ON tblComorbidity.ID = tblComorbidityPerson.comorbidityFK) ON tblKentuckyCounties.ID = Person.County
GROUP BY Person.spineinjuryAdmit, tblComorbidity.comorbidityexplanation
HAVING (Person.spineinjuryAdmit LIKE "c*");

答案 2 :(得分:3)

您可以使用WHERE子句在执行GROUP BY之前排除您不想要的行。

SELECT 
    p.spineinjuryAdmit, 
    c.comorbidityexplanation,
    Count(c.comorbidityexplanation) AS CountOfcomorbidityexplanation
FROM tblKentuckyCounties AS k
INNER JOIN (tblComorbidity AS c
            INNER JOIN (Person AS p
                        INNER JOIN tblComorbidityPerson AS cp
                            ON p.PersonID = cp.personID) 
                ON c.ID = cp.comorbidityFK) 
    ON k.ID = p.County
WHERE p.spineinjuryAdmit ALike "c%"
GROUP BY    p.spineinjuryAdmit, 
            c.comorbidityexplanation

如果您的查询是在SQL-89模式下执行的,则可以将其用作WHERE子句。

WHERE p.spineinjuryAdmit Like "c*"

在SQL-92模式下,您需要标准的ANSI通配符。

WHERE p.spineinjuryAdmit Like "c%"

我使用ALike告诉数据库引擎期望ANSI通配符。

DAO使用SQL-89模式...除非您将数据库选项设置为使用SQL-92模式(“SQL Server兼容语法”)。

如果您使用ADO运行查询,它将始终使用SQL-92模式。

答案 3 :(得分:1)

您有两种选择:

  1. 使用LIKE运算符

  2. 使用IN运算符

  3. 例如:

    Person.spineinjuryAdmit LIKE "c*"
    
    Person.spineinjuryAdmit IN ("c1", "c2", "c3")
    

    有关LIKE的详细信息,请参阅http://office.microsoft.com/en-us/access-help/like-operator-HP001032253.aspx

    公平警告:Access中的LIKE通配符为*?,而不是%_(与大多数其他版本的SQL一样)。

答案 4 :(得分:0)

您可以修改此项以在WHERE子句中包含过滤器列表。以下将查找姓氏以Smith开头的患者。 (即SmithSmithson等)以及Admitc开头的人。

....
WHERE spineinjuryAdmit LIKE 'c*'
AND   Patient.FirstName LIKE 'Smith*'
GROUP BY    Person.spineinjuryAdmit,             
            tblComorbidity.comorbidityexplanation;

答案 5 :(得分:0)

您可以使用regexp查询以多个字符开头的所有行。

SELECT * FROM table WHERE column REGEXP '^[ c1, c2, c3]';

此查询将返回列以“c1”或“c2”或“c3”开头的所有行。