我有2个表格完全相同: table1 和 table2 ,我需要执行相关的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;
查询工作正常,但正如您所见,只需搜索table1。
表1 保留旧桌面1(从0到127000), table2 是新帖子的新表格(从127001开始)。< / p>
修改 我尝试使用 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`);
答案 0 :(得分:2)
您可以通过在subselect
中组合表来实现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;
MATCH()列列表必须与某些列列表完全匹配 表的FULLTEXT索引定义,除非此MATCH()为IN 布尔模式。布尔模式搜索可以在非索引列上完成, 虽然它们可能很慢。
http://dev.mysql.com/doc/refman/5.0/en/fulltext-restrictions.html
答案 1 :(得分:0)
在选择列表中确实有两列slug
,子查询中不允许这样做。删除一个出现:
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