如何使用 SQL 命令从表中的某一列复制多条记录,并插入以更新 mysql 中具有变化选项的列中的另一表
列:“targetCategory”条件所有记录包含Top/name_test2, Top/name_test2/%
列“title”、“parentTopic”引用数据 注意:数据有根不同不导入示例:Top/name_test752
table_exemple1
===========================================================================
| aliasID | title | targetCategory | parentTopic |
===========================================================================
No | 1 | name_test752 | Top/name_test752/tel | Top/name_test752 |
---------------------------------------------------------------------------
|-<----| 2 | name_test2 | Top/name_test2 | Top/name_test2 |
| ---------------------------------------------------------------------------
||-<---| 3 | 5 | Top/name_test2/etc/5 | Top/name_test2/etc|
|| ---------------------------------------------------------------------------
|||-<--| 4 | tesla | Top/name_test2/tesla | Top/name_test2 |
||| ----------------------------------------------------------------------------
|||
|||
||| **tabla_exemple2 before update**
||| =====================================================================================================================================================================
||| | id | topic | topicShort | parentTopic | displayName | description | lastUpdate | sitecount |
||| =====================================================================================================================================================================
||| | 1 | Top/name_test/helloo | helloo | Top/name_test752 | Nuul | hello | 2010-06-19 04:10:14 | 324 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
||| | 2 | Top/name_test2/nest | nest | Top/name_test2/ | Nuul | nest | 2010-06-19 04:11:23 | 545435 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
||| | 3 | Top/name_test2/etc/57 | 5 | Top/name_test2/etc/ | Nuul | 5 | 2010-06-19 04:15:13 | 54354343 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
||| | 4 | Top/name_test2/tesla1 | tesla | Top/name_test2/ | Nuul | tesla | 2010-06-19 04:10:14 | 676756 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
|||
|||
||| **table_exemple2** after update
|||
|||
||| =====================================================================================================================================================================
||| | id | topic | topicShort | parentTopic | displayName | description | lastUpdate | sitecount |
||| =====================================================================================================================================================================
||| | 1 | Top/name_test/helloo | helloo | Top/name_test752 | Nuul | hello | 2010-06-19 04:10:14 | 324 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
||| | 2 | Top/name_test2/nest | nest | Top/name_test2/ | Nuul | nest | 2010-06-19 04:11:23 | 545435 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
||| | 3 | Top/name_test2/etc/57 | 5 | Top/name_test2/etc/ | Nuul | 5 | 2010-06-19 04:15:13 | 54354343 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
||| | 4 | Top/name_test2/tesla1 | tesla | Top/name_test2/ | Nuul | tesla | 2010-06-19 04:10:14 | 676756 |
||| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
|||--->| 6 | Top/name_test2 | name_test2 | Top | Nuul | name_test2 | insert data | 0 |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
|----->| 7 | Top/name_test2/etc/5 | 5 | Top/name_test2/etc | Nuul | 5 | insert data | 0 |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
|----->| 8 | Top/name_test2/tesla | tesla | Top/name_test2 | Nuul | tesla | insert data | 0 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Data from "targetCategory" wil goo to "topic"
data from "title" goo to "topicShort"
Data from "parentTopic" goo to "parentTopic"
I ned insert new data to column "displayName" well be "Nuul"
I ned insert new data to column "description" well be idem "topicShort"
I ned insert new data to column "displayName" well be "Nuul"
I ned insert new data to column "lastUpdate" well be "data time inserition"
I ned insert new data to column "displayName" well be "0"
condition its column targetCategory data like Top/name_test2 and like Top/name_test2/%
最佳答案
尚不完全清楚你想要什么,但你可以使用INSERT ... SELECT
连同适当的string functions 。例如,简单地从 column2
中删除第一个冒号之前的所有内容:
INSERT INTO table_exemple3 (column4, column5, column6)
SELECT column1, SUBSTRING(column2, LOCATE(':', column2) + 1), column3
FROM table_exemple2
查看 sqlfiddle .
关于mysql - 如何使用mysql中的variable选项将表中某一列的多条记录复制到列中的另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996649/