sql-update

DB2 SQL update correlating with subquery

跟風遠走 提交于 2019-12-25 03:06:05
问题 Code block: update yrb_purchase px set px.club = (select club from (select p.title, p.year, o.club, o.price, ROW_NUMBER() OVER(PARTITION BY p.title, p.year ORDER BY o.price ) rn from yrb_purchase inner join yrb_offer o on p.title = o.title and p.year = o.year inner join yrb_member m on m.club = o.club inner join yrb_customer c on c.cid = p.cid and c.cid = m.cid where p.cid = px.cid and p.title = px.title and p.year = px.year order by title ) where rn = 1 ) where .... My issue is thus: upon

How do I update multiple Entity models in one SQL statement?

不羁的心 提交于 2019-12-25 02:57:18
问题 I had the following: List<Message> unreadMessages = this.context.Messages .Where( x => x.AncestorMessage.MessageID == ancestorMessageID && x.Read == false && x.SentTo.Id == userID ).ToList(); foreach(var unreadMessage in unreadMessages) { unreadMessage.Read = true; } this.context.SaveChanges(); But there must be a way of doing this without having to do 2 SQL queries, one for selecting the items, and one for updating the list. How do i do this? 回答1: Current idiomatic support in EF As far as I

What's wrong with my MySQL statement?

好久不见. 提交于 2019-12-25 02:44:37
问题 UPDATE table1 SET announcer = ( SELECT memberid FROM ( table1 JOIN users ON table2.username = table1.announcer ) AS a WHERE a.username = table1.announcer ) #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where a.username=table1.announcer)' at line 1 回答1: Try: UPDATE announcements a SET announcer = (SELECT memberid FROM users u WHERE u.username = a.announcer) 回答2: You can also do the JOIN in the

Update statement in sql server 2005

邮差的信 提交于 2019-12-25 02:44:23
问题 Consider the following Dig, Assume that all the three tables have a column Is_Deleted by default it is set to 0 ... I want to update Is_Deleted=1 field of Customers table where CustId=2 only when the rows containing CustId=2 and Is_Deleted=1 in Orders and OrderItems Tables... I dont want to use Cascade option.. Any suggestion (source: microsoft.com) 回答1: Easiest way is EXISTS. I assume you want to check both Orders and OrderItems. This also means you only filter on CustID once. UPDATE C SET

Adding on to the current value SQLite

∥☆過路亽.° 提交于 2019-12-25 02:16:46
问题 I have a user who has a starting value for their credits set at '5'. Now I want to create a method which will allow the user to add on to that number, e.g. 2. This will then update the row in the database column to become 7. However I cant get my current method to do this. Current method: public void upDateUser(String money) { String selectQuery = "SELECT " + KEY_CREDITS + " from " + DATABASE_TABLE + " where " + KEY_ROWID + " = " + 0; SQLiteDatabase db = this.getWritableDatabase(); Cursor c =

Create table from SQL query

烈酒焚心 提交于 2019-12-25 01:56:17
问题 This is one annoying issue and I can't figure out how to solve it. I'm Using Microsoft SQL Server 2008. So I have two tables and I need to update both of them. They share a common key, say id. I want to update Table1 with some stuff and then update the Table2 rows which were respectively modified in Table1 . The issue is that I don't quite know which rows were modified, because I'm picking them randomly with ORDER BY NEWID() so I probably cannot use a JOIN on Table2 in any way. I am trying to

Concurrency Postgresql UPDATE

为君一笑 提交于 2019-12-25 00:15:02
问题 I have this situation with thread process. Have table like example : id | type | external_key | balance | amount | date 1 TOPUP MT1 10 2019-01-1 13:01:00.500110 2 USAGE MT1 -1 2019-01-1 13:01:01.300100 3 TOPUP MT3 5 2019-01-1 13:01:02.400300 every new data on each row i run this trigger to update field balance to store current balance every row : create or replace function function_update_balance() returns trigger language plpgsql as $$ BEGIN IF new.type = 'CBA_ADJ' THEN update example set

Conditional update_or_create with django

柔情痞子 提交于 2019-12-24 22:23:04
问题 Test model class Room(models.Model): """ This stores details of rooms available """ name = models.CharField( null=False, blank=False, max_length=100, help_text='Enter the name of the room' ) capacity = models.PositiveIntegerField( null=False, blank=False, help_text='Enter the number of person\'s the room can accommodate' ) created_at = models.DateTimeField(auto_now_add=True) modified_at = models.DateTimeField(auto_now=True) I want to update the model if it exists and its modified_at time is

Trigger default value of a column during UPDATE

旧巷老猫 提交于 2019-12-24 17:14:40
问题 I am wondering about this case with Oracle 12c; Table X has column A, with default value 'default' and NOT NULL modifier I insert a new row into Table X, and column A has value 'not-default' I wish to update column A of the above row to the default value of the given column, namely 'default' Is there a short way of doing this without knowing the default value? Can I do something like; UPDATE X SET A = DEFAULT_VAL(A) WHERE ... Trying to update to null obviously triggers a ORA-01407: cannot

update a column which already added to table

佐手、 提交于 2019-12-24 16:29:51
问题 Summary: assume we have table1 and I want to add a column named column1 to this table. and after that update column1 's value. note that all I said are happening in IF block. Question: how can I update column1 value without any error? IF NOT EXISTS ( SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[table1]') AND name = 'column1' ) BEGIN ALTER TABLE table1 ADD column1 BIT NULL UPDATE table1 SET table1.column1=0 END The error is: Msg 207, Level 16, State 1, Line 245 Invalid column