Mysql 如何为自动完成框选择标签?
以下是我的表格结构:Mysql 如何为自动完成框选择标签?,mysql,sql,regex,Mysql,Sql,Regex,以下是我的表格结构: // tags +----+------------+----------------------------------+----------+----------+------------+ | id | name | description | related | used_num | date_time | +----+------------+--------------------------------
// tags
+----+------------+----------------------------------+----------+----------+------------+
| id | name | description | related | used_num | date_time |
+----+------------+----------------------------------+----------+----------+------------+
| 1 | PHP | some explanations for PHP | 1 | 4234 | 1475028896 |
| 2 | SQL | some explanations for SQL | 2 | 734 | 1475048601 |
| 3 | jQuery | some explanations for jQuery | 3 | 434 | 1475068321 |
| 4 | MySQL | some explanations for MySQL | 2 | 657 | 1475068332 |
| 5 | JavaScript | some explanations for JavaScript | 3 | 3325 | 1475071430 |
| 6 | HTML | some explanations for HTML | 6 | 2133 | 1475077842 |
| 7 | postgresql | some explanations for postgresql | 2 | 43 | 1475077851 |
| 8 | script | some explanations for script | 8 | 3 | 1475077935 |
+----+------------+----------------------------------+----------+----------+------------+
现在我需要根据标签名称的一部分来选择标签,还需要选择所有相关的标签
例如:
字符串:scr。预期产出:
+----+------------+----------------------------------+----------+----------+------------+
| 3 | jQuery | some explanations for jQuery | 3 | 434 | 1475068321 |
| 5 | JavaScript | some explanations for JavaScript | 3 | 3325 | 1475071430 |
| 8 | script | some explanations for script | 8 | 3 | 1475077935 |
+----+------------+----------------------------------+----------+----------+------------+
-- Noted that "jQuery" tag is selected because of its relation with "JavaScript" tag
+----+------------+----------------------------------+----------+----------+------------+
| 1 | PHP | some explanations for PHP | 1 | 4234 | 1475028896 |
+----+------------+----------------------------------+----------+----------+------------+
+----+------------+----------------------------------+----------+----------+------------+
| 2 | SQL | some explanations for SQL | 2 | 734 | 1475048601 |
| 4 | MySQL | some explanations for MySQL | 2 | 657 | 1475068332 |
| 7 | postgresql | some explanations for postgresql | 2 | 43 | 1475077851 |
+----+------------+----------------------------------+----------+----------+------------+
-- Noted that both "SQL" and "postgresql" are selected because of their relation with "MySQL" tag
字符串:ph.预期输出:
+----+------------+----------------------------------+----------+----------+------------+
| 3 | jQuery | some explanations for jQuery | 3 | 434 | 1475068321 |
| 5 | JavaScript | some explanations for JavaScript | 3 | 3325 | 1475071430 |
| 8 | script | some explanations for script | 8 | 3 | 1475077935 |
+----+------------+----------------------------------+----------+----------+------------+
-- Noted that "jQuery" tag is selected because of its relation with "JavaScript" tag
+----+------------+----------------------------------+----------+----------+------------+
| 1 | PHP | some explanations for PHP | 1 | 4234 | 1475028896 |
+----+------------+----------------------------------+----------+----------+------------+
+----+------------+----------------------------------+----------+----------+------------+
| 2 | SQL | some explanations for SQL | 2 | 734 | 1475048601 |
| 4 | MySQL | some explanations for MySQL | 2 | 657 | 1475068332 |
| 7 | postgresql | some explanations for postgresql | 2 | 43 | 1475077851 |
+----+------------+----------------------------------+----------+----------+------------+
-- Noted that both "SQL" and "postgresql" are selected because of their relation with "MySQL" tag
字符串:ys。预期产出:
+----+------------+----------------------------------+----------+----------+------------+
| 3 | jQuery | some explanations for jQuery | 3 | 434 | 1475068321 |
| 5 | JavaScript | some explanations for JavaScript | 3 | 3325 | 1475071430 |
| 8 | script | some explanations for script | 8 | 3 | 1475077935 |
+----+------------+----------------------------------+----------+----------+------------+
-- Noted that "jQuery" tag is selected because of its relation with "JavaScript" tag
+----+------------+----------------------------------+----------+----------+------------+
| 1 | PHP | some explanations for PHP | 1 | 4234 | 1475028896 |
+----+------------+----------------------------------+----------+----------+------------+
+----+------------+----------------------------------+----------+----------+------------+
| 2 | SQL | some explanations for SQL | 2 | 734 | 1475048601 |
| 4 | MySQL | some explanations for MySQL | 2 | 657 | 1475068332 |
| 7 | postgresql | some explanations for postgresql | 2 | 43 | 1475077851 |
+----+------------+----------------------------------+----------+----------+------------+
-- Noted that both "SQL" and "postgresql" are selected because of their relation with "MySQL" tag
我该怎么做
实际上我可以这样做:
SELECT * FROM tags WHERE name LIKE %:str%
但是我的查询不支持相关列。将表本身连接起来,并查找出现在两个名称列中的行。这样,您也可以搜索相关元素的名称
SELECT t1.*
FROM tags t1
LEFT JOIN tags t2 ON t1.id = t2.related
WHERE t1.name LIKE %:str% OR t2.name LIKE %:str%;
这里的一个选项是自连接标记表,然后根据输入标记对相关标记进行分组连接。下面的查询将输出与给定输入相关的标记的CSV列表。例如,如果:str是“MySQL”,那么输出将是“MySQL,postgresql,SQL”。如果您使用的是PHP或Java之类的语言,那么分解CSV列表并将这些建议放入下拉列表中以便自动完成应该很容易
SELECT t1.name,
GROUP_CONCAT(t2.name)
FROM tags t1
INNER JOIN tags t2
ON t1.id = t2.related
WHERE t1.name LIKE '%:str%' -- e.g. MySQL
GROUP BY t1.name
您还可以进行自连接,并为每个建议的标记返回一条记录:
SELECT t2.name
FROM tags t1
INNER JOIN tags t2
ON t1.id = t2.related
WHERE t1.name LIKE '%:str%' -- e.g. MySQL
尝试使用
SELECT * FROM tags a WHERE name LIKE %:str% or exist (SELECT id FROM tags b WHERE b.name LIKE %:str% and b.id=a.related)
因为自动完成必须很快,所以你不能在多个地方寻找答案
建立并维护一个自动完成的特殊表格;它将有一个文本或varchar列,其中包含给定项目的所有可能关键字,再加上另一列,其中包含关于它所指内容的信息。@Drew yeah我知道,这不是您建议的数据库设计。为什么您的例如是完整的标记名?它是否也适用于非完整标记名?像mys、ysql或ys?只有一件事,为什么不选择description并使用_num列?我正试着做这样的事情:听起来不错,向上投票。。但是很模糊。。你能再给我解释一下吗?还是表结构和查询?