How can I add text to SQL Column

前端 未结 5 1103
盖世英雄少女心
盖世英雄少女心 2021-02-14 17:15

I want to update 1 column in SQL Table. Example: Current value in column is like this

2013/09/pizzalover.jpg 
2013/10/pasta.jpg       

Now i wa

相关标签:
5条回答
  • 2021-02-14 17:23

    You can simply update column using statement

    update TableName set ColumnName  = 'www.mypizza.com/' + ColumnName  
    
    0 讨论(0)
  • 2021-02-14 17:27

    OP doesn't specify which DBMS they are using. The following is for Postgres to update a text column by adding a prefix to it (tested with PostgreSQL v11):

    UPDATE my_table 
    SET column_1  = 'start_text_' || column_1
    WHERE column_1 LIKE 'prefix_%'
    ; 
    
    0 讨论(0)
  • 2021-02-14 17:29

    If you are using MYSql, you can use the concat() as :

    update tableName set columnName= CONCAT('www.mypizza.com/', columnName);
    

    SQLFiddle

    If you are using oracle you can use the concatenation operator '||' as :

    update tableName set "columnName"='www.mypizza.com/'||"columnName";
    

    SQLFiddle

    In SQL Server you can use + for string concatenation as:

    update tableName set name='www.mypizza.com/'+columnName;
    

    SQLFiddle

    0 讨论(0)
  • 2021-02-14 17:39

    You mean like this?:

    SELECT 'www.mypizza.com/' + ColumnName AS ColumnName FROM TableName
    

    Depending on the rest of your application environment, there is likely a much better way to accomplish this. But in terms of just using SQL to add static text to a column in a SELECT statement, you can just concatenate the text directly in the statement.

    Or, if you wanted to UPDATE the column values, something like this:

    UPDATE TableName SET ColumnName = 'www.mypizza.com/' + ColumnName
    

    Same principle, just using an UPDATE instead of a SELECT, which will modify the underlying data instead of just modifying the view of the data.

    0 讨论(0)
  • 2021-02-14 17:44

    First get the information stored in your database and then edit it, you can do that like this:

    <?php 
    $query = "SELECT * FROM  `blog-posts`  WHERE `id` = 11";
    $result = mysql_query($query);
    $post = mysql_fetch_array($result);
    $title = $post['title'];
    $title .= "aapje";
    echo $title
    ?>
    

    And then update your database like normal:

    $updateq = "UPDATE `blog-posts`  SET `title` = '$title' WHERE `id` = 11";
    
    0 讨论(0)
提交回复
热议问题