gpt4 book ai didi

mysql - 加入 2 个相同的表以在 MYSQL 中执行全文搜索

转载 作者:行者123 更新时间:2023-11-29 06:37:38 25 4
gpt4 key购买 nike

我有 2 个完全相同的表:table1table2,我需要执行相关的 table1,现在我只是使用 table1 进行以下查询。

    select SQL_CALC_FOUND_ROWS
table1.slug as slugpost,
table1.titulo,
table1.id,
table1.id_usu,
table1.id_cat,
table1.slug,
table1.fecha,
table1.html,
table1.hit,
table1.etiquetas,
table1.status as table1tatus,
categorias.nombre,
categorias.slug as slugcategoria,
categorias.icon,
usuarios.id as idusuarios,
usuarios.usuario
from table1
LEFT JOIN usuarios
on table1.id_usu = usuarios.id
LEFT JOIN categorias on categorias.id=table1.id_cat
WHERE
MATCH(titulo, etiquetas, html) AGAINST ('".$query."')
limit 1,5;

查询工作正常,但如您所见,只搜索表 1。

Table1 保留我站点中的旧表 1(从 0 号到 127000),table2 是新帖子的新表(从 127001 号开始)。

编辑我试过使用 UNION 但我无法让它工作,MYSQL 说 找不到与列列表匹配的 FULLTEXT 索引

SELECT SQL_CALC_FOUND_ROWS
t.slug as slugpost,
t.titulo,
t.id,
t.id_usu,
t.id_cat,
t.fecha,
t.html,
t.hit,
t.etiquetas,
t.status as table1tatus,
categorias.nombre,
categorias.slug as slugcategoria,
categorias.icon,
usuarios.id as idusuarios,
usuarios.usuario
FROM
(
SELECT table1.slug,table1.titulo,table1.id,table1.id_usu,table1.id_cat,table1.fecha,table1.html,table1.hit,table1.etiquetas,table1.status FROM table1
UNION
SELECT table2.slug,table2.titulo,table2.id,table2.id_usu,table2.id_cat,table2.fecha,table2.html,table2.hit,table2.etiquetas,table2.status FROM table2
) t
LEFT JOIN usuarios on t.id_usu = usuarios.id
LEFT JOIN categorias on categorias.id=t.id_cat
WHERE
MATCH(titulo, etiquetas, html) AGAINST ('dvdrip latino')
limit 1,5;

我还运行了以下命令

 ALTER TABLE `table1` ADD FULLTEXT INDEX `test` (`title`, `tags`, `html`); 
ALTER TABLE `table1` ADD FULLTEXT INDEX `test2` (`title`, `tags`, `html`);

最佳答案

你可以通过在子选择中组合你的表来做到这一点

SELECT 
t.slug as slugpost
,...
FROM
(
SELECT * FROM table1
UNION
SELECT * FROM table2
) t
LEFT JOIN usuarios on t.id_usu = usuarios.id
LEFT JOIN categorias on categorias.id=t.id_cat
WHERE ...

要使用全文搜索,您应该为两个表的列添加全文索引

ALTER TABLE `table1` ADD FULLTEXT INDEX `test` (`titulo`, `etiquetas`, `html`); 
ALTER TABLE `table1` ADD FULLTEXT INDEX `test2` (`titulo`, `etiquetas`, `html`);

另一种使用全文索引的方法,您可以通过对子选择中的每个查询单独使用 MATCH() 编写联合查询,如下所示

SELECT SQL_CALC_FOUND_ROWS
t.slug as slugpost,
t.titulo,
t.id,
t.id_usu,
t.id_cat,
t.fecha,
t.html,
t.hit,
t.etiquetas,
t.status as table1tatus,
categorias.nombre,
categorias.slug as slugcategoria,
categorias.icon,
usuarios.id as idusuarios,
usuarios.usuario
FROM
(
SELECT table1.slug,table1.titulo,table1.id,table1.id_usu,table1.id_cat,table1.fecha,table1.html,table1.hit,table1.etiquetas,table1.status FROM table1
WHERE MATCH(`title`, `tags`, `html`) AGAINST ('dvdrip latino')
UNION
SELECT table2.slug,table2.titulo,table2.id,table2.id_usu,table2.id_cat,table2.fecha,table2.html,table2.hit,table2.etiquetas,table2.status FROM table2
WHERE MATCH(`title`, `tags`, `html`) AGAINST ('dvdrip latino')
) t
LEFT JOIN usuarios on t.id_usu = usuarios.id
LEFT JOIN categorias on categorias.id=t.id_cat
limit 1,5;

The MATCH() column list must match exactly the column list in some FULLTEXT index definition for the table, unless this MATCH() is IN BOOLEAN MODE. Boolean-mode searches can be done on nonindexed columns, although they are likely to be slow.

http://dev.mysql.com/doc/refman/5.0/en/fulltext-restrictions.html

关于mysql - 加入 2 个相同的表以在 MYSQL 中执行全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com