stack
stack

Reputation: 10228

How can I copy the content of one column to another one?

I have following table structure:

// Posts
+----+---------+-----------------+------------------+------------------------+------------------------+
| id |  title  |  content_html   | content_markdown |   content_edit_html    | content_edit_markdown  |
+----+---------+-----------------+------------------+------------------------+------------------------+
| 1  | title1  | <b>content1</b> | **content1**     | <b>content1_edited</b> | **content1_edited**    |
| 2  | title2  | <i>content2</i> | *content2*       |                        |                        |
+----+---------+-----------------+------------------+------------------------+------------------------+

As you see in the above table, I just keep two last edit (content(html/markdown) is one before the last edit or the content without editing and content_edit(html/markdown) is last edit or empty if there isn't any edit)


Now please take a look at these two examples:

Example One: I want to edit first post which is edited already. Here is new value:

**content1_new_value**

Well I also convert it to html using a PHP library and make this:

<b>content1_new_value</b>

I want to fill (update) the content of content_(html/markdown) with the current content of content_edit_(html/markdown) and fill (update) this column content_edit_(html/markdown) with new value, something like this:

+----+---------+------------------------+----------------------+----------------------------+------------------------+
| id |  title  |  content_html          | content_markdown     | content_edit_html          | content_edit_markdown  |
+----+---------+------------------------+----------------------+----------------------------+------------------------+
| 1  | title1  | <b>content1_edited</b> | **content1_edited**  | <b>content1_new_value</b>  | **content1_new_value** |
+----+---------+------------------------+----------------------+----------------------------+------------------------+

Example Two: I want to edit second post which is not edited already. Here is new value:

*content2_new_value*

Well I also convert it to html using a PHP library and make this:

<i>content2_new_value</i>

In this case I want to keep content_(html/markdown) intact (without any change) and just update content_edited_(html/markdown) with new value, something like this:

+----+---------+-----------------+------------------+------------------------+--------------------------+
| id |  title  |  content_html   | content_markdown |   content_edit_html    | content_edit_markdown    |
+----+---------+-----------------+------------------+------------------------+--------------------------+
| 2  | title2  | <i>content2</i> | *content2*       | *content2_new_value*   | <i>content2_new_value</i>|
+----+---------+-----------------+------------------+------------------------+--------------------------+

So as you see, I just keep the two last edit. How can I do that?

Upvotes: 0

Views: 65

Answers (1)

Kay
Kay

Reputation: 413

Check CASE WHEN ELSE END

case  
when content_edit_html is not null
update table set content_html = content_edit_html, content_markdown = content_edit_markdown, 
content_edit_html = <b>content1_new_value</b>, content_edit_markdown = **content_new_value**
where id = 1
else
update table set content_edit_html = <b>content1_new_value</b>, content_edit_markdown = **content_new_value**
where id = 1
end

Upvotes: 2

Related Questions