0

我们的文章数据库有两个标签系统表:

1) 标签包含:
tag_id
文本(如政治)

2) tagmaps 包含:
tag_id
article_id

我想列出每篇文章的所有标签(例如:“politics Diplomacy al gore”)并将它们插入到我们搜索表的关键字字段中(使用 article_id 和关键字字段)。

我知道我可以在 phpmyadmin 中做到这一点,但不知道如何......

4

2 回答 2

2

If you want the data to be returned in the same field, then you can use GROUP_CONCAT():

select m.article_id,
    group_concat(t.text) tags
from tags t
inner join tagmaps m
    on t.tag_id = m.tag_id
group by m.article_id

Result from this would be:

article_id  | tags
1           | tag1, tag2, tag3

Then if you want them inserted into you table:

insert into search (article_id, keywords)
select m.article_id,
    group_concat(t.text)
from tags t
inner join tagmaps m
    on t.tag_id = m.tag_id
group by m.article_id

If you don't want the text values in the same field:

insert into search (article_id, keywords)
select m.article_id,
    t.text
from tags t
inner join tagmaps m
    on t.tag_id = m.tag_id

If you are trying to UPDATE the search table, then you can use:

update search
set keywords = (select  group_concat(t.text separator ', ') tags
                from tags t
                inner join tagmaps m
                    on t.tag_id = m.tag_id
                where search.article_id = m.article_id
                group by m.article_id);

See SQL Fiddle with Demo

于 2012-11-08T18:08:36.400 回答
0

From a friend:

update engine4_core_search left join (select resource_id as tagid , group_concat(engine4_core_tags.text SEPARATOR ', ') as keywords from engine4_core_tagmaps left join engine4_core_tags on engine4_core_tagmaps.tag_id=engine4_core_tags.tag_id group by resource_id) keywordstable on engine4_core_search.id=keywordstable.tagid set engine4_core_search.keywords=keywordstable.keywords where engine4_core_search.type='artarticle' and engine4_core_search.id <10

That will update 10 rows....

于 2012-11-08T19:55:38.203 回答