公告板正在使用node.js和mysql实施。这次我将实现搜索功能。这是搜索表单在家庭中显示并在控制器中进行处理后最终显示为搜索页面的方式。但是,该表应与数据库和搜索表单字符串相同。如果不正确,它将不会出来。
表格
form(action=routers.search, method="get")
input(type="text" placeholder="searh", name="result")
input(type="submit" value="searh")
控制器
export const search = (req, res) => {
try {
let result_value = req.query.result;
let sql =
"SELECT id, folder, title, nicName, date_format(writeDate, '%Y/%m/%d %T') as writeDate FROM board where title=? order by id desc ";
dbConnection.query(sql, [result_value], (err, result) => {
if (err) {
console.log(err);
} else {
console.dir(result);
res.render("search", {
homeName: "watchingame",
pageName: "search",
routers,
result_value,
result
});
}
});
} catch (err) {
console.log(err);
}
};
视图
extends layouts/layout
block content
div
h2 #{result_value}
table
each board in result
tr
td=board.id
td
a(href=`/board${routers.boardDetail(board.id)}`)=board.title
td=board.nicName
td=board.writeDate
a(href=`/board${routers.boardCreate}`) write
答案 0 :(得分:0)
However, the table should be the same as the database and search form string. If it's not accurate, it won't come out.
我想这意味着仅当查询中的标题与表中项目的标题完全匹配时才会显示结果。
如果要进行query=dog, matches=dogs, the dog, hotdog, etc...)
之类的部分匹配,则应使用LiKE
和%
运算符。
下面是示例SQL语句。
SELECT
id,
folder,
title,
nicName,
date_format(writeDate, '%Y/%m/%d %T') as writeDate
FROM board where title like %dog% order by id desc
然后它将尝试查找标题中包含dog
的任何内容。
因此,您的sql
字符串应类似于
'SELECT
id,
folder,
title,
nicName,
date_format(writeDate, '%Y/%m/%d %T') as writeDate
FROM board where title LIKE ' + connection.escape('%' + result_value + '%')
如果您想详细了解SQL Like Operator
,请检出link。