[英]How to update a table with the information from another table using only SQL?
適用於Win64(x86_64)的MySQL Ver 14.14 Distrib 5.7.16
我有兩個表:
content_table: content_id, content(String), tag_id(int)
tag_table: tag_id(int), tag(String)
現在content_table.tag_id
全部為空。
我想做這樣的事情:
對於content_table中的每條記錄,我想找到正確的tag_id(僅一個)並更新record.tag_id。 標簽應為內容的子字符串。
有一種方法只能使用SQL嗎? 還是我必須使用JDBC或其他語言的東西?
謝謝。
數據示例:
在content_table中,我有以下記錄: content_id, content, tag_id 1, "This is a great movie", 0 2, "I bought the car last week", 0 3, "His iPad is a gift from his brother", 0
在tag_table中,我有以下記錄: tag_id, tag 1, "movie" 2, "iPad" 3, "car" 4, "phone" ...
操作后,我想將content_table更新為: content_id, content, tag_id 1, "This is a great movie", 1 2, "I bought the car last week", 3 3, "His iPad is a gift from his brother", 2
這些記錄都是示例,content_table中實際上有10K +條記錄以及tag_table中的3k +條記錄。
從標簽表中填充一個帶有tag_id,tag的二維數組,即tagArray,然后在數組上迭代到substiture id,並像將數組中的變量一樣,放入下面的update sql語句中並執行該語句:
字符串sqlUpdate =“ UPDATE content_table SET tag_id =” + tagArray [i] [0] +“內容類似於CONCAT('%',” + tagArray [i] [1] +“,'%')”;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.