Mysql : Query to update email with unique random value - mysql

I need to anonymize emails in DB, so I try to set a query, but I cannot find a way to generate unique random string for each.
What I have so far is :
update candidate set email = (select CONCAT(SUBSTRING(MD5(RAND()) FROM 1 FOR 15) , '#test.fr'));
But obviously the value is not unique, is that possible with a simple query?
I tried solution here : https://harrybailey.com/2015/08/mysql-roughly-random-string-generation-for-updating-rows/ but same result, I got a
Error Code: 1062. Duplicate entry '0417da5fb3d071b9bd10' for key
'email'

You can use UUID
UPDATE `candidate` SET email = CONCAT(MD5(UUID()),'#test.fr');
and if you want exactly 15 characters
UPDATE candidate SET email=CONCAT(SUBSTRING(MD5(UUID()),1,15) , '#test.fr');

Related

MySQL Query: UPDATE and/or APPEND

I have a temporary table that I use to insert into the master db.
The temp table is named "temp_table"
The master table is "master"
I currently use the following command to update "master"
SELECT COUNT(*) FROM master;
SHOW COLUMNS FROM master;
INSERT INTO master
SELECT * FROM temp_table
ON DUPLICATE KEY UPDATE email = VALUES(email), phone = VALUES(phone)
Now, I want to be able to append field (counter) from the "temp table" into "master." The field already exists in both tables and I just want to be able to update or append it.
"counter" field in master may be empty or it may contain a number value already.
In cases where the value exists, it should append separated by a comma. Format (88,89,90)
In cases where the it's empty, it should update (88)
Thank you in advance.
I think you want:
on duplicate key update
email = values(email),
phone = values(phone),
counter = case when counter is null
then values(counter)
else concat(counter, ',', values(counter))
end
You can also phrase this with coalesce(), although the expression might be a bit more complicated to understand:
on duplicate key update
email = values(email),
phone = values(phone),
counter = concat(
coalesce(concat(counter, ','), ''),
values(counter)
)

MySQL insert new value when no other like that

I am trying to solve the problem with insert new value into table. Primary key is ID not email... Before insert i need check other records for email value. If no one match inserted email, data is inserted. But i canot write correct code for that...
IF EXIST (SELECT email FROM users WHERE email = "email")
THEN
BEGIN
'We Have Records of this Customer'
END
ELSE
BEGIN
INSERT INTO users
VALUES (null,'email9','password9','forename9','lastname9')
END
END IF
Or:
IF SELECT COUNT(*) FROM users WHERE email = 'email' > 0
THEN
BEGIN
//email exist
END
ELSE
BEGIN
//inserting
END
END IF
This looks like a good use case for MySQL INSERT ... ON DUPLICATE KEY UPDATE syntax.
First of all, you want to create a UNIQUE constraint on column email. This is the proper way to represent your business rule:
ALTER TABLE users ADD CONSTRAINT users_unique_email UNIQUE (email);
Now, if the record that is about to be inserted would generate a duplicate on the primary key or on that UNIQUE constraint, then MySQL lets you turn the operation to an UPDATE instead.
In your case, since you want not to update anything in that case, you can simply re-assign the email column (which we know is the same here).
Consider:
INSERT INTO users
VALUES (null,'email9','password9','forename9','lastname9')
ON DUPLICATE KEY UPDATE SET email = 'mail9';
I cant comment, but I post this. I recommend making an external script like in python or java to do this for you by selecting * and parsing through the output because SQL itself does not have the power to do this.

If record exists fetch the existing column , compare it and then do an update Or else just insert new record

