MySql 存储过程搜索赞
MySql Store Procedure Search Like
我有一个带有名为“查询”的参数的搜索字段,此参数将搜索不同的列以进行匹配。它有效,但查询必须完全匹配才能获得 return。我试过使用“%”,但我认为我没有正确使用它。我正在尝试进行更通用的搜索。
CREATE DEFINER=`root`@`localhost` PROCEDURE `client_search_reps`(IN offset INT, IN row_count INT, IN query varchar(100))
BEGIN
SELECT
U.Id,
U.RoleId,
UP.FirstName,
UP.LastName,
UP.FileUrl,
L.City,
L.Zip,
CP.Name,
CP.Url,
CP.Phone,
CP.Email,
P.ProductOne,
P.ProductTwo,
P.ProductThree,
P.ProductFour,
FROM user_profiles AS UP
LEFT JOIN users AS U ON U.Id = UP.UserId
LEFT JOIN location AS L ON L.UserProfileId = UP.UserId
LEFT JOIN company_profile AS CP ON CP.UserId = UP.UserId
LEFT JOIN products AS P ON P.UserId = U.Id
WHERE UP.FirstName LIKE query || UP.LastName LIKE query || CP.Name LIKE query
|| CP.Phone LIKE query || CP.Email LIKE query
LIMIT offset, row_count;
END
下面是我的 React.Js 代码,以防它有助于理解我的问题。
searchAccount = (query) => {
profileServices
.searchAccounts(0, query)
.then(this.searchSuccess)
.catch(this.searchError);
};
searchSuccess = (data) => {
let accounts = data.item.pagedItems;
this.setState({
mappedProfiles: accounts.map(this.mapSearch),
currentItems: data.item.totalCount,
});
};
searchError = (data) => {
swal({
title: "Search is Broad",
text: "Search by: Company Name, Phone, or Email",
icon: "warning",
buttons: true,
dangerMode: true,
});
};
onSearch = (e) => {
let value = e.target.value;
this.setState((prevState) => {
return {
...prevState,
query: value,
};
});
};
clearSearch = () => {
this.setState((prevState) => {
return {
...prevState,
query: "",
};
});
// this.getProfiles(0);
};
search = () => {
if (this.state.query.length > 0 ? this.searchAccount(this.state.query) : 0);
this.setState({
searchModal: true,
});
};
您可以将 SQL 的通配符添加到 query
CREATE PROCEDURE client_search_reps(
offset INT,
row_count INT,
query varchar(100)
)
BEGIN
SET query = CONCAT('%', query, '%');
...
顺便说一句,最好使用标准 OR
而不是非标准 ||
(||
在 MySQL 8.0.17 中已弃用,并且有不同的行为取决于sql_mode 在所有版本中使用)。
我有一个带有名为“查询”的参数的搜索字段,此参数将搜索不同的列以进行匹配。它有效,但查询必须完全匹配才能获得 return。我试过使用“%”,但我认为我没有正确使用它。我正在尝试进行更通用的搜索。
CREATE DEFINER=`root`@`localhost` PROCEDURE `client_search_reps`(IN offset INT, IN row_count INT, IN query varchar(100))
BEGIN
SELECT
U.Id,
U.RoleId,
UP.FirstName,
UP.LastName,
UP.FileUrl,
L.City,
L.Zip,
CP.Name,
CP.Url,
CP.Phone,
CP.Email,
P.ProductOne,
P.ProductTwo,
P.ProductThree,
P.ProductFour,
FROM user_profiles AS UP
LEFT JOIN users AS U ON U.Id = UP.UserId
LEFT JOIN location AS L ON L.UserProfileId = UP.UserId
LEFT JOIN company_profile AS CP ON CP.UserId = UP.UserId
LEFT JOIN products AS P ON P.UserId = U.Id
WHERE UP.FirstName LIKE query || UP.LastName LIKE query || CP.Name LIKE query
|| CP.Phone LIKE query || CP.Email LIKE query
LIMIT offset, row_count;
END
下面是我的 React.Js 代码,以防它有助于理解我的问题。
searchAccount = (query) => {
profileServices
.searchAccounts(0, query)
.then(this.searchSuccess)
.catch(this.searchError);
};
searchSuccess = (data) => {
let accounts = data.item.pagedItems;
this.setState({
mappedProfiles: accounts.map(this.mapSearch),
currentItems: data.item.totalCount,
});
};
searchError = (data) => {
swal({
title: "Search is Broad",
text: "Search by: Company Name, Phone, or Email",
icon: "warning",
buttons: true,
dangerMode: true,
});
};
onSearch = (e) => {
let value = e.target.value;
this.setState((prevState) => {
return {
...prevState,
query: value,
};
});
};
clearSearch = () => {
this.setState((prevState) => {
return {
...prevState,
query: "",
};
});
// this.getProfiles(0);
};
search = () => {
if (this.state.query.length > 0 ? this.searchAccount(this.state.query) : 0);
this.setState({
searchModal: true,
});
};
您可以将 SQL 的通配符添加到 query
CREATE PROCEDURE client_search_reps(
offset INT,
row_count INT,
query varchar(100)
)
BEGIN
SET query = CONCAT('%', query, '%');
...
顺便说一句,最好使用标准 OR
而不是非标准 ||
(||
在 MySQL 8.0.17 中已弃用,并且有不同的行为取决于sql_mode 在所有版本中使用)。