SQL update fields of one table from fields of another one
You can use the non-standard FROM clause. UPDATE b SET column1 = a.column1, column2 = a.column2, column3 = a.column3 FROM a WHERE a.id = b.id AND b.id = 1
You can use the non-standard FROM clause. UPDATE b SET column1 = a.column1, column2 = a.column2, column3 = a.column3 FROM a WHERE a.id = b.id AND b.id = 1
Simply increment the value that already exists in the database $sql = “UPDATE member_profile SET points = points + 1 WHERE user_id = ?”; $db->prepare($sql)->execute([$userid]); This code would work for both PDO and mysqli in the modern PHP versions
No special syntax: CREATE TABLE your_table (some_id int, your_column varchar(100)); INSERT INTO your_table VALUES (1, ‘Hello’); UPDATE your_table SET your_column = NULL WHERE some_id = 1; SELECT * FROM your_table WHERE your_column IS NULL; +———+————-+ | some_id | your_column | +———+————-+ | 1 | NULL | +———+————-+ 1 row in set (0.00 sec)
UPDATE t1 LEFT JOIN t2 ON t2.id = t1.id SET t1.col1 = newvalue WHERE t2.id IS NULL Note that for a SELECT it would be more efficient to use NOT IN / NOT EXISTS syntax: SELECT t1.* FROM t1 WHERE t1.id NOT IN ( SELECT id FROM t2 ) See the article in my blog … Read more
You use the sql%rowcount variable. You need to call it straight after the statement which you need to find the affected row count for. For example: set serveroutput ON; DECLARE i NUMBER; BEGIN UPDATE employees SET status=”fired” WHERE name LIKE ‘%Bloggs’; i := SQL%rowcount; –note that assignment has to precede COMMIT COMMIT; dbms_output.Put_line(i); END;
If you fetch an item and then update it, there may be an update by another thread between those two steps. If you update an item first and then fetch it, there may be another update in-between and you will get back a different item than what you updated. Doing it “atomically” means you are … Read more
According to SQLite Update Docs : <!– language: lang-java –> @Query(“UPDATE tableName SET field1 = :value1, field2 = :value2, … //some more fields to update … field_N= :value_N WHERE id = :id) int updateTour(long id, Type value1, Type value2, … , // some more values here … , Type value_N); Example: Entity: @Entity(tableName = “orders”) … Read more
You can do it this way: UPDATE table_users SET cod_user = (case when user_role=”student” then ‘622057’ when user_role=”assistant” then ‘2913659’ when user_role=”admin” then ‘6160230’ end), date=”12082014″ WHERE user_role in (‘student’, ‘assistant’, ‘admin’) AND cod_office=”17389551″; I don’t understand your date format. Dates should be stored in the database using native date and time types.
Try this: UPDATE table1 SET a = t2.a, b = t2.b, ……. FROM table2 t2 WHERE table1.id = t2.id That should work in most SQL dialects, excluding Oracle. And yes – it’s a lot of typing – it’s the way SQL does this.
If you’re able to upgrade to Postgresql 9.5, the jsonb_set command is available, as others have mentioned. In each of the following SQL statements, I’ve omitted the where clause for brevity; obviously, you’d want to add that back. Update name: UPDATE test SET data = jsonb_set(data, ‘{name}’, ‘”my-other-name”‘); Replace the tags (as oppose to adding … Read more