使用select result替换字符串

时间:2013-07-18 15:50:30

标签: mysql sql

我有一个表,我需要将值从verse表连接到chapter表。 仅当经文字段值以插入符号('^')开头时,才应执行此操作。它们都共享entity_id列。

我做了以下选择查询,得到了我需要的值:

SELECT 
chapter.field_bible_chapter_value,
verse.field_bible_verse_value,
verse.entity_id
FROM field_data_field_bible_verse AS verse
INNER JOIN field_data_field_bible_chapter AS chapter
ON chapter.entity_id = verse.entity_id
WHERE verse.field_bible_verse_value LIKE '^%'
ORDER BY verse.entity_id

现在我很难尝试使用这些数据对章节字段进行实际更改。

我该如何处理?

2 个答案:

答案 0 :(得分:1)

如果我理解了正确的要求,请尝试以下sql:

UPDATE field_data_field_bible_verse AS verse
INNER JOIN field_data_field_bible_chapter AS chapter
ON chapter.entity_id = verse.entity_id
SET chapter.field_bible_chapter_value = concat(verse.field_bible_verse_value,' ',chapter.field_bible_chapter_value)
WHERE verse.field_bible_verse_value LIKE '^%'

逻辑如下:

IF entity_id is equal THEN
    IF the verse_value starts with '^' THEN
       Concatenate verse value with chapter value
       Replace chapter value with the concatenated value

答案 1 :(得分:0)

您似乎想要进行多表更新。语法非常简单,请参阅http://dev.mysql.com/doc/refman/5.0/en/update.html

对于您的示例,这将类似于

UPDATE field_data_field_bible_verse AS verse
INNER JOIN field_data_field_bible_chapter AS chapter
ON chapter.entity_id = verse.entity_id
SET chapter.TARGET_COLUMN = concat(YOUR_VALUES)
WHERE verse.field_bible_verse_value LIKE '^%'

(您必须使用正确的名称和值填充SET行)