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

dzi*_*lla 6 sql ms-access ms-access-2007

我有一个数据库,其表具有标识符c1,c2,c3..etc ..

不是编写一堆包含ORs 的查询,而是如何修改下面的查询,以便捕获以某个字母开头的所有记录?

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"));
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 17

你试过用过LIKE吗?举个例子:

SELECT * FROM patients WHERE lastName LIKE 'm%';
Run Code Online (Sandbox Code Playgroud)

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

  • 传统的Access(Jet/ACE)通配符是*和?,使用默认的"ANSI 89 SQL Mode"(Access是专有的).%和_是ANSI 92 SQL,您可以在ANSI 92 SQL模式下运行Access,但我不推荐它(它在ANSI 89模式下构建的应用程序中打破了很多东西).或者,如果要使用ANSI 92通配符,可以使用专有的ALIKE运算符,或使用OLEDB/ADO运行SQL,默认情况下使用ANSI 92模式(DAO使用ANSI 89). (2认同)

JK0*_*124 5

    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*");
Run Code Online (Sandbox Code Playgroud)