Trim leading and update in mysql - mysql

I wanted to create a query in MYSQL that would trim the leading '# ' from all of the fields in a column.
update values set value = TRIM(LEADING '# ' from value)
However, doing this gives me an error
Duplicate entry '3002' for key value
The value column has a unique constraint and the error probably occurs because the query is trying to set the same value to all of value column after trimming.
Is there a way to do trim leading and update in mysql?

This query looks fine.
The issue might be here. Let me give you an example.
Case 1:
At row-x, you have value '#info' and at row-y you have info.
You removed the # from row-x in your query. Now you have already info value at row-y. You can not update the new value to info as it is there already.
I would suggest either to remove the UNIQUE constraint or you do not update the database itself. You can trim with your backend programming.

This is not setting all values to the same value, this is where once trimmed you now have two different rows with the same value for that column.
To find this:
SELECT id, value, TRIM(LEADING '# ' from value) AS trimmed_value ORDER BY trimmed_value
Presuming you have some kind of id column you'll be able to find any rows where trimmed_value is identical.

Related

Sql replace is setting other value to null

Hey i'm trying to do some sql request but got a problem.
Firstly i do a replace request who filled my database.
After i do an update request that update some values.
And i'm doing another replace which is used to avoid duplicates, however some fields that were updated just before going to null because of the delete that is done before the insert (replace case)
How can I proceed so that the replace avoids putting some attribute to null and keep the values from the update?
Here is the sequence of the 3 queries that I make
REPLACE INTO all.serviceworker_subscriptions
(userId, userAgent, getDisplayMedia)
VALUES ('"+userId+"','"+userAgent+"','"+getDisplayMedia+"')
UPDATE all.serviceworker_subscriptions
SET dateDerniereConnexion = '"+socketDisconnectDate+"'
WHERE userId = '"+userId+"'
REPLACE INTO all.serviceworker_subscriptions
(userId, userAgent, getDisplayMedia)
VALUES ('"+userId+"','"+userAgent+"','"+getDisplayMedia+"')
My field : dateDerniereConnexion disappear after each 2nd replace ...
Thanks

How to delete specific value from a column in SQL row?

I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.
You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.
You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.
If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?

MYSQL: Update field with concat of multiple fields

I'm trying to update a field of my table with the CONCAT of the some fields of the same table.
Whith this
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
This query has 0 rows affected and no errors.
With this other query
UPDATE tabex SET field1=CONCAT_WS(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
If the content of some of a(n) fields is NULL mysql puts a copy of the previous result
Someone can help me?
When this query
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
doesn't affect a row, the only explanation would be, that the table is empty. It would update every row in the table. But if one of the columns is NULL, your field1 column will also be NULL.
To avoid that, you have to use the COALESCE() function. This function returns the first of its parameters which is not NULL.
UPDATE tabex SET field1=CONCAT(COALESCE(tabex.a1, ''),', ',...);
On a sidenote I have to ask, why you want to do this. Comma separated values in columns are a bad idea most of the times.
And finally, your query using CONCAT_WS() is wrong. The _WS in the function name is short for "with separator", so the first parameter is the separator which then is placed between the other parameters of the function. So you should write it like this:
UPDATE tabex SET field1=CONCAT_WS(',', tabex.a1, tabex.a2, tabex.a3,...);
Another advantage of the CONCAT_WS() function is, that it ignores NULL values. Read more about the two functions in the manual.

mysql syntax for populating a column of an existing table and setting a default value for said column

I have a table and I have added a new column to it. I need to populate this new column and also set the default value for it.
The value of the new col is obtained by concatenating two strings based on the values of other columns:
the first string is the sum COL_1 + 10000
the second string is a obtained by stripping everything but the alphanumerics in COL_2
Update TABLE set NEW_COL = CONCAT ((SUM (10000 + COL_1)), (preg_replace('/[\s\W]+/','',COL_2)))
This will be the default value for the column
The reason your update is failing is that preg_replace() is not a valid MySQL function. That's a PHP function. Here's a relevant question that addresses that functionality in MySQL:
How to do a regular expression replace in MySQL?

REPLACE statement with embedded IF() logic?

EDIT: This actually works fine, no idea why I thought otherwise.
I have a prices table which includes a column price_was which needs to contain the highest ever value for prices.
Is it possible to do a REPLACE query which would update this if required?
The following (which is simplified and built dynamically in PHP) doesn't seem to work.
REPLACE prices
SET price = 1.99,
price_was = IF(1.99 > price_was, 1.99, price_was)
id_product = 1
I'm thinking perhaps it's not possible, but would love to hear otherwise since I'm updating many records and need to be as efficient as possible.
The query you posted is indeed valid, try it for yourself. I would use an UPDATE though since you're only updating one field and the REPLACE can possible over-write other column data you want left alone.
Try INSERT ... ON DUPLICATE KEY UPDATE instead:
INSERT INTO prices (price, price_was, id_product)
VALUES (1.99, 1.99, 1)
ON DUPLICATE KEY UPDATE
price_was = IF(VALUES(price) > price_was, VALUES(price), price_was)
id_product = VALUES(id_product)
This will do either an INSERT or an UPDATE, while the REPLACE statement does either an INSERT or a DELETE followed by an INSERT. You are not able to reference old values in a REPLACE statement, probably because of the DELETE/INSERT semantics. From the docs:
Values for all columns are taken from
the values specified in the REPLACE
statement. Any missing columns are set
to their default values, just as
happens for INSERT. You cannot refer
to values from the current row and use
them in the new row. If you use an
assignment such as SET col_name =
col_name + 1, the reference to the
column name on the right hand side is
treated as DEFAULT(col_name), so the
assignment is equivalent to SET
col_name = DEFAULT(col_name) + 1.