insert-update

Update the value of a field in database by 1 using codeigniter

喜你入骨 提交于 2019-11-27 06:40:55
问题 I want to implement a SQL statement using codeigniter active record. UPDATE tags SET usage = usage+1 WHERE tag="java"; How can I implement this using Codeigniter active records? Regards 回答1: $this->db->set('usage', 'usage+1', FALSE); $this->db->where('tag', 'java'); $this->db->update('tags'); 回答2: You can also use something like this $data = array('usage' => 'usage+1', *other columns*); $this->db->where('tag', 'java'); $this->db->update('tags', $data); UPDATE: $data was not being passed on to

.NET Dictionary: get or create new

二次信任 提交于 2019-11-27 01:34:24
问题 I often find myself creating a Dictionary with a non-trivial value class (e.g. List), and then always writing the same code pattern when filling in data. For example: var dict = new Dictionary<string, List<string>>(); string key = "foo"; string aValueForKey = "bar"; That is, I want to insert "bar" into the list that corresponds to key "foo" , where key "foo" might not be mapped to anything. This is where I use the ever-repeating pattern: List<string> keyValues; if (!dict.TryGetValue(key, out

Update record with previous row

て烟熏妆下的殇ゞ 提交于 2019-11-26 23:41:10
问题 I have a situation where I need to update the records with previous row value. Source: |MatId | BaseId |Flag|Pkg1| CS1 -------------------------------- |3001 | 3001 | 1 | 20 | 2 | |3002 | 3001 | 0 | 15 | 3 | |3003 | 3001 | 0 | 10 | 4 | Here both 3001 ( MatID ) and 3001( BaseID ) are same so FLAG =1, in the next record only BASEID is same. The output should be only PKG1 field updated with the current row value. Target or output: |MatId | BaseId|Flag|Pkg1|CS1 ------------------------------

Update a column in MySQL

怎甘沉沦 提交于 2019-11-26 22:22:58
问题 I have a table table1 with three columns and a bunch of rows: [key_col|col_a|col_b] I want to update col_a with a set of values (i.e. leaving col_b unchanged), something like this: INSERT INTO table1 AS t1 (key_col, col_a) VALUES ("k1", "foo"), ("k2", "bar"); But it doesn't work, how do I do this? 回答1: You have to use UPDATE instead of INSERT: UPDATE Syntax For Example: UPDATE table1 SET col_a='k1', col_b='foo' WHERE key_col='1'; UPDATE table1 SET col_a='k2', col_b='bar' WHERE key_col='2';

MySQL trigger On Insert/Update events

老子叫甜甜 提交于 2019-11-26 18:55:10
So I have two tables like this... ext_words ------------- | id | word | ------------- | 1 | this | ------------- | 2 | that | ------------- | 3 | this | ------------- ext_words_count --------------------- | id | word | count | --------------------- | 1 | this | 2 | --------------------- | 2 | that | 1 | --------------------- I am trying to create a trigger that will: update ext_words_count.count when ext_words.word is updated. To further complicate matters, if ext_words.word does not exist in ext_words_count when ext_words is updated, I would like to insert it into ext_words_count and set

MySQL “INSERT … ON DUPLICATE KEY UPDATE” on Java: How to Differ Inserted/Updated/NoChange states

被刻印的时光 ゝ 提交于 2019-11-26 14:51:30
问题 I'm confused about the return value of MySQL's INSERT ... ON DUPLICATE KEY UPDATE statement. When I try it on a MySQL client (mysql terminal, phpmyadmin or MySQL Workbench), the execution results with one of the followings: 1 : If new record inserted (i.e. no duplicate key). 2 : If existing record updated in case of duplicate key. 0 : If update executed but no column value was changed. These results make sense. However, when I executed the same query in Java (with mysql-connector 5.1.34),

Does DB2 have an “insert or update” statement?

怎甘沉沦 提交于 2019-11-26 08:09:28
问题 From my code (Java) I want to ensure that a row exists in the database (DB2) after my code is executed. My code now does a select and if no result is returned it does an insert . I really don\'t like this code since it exposes me to concurrency issues when running in a multi-threaded environment. What I would like to do is to put this logic in DB2 instead of in my Java code. Does DB2 have an insert-or-update statement? Or anything like it that I can use? For example: insertupdate into mytable

SQL Server insert if not exists best practice

╄→гoц情女王★ 提交于 2019-11-26 00:32:03
问题 I have a Competitions results table which holds team member\'s names and their ranking on one hand. On the other hand I need to maintain a table of unique competitors names : CREATE TABLE Competitors (cName nvarchar(64) primary key) Now I have some 200,000 results in the 1st table and when the competitors table is empty I can perform this: INSERT INTO Competitors SELECT DISTINCT Name FROM CompResults And the query only takes some 5 seconds to insert about 11,000 names. So far this is not a

How to UPSERT (MERGE, INSERT … ON DUPLICATE UPDATE) in PostgreSQL?

送分小仙女□ 提交于 2019-11-25 21:41:50
问题 A very frequently asked question here is how to do an upsert, which is what MySQL calls INSERT ... ON DUPLICATE UPDATE and the standard supports as part of the MERGE operation. Given that PostgreSQL doesn\'t support it directly (before pg 9.5), how do you do this? Consider the following: CREATE TABLE testtable ( id integer PRIMARY KEY, somedata text NOT NULL ); INSERT INTO testtable (id, somedata) VALUES (1, \'fred\'), (2, \'bob\'); Now imagine that you want to \"upsert\" the tuples (2, \'Joe

Insert into a MySQL table or update if exists

為{幸葍}努か 提交于 2019-11-25 21:39:49
问题 I want to add a row to a database table, but if a row exists with the same unique key I want to update the row. For example, insert into table (id, name, age) values(1, \"A\", 19) Let’s say the unique key is id , and in my database there is a row with id = 1 . In that case I want to update that row with these values. Normally this gives an error. If I use insert IGNORE it will ignore the error, but it still won’t update. 回答1: Use INSERT ... ON DUPLICATE KEY UPDATE QUERY: INSERT INTO table (id