2016-03-19 67 views
0

我有以下表結構:如何將一列的內容複製到另一列?

// 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*  |      |      | 
+----+---------+-----------------+------------------+------------------------+------------------------+ 

正如你在上面的表格中看到的,我只是不停的最後兩個編輯(content(html/markdown)是一個最後的編輯前或沒有編輯的內容和content_edit(html/markdown)是最後編輯或空當沒有任何編輯)


現在請在這兩個例子來看看:

例一:我首先要編輯已編輯。這裏是新的價值:

**content1_new_value** 

嗯,我也把它轉換使用PHP庫,以HTML並把這樣的:

<b>content1_new_value</b> 

我要填寫(更新)的content_(html/markdown)與當前內容的內容content_edit_(html/markdown)和填充(更新)此列content_edit_(html/markdown)新的價值,這樣的事情:

+----+---------+------------------------+----------------------+----------------------------+------------------------+ 
| 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** | 
+----+---------+------------------------+----------------------+----------------------------+------------------------+ 

例二:我想編輯第二個帖子,它已經沒有編輯了。這裏是新的價值:

*content2_new_value* 

嗯,我也把它轉換使用PHP庫,以HTML並把這樣的:

<i>content2_new_value</i> 

在這種情況下,我想保持content_(html/markdown)完整(沒有任何變化),只是更新content_edited_(html/markdown)新的價值,是這樣的:

+----+---------+-----------------+------------------+------------------------+--------------------------+ 
| 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>| 
+----+---------+-----------------+------------------+------------------------+--------------------------+ 

所以你看,我只是保持兩個最後的編輯。我怎樣才能做到這一點?

回答

2

檢查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 
+0

@stack如果答案解決您的問題,你也應該將其標記爲正確的。 – xpy

相關問題