sql-update

Postgres update from left join

人盡茶涼 提交于 2019-12-21 07:36:52
问题 I'm new in PostgreSQL and trying to convert a query from SQL Server. I have a table Users with, among others, the columns bUsrActive, bUsrAdmin and sUsrClientCode. I want to update Users and set bUsrActive = false if there does not exist a another user with the same sUsrClientCode where bUsrAdmin = true and bUsrActive = true. In SQL Server I have this query UPDATE u SET u.bUsrActive = 0 FROM Users u LEFT JOIN Users u2 ON u.sUsrClientCode = u2.sUsrClientCode AND u2.bUsrAdmin = 1 AND u2

Update top N values using PostgreSQL

你说的曾经没有我的故事 提交于 2019-12-21 07:19:15
问题 I want to update the top 10 values of a column in table. I have three columns; id , account and accountrank . To get the top 10 values I can use the following: SELECT * FROM accountrecords ORDER BY account DESC LIMIT 10; What I would like to do is to set the value in accountrank to be a series of 1 - 10 , based on the magnitude of account . Is this possible to do in PostgreSQL? 回答1: WITH cte AS ( SELECT id, row_number() OVER (ORDER BY account DESC NULLS LAST) AS rn FROM accountrecords ORDER

MySQL - How do I update the decimal column to allow more digits?

我是研究僧i 提交于 2019-12-21 07:14:41
问题 I'm a beginner in MySQL, and I accidentally created a table with a column named (price decimal(2,2)); It needs to be decimal(4,2) to allow 4 digits. Since I've already created it, what is the easiest way to update that decimal value to decimal(4,2) ? Or do I need to drop that column completely, and re-create it with the correct numbers? I can't seem to get the syntax right. Thank you very much. 回答1: ALTER TABLE mytable MODIFY COLUMN mycolumn newtype example: ALTER TABLE YourTableNameHere

Do sqlite triggers trigger other triggers?

大憨熊 提交于 2019-12-21 06:23:01
问题 I am trying to implement the equivalent of the "ON UPDATE CURRENT_TIMESTAMP" MySQL feature in sqlite. My idea it to use a trigger like this: CREATE TRIGGER last_update_trigger AFTER UPDATE ON mytable FOR EACH ROW BEGIN UPDATE mytable SET last_update = CURRENT_TIMESTAMP WHERE id = old.id; END But there's a problem with this. Each time an update occurs on a record of this table, the trigger triggers a new update on this same record. This should trigger the trigger again, and again, leading to

Do sqlite triggers trigger other triggers?

瘦欲@ 提交于 2019-12-21 06:21:29
问题 I am trying to implement the equivalent of the "ON UPDATE CURRENT_TIMESTAMP" MySQL feature in sqlite. My idea it to use a trigger like this: CREATE TRIGGER last_update_trigger AFTER UPDATE ON mytable FOR EACH ROW BEGIN UPDATE mytable SET last_update = CURRENT_TIMESTAMP WHERE id = old.id; END But there's a problem with this. Each time an update occurs on a record of this table, the trigger triggers a new update on this same record. This should trigger the trigger again, and again, leading to

Creating SQL batch updates from within Java

给你一囗甜甜゛ 提交于 2019-12-21 06:07:09
问题 I want to update every row on a specific column in a mySql database. Currently I am using a java.sql.PreparedStatement for each row and iterating in a for loop. I was wondering if there were any other alternatives in terms of Java programming to make this less time and resource consuming (something like executing the prepared statements in a batch). The updates are made from Java code because that is where I get the values from. I am also not interested in making stored procedures on the

How can I receive an e-mail when my MySQL table is updated?

陌路散爱 提交于 2019-12-21 05:36:06
问题 Hi I was wondering if there was a way in MySQL to automatically send an e-mail to myself whenever there is a row added to a MySQL table? 回答1: The best way to achieve this would be using a trigger and a cron. Create a 'notification queue' table and populate that with a trigger when a row is inserted in the desired table. eg. CREATE TABLE `notification_queue` ( `notification_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `sent` tinyint(1) unsigned NOT NULL, PRIMARY KEY (`notification_id`) );

NHibernate update on single property updates all properties in sql

安稳与你 提交于 2019-12-21 03:41:58
问题 I am performing a standard update in NHibernate to a single property. However on commit of the transaction the sql update seems to set all fields I have mapped on the table even though they have not changed. Surely this can't be normal behaviour in Nhibernate? Am I doing something wrong? Thanks using (var session = sessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { var singleMeeting = session.Load<Meeting>(10193); singleMeeting.Subject = "This is a test 2";

UPDATE-no-op in SQL MERGE statement

孤街醉人 提交于 2019-12-21 03:17:13
问题 I have a table with some persistent data in it. Now when I query it, I also have a pretty complex CTE which computes the values required for the result and I need to insert missing rows into the persistent table. In the end I want to select the result consisting of all the rows identified by the CTE but with the data from the table if they were already in the table, and I need the information whether a row has been just inserted or not. Simplified this works like this (the following code runs

UPDATE-no-op in SQL MERGE statement

拟墨画扇 提交于 2019-12-21 03:17:06
问题 I have a table with some persistent data in it. Now when I query it, I also have a pretty complex CTE which computes the values required for the result and I need to insert missing rows into the persistent table. In the end I want to select the result consisting of all the rows identified by the CTE but with the data from the table if they were already in the table, and I need the information whether a row has been just inserted or not. Simplified this works like this (the following code runs