5

How do I add a value to the current version with SQL UPDATE

 3 years ago
source link: https://www.codesd.com/item/how-do-i-add-a-value-to-the-current-version-with-sql-update.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I add a value to the current version with SQL UPDATE

advertisements

I am creating an access log on my website, for whenever a person entering the site, he set the date and time whenever in the same registry in the database. however, instead of deleting the last visit, he added with the other. exemple:

this is the current value: 2014-01-31 17:18:27

this is the new value: 2014-02-01 17:18:27

if I use the UPDATE method, it will replace the current value to the new. And what I would like to stay was as follows: 2014-01-31 17:18:27, 2014-02-01 17:18:27

let's be a little closer to what I'm doing.

this is the function i'm creating:

function functionInd() {
    $this = 'this';
    $conn = db();
    $prepare = $conn->prepare('
    UPDATE table
    SET dates=:date
    WHERE this=:this');
    $prepare->bindParam(':date',date("Y-m-d H:i:s"));
    $prepare->bindParam(':this', $this, PDO::PARAM_STR);
    $prepare->execute();
    disconnectBase($conn);
}

What i need to do?


You don't want to do this. Storing lists in SQL is not the right approach to using a database. You need a table that stores the dates in a separate table, with one row per this per date. If you had such a table, your code would be easy:

insert into ThisDateTable(this, date)
    select :this, :date;

That said, if you still insist on storing lists in a field -- and all the subsequent delays and problems this will cause you -- the syntax is one of the following:

set date = concat(date, ',' :date)

set date = date || ',' || :date

set date = date + ',' + :date

set date = date & "," & :date

The first uses the ANSI-standard function and is supported by MySQL, Oracle, SQL Server 2012, Postgres, DB2, and Teradata (and perhaps others).

The second uses the ANSI-standard operator and is supported by several databases, notably Oracle and Postgres (and probably others).

The third is "t-sql" format, and is used by SQL Server (all versions) and Sybase.

The fourth is used by Access and is highly non-standard, both in the use of & and the use of " to delimit strings.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK