按姓氏、名字和名字进行全文搜索
Full-Text Search by last name, first name and second name
您好,我使用 SQL 服务器 ,我想在下面进行全文搜索 table
CREATE TABLE Students
(
id int PRIMARY KEY IDENTITY,
firstName varchar(MAX) NOT NULL CHECK (firstName != ''),
secondName varchar(MAX) NULL CHECK (secondName != ''),
lastName varchar(MAX) NOT NULL CHECK (lastName != ''),
birthDate datetime2 NOT NULL CHECK (birthDate >= '1.01.1990'),
recordBookNumber int NOT NULL UNIQUE CHECK (recordBookNumber > 0),
enrollmentDate datetime2 NOT NULL CHECK (enrollmentDate >= '1.01.1990'),
groupId int NOT NULL FOREIGN KEY REFERENCES [Groups](id) ON DELETE CASCADE
)
所以我创建了视图 StudentsSearchView、聚簇索引、全文目录和全文索引
全文索引在这里:
CREATE FULLTEXT INDEX ON
dbo.StudentsSearchView (
firstName LANGUAGE 1033,
secondName LANGUAGE 1033,
lastName LANGUAGE 1033
)
KEY INDEX IDX_SEARCH ON ftCatalog
WITH (
CHANGE_TRACKING = AUTO,
STOPLIST = SYSTEM
)
所以我有一个文本框,我在其中输入像 'John Peter Smith' 这样的字符串,我想得到所有
匹配,但我不知道我需要使用什么查询。我试试这个
SELECT * FROM
dbo.StudentsSearchView
WHERE CONTAINS (
*, '"LastName*" & "FirstName*" & "SecondName*"'
)
但是没有找到,虽然我在table这一行。
P.S. LastName、FirstName 和 SecondName 将以编程方式插入
我解决了。我的错误是视图。这是
CREATE VIEW
dbo.StudentsSearchView WITH SCHEMABINDING
AS
SELECT
id, firstName, secondName, lastName
FROM
dbo.Students
GO
但是需要这样:
CREATE VIEW
dbo.StudentsSearchView (id, Fio) WITH SCHEMABINDING
AS
SELECT
id, firstName + ' ' + ISNULL(secondName, '') + ' ' + lastName
FROM
dbo.Students
GO
所以,现在这个 Select 查询有效了
SELECT * FROM
dbo.StudentsSearchView
WHERE CONTAINS (
*, '"LastName*" & "FirstName*" & "SecondName*"'
)
您好,我使用 SQL 服务器 ,我想在下面进行全文搜索 table
CREATE TABLE Students
(
id int PRIMARY KEY IDENTITY,
firstName varchar(MAX) NOT NULL CHECK (firstName != ''),
secondName varchar(MAX) NULL CHECK (secondName != ''),
lastName varchar(MAX) NOT NULL CHECK (lastName != ''),
birthDate datetime2 NOT NULL CHECK (birthDate >= '1.01.1990'),
recordBookNumber int NOT NULL UNIQUE CHECK (recordBookNumber > 0),
enrollmentDate datetime2 NOT NULL CHECK (enrollmentDate >= '1.01.1990'),
groupId int NOT NULL FOREIGN KEY REFERENCES [Groups](id) ON DELETE CASCADE
)
所以我创建了视图 StudentsSearchView、聚簇索引、全文目录和全文索引
全文索引在这里:
CREATE FULLTEXT INDEX ON
dbo.StudentsSearchView (
firstName LANGUAGE 1033,
secondName LANGUAGE 1033,
lastName LANGUAGE 1033
)
KEY INDEX IDX_SEARCH ON ftCatalog
WITH (
CHANGE_TRACKING = AUTO,
STOPLIST = SYSTEM
)
所以我有一个文本框,我在其中输入像 'John Peter Smith' 这样的字符串,我想得到所有 匹配,但我不知道我需要使用什么查询。我试试这个
SELECT * FROM
dbo.StudentsSearchView
WHERE CONTAINS (
*, '"LastName*" & "FirstName*" & "SecondName*"'
)
但是没有找到,虽然我在table这一行。
P.S. LastName、FirstName 和 SecondName 将以编程方式插入
我解决了。我的错误是视图。这是
CREATE VIEW
dbo.StudentsSearchView WITH SCHEMABINDING
AS
SELECT
id, firstName, secondName, lastName
FROM
dbo.Students
GO
但是需要这样:
CREATE VIEW
dbo.StudentsSearchView (id, Fio) WITH SCHEMABINDING
AS
SELECT
id, firstName + ' ' + ISNULL(secondName, '') + ' ' + lastName
FROM
dbo.Students
GO
所以,现在这个 Select 查询有效了
SELECT * FROM
dbo.StudentsSearchView
WHERE CONTAINS (
*, '"LastName*" & "FirstName*" & "SecondName*"'
)