If the record doesn't exist , i need to insert a new record , but if it exists then
i need to select a particular column value from that row and do an update based on it .
I have managed to write down below query , which does insert / Update using duplicate on update
insert into securities(symbol, buyerquan, sellerquan , totaltradedquan) values('BANKBARODA', 73, 0, 4290270) on duplicate key update buyerquan=buyerquan+VALUES(buyerquan),sellerquan=sellerquan+VALUES(sellerquan),totaltradedquan=totaltradedquan+VALUES(totaltradedquan)
This is my sqlfiddle http://sqlfiddle.com/#!2/aaf91/5
My requirement is
If the record exists , how can i fetch the existing column , compare it and then do an update ??
Do we need to write an another SQL query for fetching existing column value ??
If record exists fetch the existing column , compare it and then do an update Or else just insert new record
Can we do all this in same query ??
Can anybody please help me .
Mysql has a shortcut for that, it's called "INSERT ... ON DUPLICATE KEY UPDATE"
http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html
Your question is vague. For instance, you don't specify what columns are used to define a duplicate. Based on your query, I am guessing that you mean symbol.
Your query is:
insert into securities(symbol, buyerquan, sellerquan , totaltradedquan)
values('BANKBARODA', 73, 0, 4290270)
on duplicate key update buyerquan = buyerquan+VALUES(buyerquan),
sellerquan = sellerquan + VALUES(sellerquan),
totaltradedquan = totaltradedquan + VALUES(totaltradedquan);
This seems to be the right syntax (which for most people that is the hard part of the question). The only thing that is missing is the specification of uniqueness:
create unique index securities_symbol on securities(symbol);
You can also specify this condition in the create table statement, using the unique keyword.

MYSQL INSERT - "0 rows inserted". Any suggestions why is that?

INSERT INTO fields (id_region, id_fields_info, subsidy_dka, id_rents_dka, type_uses, id_rented_from, id_categories, id_farmer, id_season)
SELECT
regions.id,
fields_info.id ,
120,
rents_dka.rent_dka,
"собствена",
rented_froms.id,
categories.id_category,
farmers.id,
seasons.id
FROM regions, fields_info, rents_dka, rented_froms, categories, farmers,
seasons
WHERE
region = "Азмък" AND
field_num = 2222 AND
rent_dka = 60 AND
name = "Десислав" AND
id_category = 3 AND
name = "Десислав" AND
season = "2012-2013"
So I have these tables:
regions,
fields_info,
rents_dka,
rented_froms,
categories,
farmers,
seasons
and they are filled with some data.
I've made a form where the user fills the fields with data from these tables, that I've mentioned, and when the submit button is clicked I want to fill table FIELDS in MYSQL with ID's which I get from the data, the user had entered.
to spot the problem, I'd proceed as follow:
execute an insert without cyrillic
remove all the and
make a default insert with default values
if you get "0 rows inserted" it means that the syntax is correct, but the where clause fails to find any matching entry. I suspect the problem is the AND with the cyrillic. Remove the ANDs until the query finds some entries
I got this case when i tried to insert into a table having a column unique and my SQL had the keyword IGNORE in it :
The query:
INSERT IGNORE INTO users SET `user_id` = 7321, `name`= 'test_name', `phone` = '+188888888';
If the query didn't have IGNORE, it would throw an error (because the column user_id was set unique and I already had a row with the same user_id) but due to IGNORE keyword it just ignores the query and hence results in 0 rows inserted.
Note: I know the question asked doesn't has IGNORE key in the query, but might help someone.

MySQL Query - avoid Duplicate Entry

The Userdatabase has usernames with "?" ending on the name. For example username: "Alex?"
Instead of deleting it I'm trying to replace this "?" with a "2" to avoid duplicate entries. The Problem is, there a still duplicate entries even with 2 at the end. I need a query, which automatically changes the 2 to 3,4,5,6,7,8 or 9 until no duplicate entry exists anymore. I was doing this manually until now, but honestly I changed over 200 lines and I guess there are more than 1000.
Some Ideas?
The Query I use:
UPDATE `userdatabase`
SET `username` = replace(`username`, "?","2")
i am not familiar mysql update syntax in detail, but if table "userdatabase" have unique id column may be possible something like that
replace(username, "?", (select count(*) from userdatabase db where db.username = username and db.id < id))