如何为自动完成框选择标签?

时间:2016-09-29 01:07:04

标签: mysql sql regex

这是我的表结构:

// 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

字符串:ph。预期产出:

+----+------------+----------------------------------+----------+----------+------------+
| 1  | PHP        | some explanations for PHP        | 1        | 4234     | 1475028896 |
+----+------------+----------------------------------+----------+----------+------------+

字符串:ys。预期产出:

+----+------------+----------------------------------+----------+----------+------------+
| 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%

但我的查询不支持related列。

4 个答案:

答案 0 :(得分:1)

将表连接到自身,并在两个名称列中的任意一列中查找出现的行。这样,您也可以搜索相关元素的名称。

class ChildClassD extends ParentClassWithFinal {
    public function ChildClassD() {
    }
}

// Fatal error: Cannot override final ParentClassWithFinal::__construct() with ChildClassD::ChildClassD()
// Also in PHP 7: Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; ChildClassD has a deprecated constructor

答案 1 :(得分:1)

此处的一个选项是自联接<table class="table table-hover"> <tr><td>body cell</td></tr> <tr class="no-hover"><th>head cell</th></tr> <tr><td>body cell</td></tr> </table> 表,然后根据输入标记对相关标签进行分组。下面的查询将输出与给定输入相关的CSV标签列表。例如,如果tags:str,则输出为'MySQL'。如果您使用的是PHP或Java等语言,则应该很容易爆炸该CSV列表并将这些建议放入下拉列表中以进行自动完成。

'MySQL,postgresql,SQL'

您还可以进行自我加入并为每个建议的标记返回一条记录:

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

答案 2 :(得分:1)

尝试使用

df <- data.frame(x = c(1:10),y = c(2,2,3,4,5,5,6,7,5,4))

library(ggplot2)
ggplot(data=df, aes(x,y, group=1)) +
  geom_line() +
  scale_x_reverse() +
  geom_vline(xintercept=3) +
  #geom_text(data = data.frame(x = 3, y = 5), label = paste("paste(3400, cm)","^-1", sep=""), 
  #  angle=90, parse = TRUE, vjust = 1.2) + 
  annotate("text", x = 3, y = 5, angle = 90, label = paste("paste(3400, cm)","^-1", sep=""), 
    vjust = 1.2, parse = TRUE)

答案 3 :(得分:1)

由于自动填充必须很快,因此您无法在多个地方寻找答案。

构建(并维护)一个用于自动完成的特殊表;它将有一个text(或varchar)列,其中包含给定项目的所有可能关键字,以及另一个包含其所引用内容的信息的列。