mysql REPLACE INTO and optional values IFNULL - mysql

I'm trying to do something like this inside of a stored procedure:
REPLACE INTO mytable
SET myid = `IDvalue`, mytitle = `sMyTitle`, myoptionalvalue = IFNULL(`sMyOptValue`, myoptionalvalue);
But not seems to work, any idea how to do this?
Thanks!

The REPLACE INTO syntax works exactly like INSERT INTO except that any old rows with the same primary or unique key is automaticly deleted before the new row is inserted.
This means that instead of a WHERE clause, you should add the primary key to the values beeing replaced to limit your update.
REPLACE INTO myTable (
myPrimaryKey,
myColumn1,
myColumn2
) VALUES (
100,
'value1',
'value2'
);
...will provide the same result as...
UPDATE myTable
SET myColumn1 = 'value1', myColumn2 = 'value2'
WHERE myPrimaryKey = 100;

Related

update table with history keeper of the updated records in a new column

I need to log the updated columns into an existing empty column in which it should appear "Updated" or "Not Updated". I am running this query in MS Access.
Below find an example for my update query (which works) and code for the trigger (not sure i need one, or if i'm using it correctly)
update my_table
set col_i_need_to_set = 'value'
WHERE another_col like 'some_text'
and another_col2 LIKE 'some_other_text'
and another_col3 LIKE 'text'
CREATE OR REPLACE TRIGGER historyKeeper
AFTER UPDATE my_table
FOR EACH ROW
BEGIN
IF( UPDATING( 'col_i_need_to_set' ) )
THEN
INSERT INTO my_table( column_in_which_i_want_to_insert, column_value )
VALUES( 'Updated');
else
INSERT INTO my_table( column_in_which_i_want_to_insert, column_value )
VALUES( 'Not updated');
END IF;
END;
Thank you
actually, found the solution. after set col_i_need_to_set = 'value' I just need to add , column_in_which_i_want_to_insert = 'UPDATED' Maybe this will help someone

Substring Binary Data in MYSQL

I have a binary column in my table in mysql. I need to update all the rows for example.
Replace 2nd byte with 1. Doesn't matter what the value was.
SELECT HEX(data) FROM table;
Then
UPDATE table SET data[1] = 1; // the idea
But how do you do something like this in mysql?
Here how you can do it. First get the binary representation with hex function.
Then on the returned value use substring and concat function to replace the 2nd value with 1 and finally update.
Here is an example.
CREATE TABLE t (c BINARY(10));
INSERT INTO t SET c = 'b';
select (HEX(c)) from t;
62000000000000000000
SELECT concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
from t
61000000000000000000
Now putting all together in the update command will replace the value
update t set c = unhex(
concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
);
select (HEX(c)) from t;
61000000000000000000

operation on column in SQL

I have a table in SQL database, where one column contains the character "C" or "P".
I want to define a new column, where the value is 1, if this character is "C" and zero otherwise.
kind regards,
Daniel
if you want to define through projection, try
SELECT columnName, IF(columnName = 'C', 1, 0) newValue
FROM tableName
As an alternative suggested by Michael Berkowski,
SELECT columnName, (columnName = 'C') newValue
FROM tableName
SELECT
your_fields,
IF(your_column = 'C', 1, 0) AS new_field
FROM your_table
Do you want it to be a permanent column? If so, create a new column in your table with a default value of 0. Then issue an update statement as follows:
UPDATE tableName SET newColumn = 1 WHERE oldColumn = 'C';
ALTER MY_TABLE ADD NEW_COLUMN INT
UPDATE MY_TABLE SET NEW_COLUMN=1 WHERE OTHER_COLUMN='C'
UPDATE MY_TABLE SET NEW_COLUMN=0 WHERE OTHER_COLUMN='P'
INSERT
WHEN Col_name1='c' THEN
INTO your_Table (Col_name2)
VALUES (1)
WHEN Col_name1='P' THEN
INTO your_table (Col_name2)
VALUES (0);
SELECT * FROM your_Table;
It will work.

MySQL, how update string field by concatenating to it?

Question is how to update string field by concatenating to it?
Here is code for creating database and adding one row:
CREATE DATABASE my_db;
USE my_db;
CREATE TABLE IF NOT EXISTS my_table(
article_id INTEGER unsigned,
revision_ids VARCHAR(10),
PRIMARY KEY (article_id)
);
INSERT INTO my_table (article_id, revision_ids) VALUES (1, "154");
I need to write code which concatenate string to revision_ids field. For example, I need to concatenate ", 999" to "154", so I will get "154, 999". My version of code does not work:
UPDATE my_table SET revision_ids = CONCAT((SELECT revision_ids FROM my_table WHERE article_id = 1), ", ", "999") WHERE article_id = 1;
How to do it?
There is one important condition. Theoretically this concatenation could do several scripts, so it is important that while our updating nobody can change the field value.
This should be all you need:
UPDATE my_table SET revision_ids = CONCAT(revision_ids, ", ", "999") WHERE article_id = 1;
I gues you can do this directly,
UPDATE my_table
SET revision_ids = CONCAT(revision_ids, ', ', '999')
WHERE article_id = 1;
Use:
UPDATE my_table SET revision_ids = CONCAT(revision_ids, ", 999") WHERE article_id = 1;

Addition in MySQL trigger not working as expected

I'm doing some addition as part of a mysql trigger. The added value is added a to a column on the table. The code is as follows;
BEGIN
IF NEW.Status = 'processed' AND NEW.Success = 1 THEN
INSERT INTO crm_master
(msisdn, last_action_date, source, contract_type, revenue,inc)
VALUES
(new.msisdn,NOW(), 'INC5', new.Contract_Type, revenue = revenue+5, 1)
ON DUPLICATE KEY UPDATE last_action_date = NOW(),
contract_type = new.Contract_Type,
revenue = revenue+5,
inc = 1;
END IF;
END
The column revenue in the table crm_master is set to default of 0
The problem is that I'm getting unexpected results with incorrect values and in some cases 0 even though there should never be a 0 value.
I don't think it's valid reference to default value revenue = revenue+5 in your insert statement. It should look like
INSERT INTO crm_master
(msisdn, last_action_date, source, contract_type, revenue,inc)
VALUES
(new.msisdn,NOW(), 'INC5', new.Contract_Type, DEFAULT(revenue) +5, 1)
ON DUPLICATE KEY UPDATE ....
Or you can simply do
INSERT INTO ....
VALUES
(new.msisdn,NOW(), 'INC5', new.Contract_Type, 5, 1) ...
*Your update part of INSERT ... ON DUPLICATE KEY UPDATE is ok.
INSERT INTO sometable( column ) VALUES ( column = column + 5 );
is equivalent to
INSERT INTO sometable( column ) VALUES ( 0 );
because (column = column+5) evaluates to 0 as column is never equal to column+5.
This syntax is ok for UPDATE query, but for INSERT you should provide an explicit value, like
INSERT INTO sometable( id, column ) VALUES ( 1, 5 )
ON DUPLICATE KEY UPDATE column = column + 5;
which would insert value 5 if there is no row with given id and add 5 to column if there is one.