从共享相同 LIMIT 的多个表中查询
Query From multiple tables sharing the same LIMIT
我有一个常见的分页限制,即每页显示 30 个元素
现在我需要使用来自 3 个不同 table 的匹配来获得结果,并从每个 table 中获得 3 个字段。
Table A 字段 news_id, 标题, 内容
Table B 字段 events_id, 标题, 内容
Table C 字段 enc_id、标题、内容
以及where条件
WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(content) AGAINST('{$search}')OR MATCH(resume) AGAINST('{$search}') )
每个 table 行都有一个状态字段,在 where 条件下必须等于 1。
最后我需要为全局查询做一个LIMIT $start_from, 30
有什么可行的方法吗?
** 编辑 **
这是我用于单个 table
的查询
"SELECT
news_id,title,
FROM news
WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}')
AND noticias.status = 1
LIMIT 0, 30"
这是我尝试过但没有成功的方法。它应该带 7 行,但带了 30 行
SELECT
news.news_id AS id,
events.events_id AS id,
enc.enc_id AS id,
news.title AS title,
events.title AS title,
enc.title AS title
FROM news, events, enc
WHERE ( MATCH (news.title) AGAINST('{$search}') OR MATCH(news.resume) AGAINST('{$search}')OR MATCH(news.content) AGAINST('{$search}')
OR MATCH (enc.title) AGAINST('{$search}') OR MATCH(enc.resume) AGAINST('{$search}')OR MATCH(enc.content) AGAINST('{$search}')
OR MATCH (events.title) AGAINST('{$search}') OR MATCH(events.resume) AGAINST('{$search}')OR MATCH(events.content) AGAINST('{$search}') )
AND ( news.status = 1 OR enc.status = 1 OR events.status = 1)
LIMIT 0, 30
我找到了一个解决方案,所以我会在这里分享它以防其他人需要它。
使用UNION
:
(SELECT news_id AS id, title, 'news' AS origin FROM news WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}') AND status = 1) )
UNION
(SELECT events_id AS id, title, 'events' AS origin FROM events WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}') AND status = 1) )
UNION
(SELECT enc_id AS id, title, 'enc' AS origin FROM enc WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}') AND status = 1) )
LIMIT $start_from, 30
我使用字段 origin
来了解每个内容来自哪个 table。
我有一个常见的分页限制,即每页显示 30 个元素
现在我需要使用来自 3 个不同 table 的匹配来获得结果,并从每个 table 中获得 3 个字段。
Table A 字段 news_id, 标题, 内容 Table B 字段 events_id, 标题, 内容 Table C 字段 enc_id、标题、内容
以及where条件
WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(content) AGAINST('{$search}')OR MATCH(resume) AGAINST('{$search}') )
每个 table 行都有一个状态字段,在 where 条件下必须等于 1。
最后我需要为全局查询做一个LIMIT $start_from, 30
有什么可行的方法吗?
** 编辑 **
这是我用于单个 table
的查询"SELECT
news_id,title,
FROM news
WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}')
AND noticias.status = 1
LIMIT 0, 30"
这是我尝试过但没有成功的方法。它应该带 7 行,但带了 30 行
SELECT
news.news_id AS id,
events.events_id AS id,
enc.enc_id AS id,
news.title AS title,
events.title AS title,
enc.title AS title
FROM news, events, enc
WHERE ( MATCH (news.title) AGAINST('{$search}') OR MATCH(news.resume) AGAINST('{$search}')OR MATCH(news.content) AGAINST('{$search}')
OR MATCH (enc.title) AGAINST('{$search}') OR MATCH(enc.resume) AGAINST('{$search}')OR MATCH(enc.content) AGAINST('{$search}')
OR MATCH (events.title) AGAINST('{$search}') OR MATCH(events.resume) AGAINST('{$search}')OR MATCH(events.content) AGAINST('{$search}') )
AND ( news.status = 1 OR enc.status = 1 OR events.status = 1)
LIMIT 0, 30
我找到了一个解决方案,所以我会在这里分享它以防其他人需要它。
使用UNION
:
(SELECT news_id AS id, title, 'news' AS origin FROM news WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}') AND status = 1) )
UNION
(SELECT events_id AS id, title, 'events' AS origin FROM events WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}') AND status = 1) )
UNION
(SELECT enc_id AS id, title, 'enc' AS origin FROM enc WHERE ( MATCH (title) AGAINST('{$search}') OR MATCH(resume) AGAINST('{$search}')OR MATCH(content) AGAINST('{$search}') AND status = 1) )
LIMIT $start_from, 30
我使用字段 origin
来了解每个内容来自哪个 table。