New to MySQL (coming from Oracle), and I have this UPDATE :
UPDATE the_table
SET the_col = 'the_value',
the_col2 = CASE WHEN the_col = 'the_value' THEN 'x' ELSE 'y' END
WHERE a = 1;
The idea is to set the_col2 to 'x' if the_col is set to 'the_value', otherwise set it to 'y'.
What I am seeing is the_col being updated fine, but the_col2.
I am not getting an error reported so I am assuming the syntax/usage is OK.
It is as if the "SET the_col = 'the_value'" sets the value so that when referenced in the CASE it has the new value. I think this is unlikely, but this seems to be what is happening.
What I want to do is test the column's value pre-update.
I don't think that the_col has the new value when you do the set the_col2 case...
But one way to find out - run the update twice, doing the_col, then the_col2. Compare answers (you'll have to - should - make a temporary table to be safe. Note: to create a temporary test table (since you're new to MySQL and it might be different), a) run DESCRIBE your_table; b) use the values in that to CREATE TABLE temp_table; c) create a subquery to read out all the rows in your_table and insert them into temp_table - e.g. INSERT into temp_table values (select * from your table). Good luck.
Related
I am trying to add new column and wants update its value based on some condition but it does not allow me to do that it says "You can't specify target table 'a' for update in FROM clause"
Any Idea, how to do that?
mysql
ALTER TABLE test ADD COLUMN step1_pincode VARCHAR(20);
UPDATE test a SET a.step1_pincode =
(
SELECT CASE WHEN b.RetailerPincode IS NOT NULL
THEN RetailerPincode
ELSE b.StorePartyPincode
END AS step1_pincode1
FROM test b
);
In MySQL, you cannot specify the same table in SET clause during UPDATE. Moreover, you don't really need a subquery in your case; you can simply use the conditional CASE .. WHEN expression directly. Try the following:
UPDATE test a
SET a.step1_pincode = CASE WHEN a.RetailerPincode IS NOT NULL
THEN a.RetailerPincode
ELSE a.StorePartyPincode
END
As #TimBiegeleisen rightly suggested in comments, you can actually write this query using COALESCE() function, in a concise manner:
UPDATE test
SET step1_pincode = COALESCE(RetailerPincode, StorePartyPincode)
Here is my query (used in a TRIGGER):
update user_details
set views_profile = views_profile + 1
where user_id = new.user_id and not exists (
SELECT 1
FROM views_profile vp
WHERE vp.user_id = new.user_id and vp.viewer_id = new.viewer_id
)
The TRIGGER:
As you can see, my query is an UPDATE statement and the problem is, it never happens. According to some tests, the problem is related to EXISTS. When I remove it, that UPDATE happens.
Anyway, why EXISTS is true all the time? Even when there isn't any row in views_profile table?
You are using a TRIGGER with AFTER INSERT so the new line is available on the UPDATE (and can be found on the EXISTS). You can change the time to BEFORE.
Is there a way in SQL (MySQL) to increment a value, and also return the value in a single query. I am trying to ovoid doing two queries like the following:
QUERY 1
UPDATE my_table SET my_col = (my_col + 1) WHERE something = something_else;
QUERY 2
SELECT my_col FROM my_table WHERE something = something_else;
Thanks.
To my knowledge there is still no such possibility in MySQL, but take a look at this question for a possible workaround that at least lets you have the select and update work with the same data transactionally.
There is no way to make a select and a update at the same time.
If you want to avoid the select you can declare a variable and put there the value, but that will put the last updated row value in the variable.
declare #value int
UPDATE my_table SET my_col = (my_col + 1), #value = (my_col + 1) WHERE something = something_else;
I don't know what scripting language you are using but here is an example on creating a stored procedure in MySQL that returns the updated value so you can update and select in one operation:
Get Updated Value in MySQL instead of affected rows
UPDATE items SET name = 'haha' WHERE id = '12'
I'm curious if update also inserts the values if the where condition fails. I've read on w3schools that update only updates existing data on the database but on my script it's automatically inserting rows with the data. I am wondering if it might be a bug in the script or that's just how UPDATE works on mysql.
No. If, in your example, there's no entry with id = 12 in the database, the query will return "no rows affected". An update will never create a new entry in MySQL.
EDIT: although update won't create a new entry, it may include default/automatic values set up in your database schema (current timestamp, for instance).
NO. Update does not insert a value if the value doesn't exist in table. Please check if the script checks if the status of the update and makes another call to DB to insert the data.
Your SQL should do the following -
Update all records in the items table that have an id of 12 by setting their name to 'haha'
Update won't insert records if they don't exist, it will only update existing records in the table.
Short answer: No.
Long Answer: If your column doesn't exist you will get an error. If your where condition column doesn't exist you get error too. If your where condition value doesn't exist, it do nothing.
I use a temp table to review the data update conditions, you can refer
UPDATE table1
SET
column1 = 'things'
WHERE
IDcolumn = 'id' AND
(NOT EXISTS (SELECT * FROM (SELECT * FROM table1) AS temp WHERE temp.column1 = N'things'))
I've got a classic case of UPDATE or INSERTing some data into a table. I'm not sure if I should just do an UPDATE and if i get zero ROWCOUNT, then do an INSERT. Alternatively, I've heard rumours that the MERGE statement now replaces this, but I'm not sure how and if it's appropriate, in this situation.
Here's some sample sql to help demonstrate this...
ALTER PROCEDURE [dbo].[InsertLocationName]
(
#SomeId INTEGER,
#SomeName NVARCHAR(100)
)
AS
BEGIN
SET NOCOUNT ON
UPDATE TableFoo
SET SomeName = #SomeName
WHERE SomeId = #SomeId
-- Did we update something?
IF ##ROWCOUNT <= 0
-- Nope, so add the record.
INSERT INTO TableFoo
VALUES (#SomeName)
END
thoughts?
Sure - the MERGE syntax is probably the easiest. You basically need:
a target table to update
a source table to read from
a JOIN condition
a bunch of statement to execute for matched or non-matched rows.
So it basically looks something like this:
MERGE TableFoo as t
USING TableFooSource as s
ON t.SomeID = s.SomeID
WHEN MATCHED THEN
UPDATE SET t.SomeName = s.SomeName
WHEN NOT MATCHED THEN
INSERT(SomeName) VALUES(s.SomeName)
;
Don't forget the semicolon at the end!!
Marc
PS: Updated to use your table and field names. The point here is - the set of data used to be updated needs to be in a source table of its own (if needed, bulk-import that from e.g. an external file) and then the whole operation (all INSERTs and UPDATEs) are done in a single SQL statement.
Bit more of an explanation here: http://www.databasejournal.com/features/mssql/article.php/3739131/UPSERT-Functionality-in-SQL-Server-2008.htm