简体   繁体   English

将列值复制到同一表中的另一列

[英]Copy column values to another column in the same table

I'm trying to copy title column to keywords column in database, so the keywords will be inserted automatically from the title. 我正在尝试将标题列复制到数据库中的关键字列,因此将自动从标题中插入关键字。

http://store2.up-00.com/2015-06/1435609110941.png http://store2.up-00.com/2015-06/1435609110941.png

I want to add comma ', ' before each word for example. 例如,我想在每个单词前添加逗号','。

" It's my first program "   

it will turn into 它会变成

" It's, my, first, program, "

This the code I wrote. 这是我写的代码。

<?php

  // $id =mysql_insert_id;
  $select_posts = mysql_query("SELECT * FROM `posts`");

  while($row = mysql_fetch_array($select_posts)){
        $id  = $row['post_id'];
        $text =  $row['post_title'];  

       $delim = ' \n\t,.!?:;';
       $tok = strtok($text, $delim);


    while ( $tok !== false){
          echo $tok1 = $tok.',';
          mysql_query("UPDATE `posts` SET  `post_keywords` =  '$tok1' WHERE `post_id` = $id  ");
          $tok = strtok($delim);
        }   
}

?>    

it insert the last word in each title column , because the words is overwritten by while loop. 它在每个标题列中插入最后一个单词,因为单词被while循环覆盖。

Please help me . 请帮我 。

Concat the values: 合并值:

... SET post_keywords = CONCAT(post_keywords, '$tok1')

and note that you're vulnerable to sql injection attacks . 并请注意,您容易受到sql注入攻击的攻击 Just because that $tok1 value came out of a database doesn't mean it's safe to REUSE in a query... 仅仅因为$ tok1值是从数据库中发出的,并不意味着在查询中重用是安全的...

您可以通过一个查询来完成:

UPDATE `posts` SET post_keywords = REPLACE(post_title, ' ', ',');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用updateAll()使用同一个表的其他列值更新表列 - Update table column with another column values of the same table using updateAll() 从一个表复制到另一个具有相同列数和名称的表 - copy from one table to another with the same column count and name 根据同表 sql 和 php 另一列的更改更新表中的列值 - Update column values in table based on changes on another column same table sql and php SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column 如何从一个表数据复制列到另一表并在mysql中同时插入更多列? - how to copy column from one table data to another table and insert more columns same time in mysql? 如何根据同一表中另一列的值获取列的所有值? - How to fetch all values of a column based on value of another column in same table? 在同一表的同一列中回显不同的值 - Echo different values in the same column in the same table SQL复制数据从一列到另一个表指定列 - SQL copy data from one column to another table specified column 创建记录时,Mysql触发器将id从一列复制到同一表中的另一列 - Mysql trigger to copy id from one column to another in same table when record is created MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM