MySQL update md5 of another column - mysql

I need to do an update query making an md5 hash from my google calendar column. This is my query:
UPDATE `ea_appointments` SET `hash` = MD5(`id_google_calendar`)
Would this work to make something like this?:
Table: ea_appointments
id_google_calendar Hash
e5e3were760lkj792c7t5vm61bvk_20160729T200000Z d5f9f4ef02e438d49c8bf39cd4b4118d

Yes, it'll be work.
And you can loosely check it by:
select md5('test');
Result:
+----------------------------------+
| md5('test') |
+----------------------------------+
| 098f6bcd4621d373cade4e832627b4f6 |
+----------------------------------+
Or:
select md5('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z');
Result:
+------------------------------------------------------+
| md5('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z') |
+------------------------------------------------------+
| 06b5a13d9a7b0ed26ab1406434954972 |
+------------------------------------------------------+
Or:
create table t(id_google_calendar varchar(100), hash varchar(100));
insert into t values ('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z', '');
update t set Hash = md5(id_google_calendar);
select * from t;
Result:
+-----------------------------------------------+----------------------------------+
| id_google_calendar | hash |
+-----------------------------------------------+----------------------------------+
| e5e3were760lkj792c7t5vm61bvk_20160729T200000Z | 06b5a13d9a7b0ed26ab1406434954972 |
+-----------------------------------------------+----------------------------------+

I think it would work.
Also I suggest that you test it first.
You can test query with:
Select statement:
SELECT id_google_calendar, MD5(id_google_calendar) as hash FROM ea_appointments
Update record in test table. Create some test table add few record in it and run query

Related

INSERT INTO WHERE LIKE Condition

I am working on a trigger which needs INSERT INTO with WHERE LIKE logic.
I have one table :
Tabel test;
idDocument = varchar(32) idUnit = varchar(3)
-----------------------------
| idDocument | idUnit |
-----------------------------
| AA/2021/KK | NULL |
| AA/2021/JJ | NULL |
| BB/2021/KK | NULL |
| CC/2021/JB | NULL |
-----------------------------
How to INSERT INTO using WHERE LIKE Condition and myquery still ERROR.
myquery :
INSERT INTO test ('idUnit') Values ('111') WHERE idDocument LIKE
'%KK%'
Normally to update existing rows with a new value you'd do something like this:
UPDATE test SET idUnit='111' WHERE idDocument LIKE '%KK%'
This will not insert data, it will only alter existing data.
Note:
INSERT is specifically for adding new rows of data
UPDATE is exclusively for updating existing rows with new data
You can't conditionally add new rows. You either add them or you don't. You can conditionally update or delete them.
Don't think about it in terms of inserting new data, always think in terms of rows and columns which is how SQL works.

mysql On Duplicate value in field, insert new row with new value

I want to add a new record in a table if duplicate value enters in a unique field. I don't want to update the existing one but want to add a new record by modifying the unique field value.
Is this possible in mysql?
EDIT:
Edited after user comment on this post:
You need write table locking on both of those two processes.
A WRITE lock has the following features:
The only session that holds the lock of a table can read and write data from the table.
Other sessions cannot read data from and write data to the table until the WRITE lock is released.
Also look at SQL UNIQUE Constraint
BEFORE EDIT:
Yes it is possible. And it took me awhile to figure it out. I build this on your input and compering values as test1, test2 etc, where test is always the same and has trailing number. As you specified.
It can be done as MySQL TRANSACTION in 4 steps.
Lets say you have table testT where name is unique to insure we have no doubles.
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
And you want to insert a new item with name test1 we set is as:
SET #newName = 'test1';
Then we need to check if it already exists in table:
SELECT #check:=COUNT(*) FROM testT WHERE name = #newName;
We do a count here to get true or false and save it as #check here so we can compare it later. This will result into 1 row as test1 already exists in table.
Next we do another selection to get the highest number of test* and store it as #number, this next query selects all tests and does a SUBSTRING after 4 latter's giving us all numbers after first 4 latter's. (99999999999) numbers actually just to be sure we don't miss any but in our case result is only "3" because that is last record "test3" in table.
SELECT
#number:= SUBSTRING(name,5,99999999999)
FROM testT;
Now we can do an insert:
INSERT INTO testT(name)
VALUES
(
IF(#check = "", #newName , CONCAT(LEFT(#newName,4),RIGHT(#number,1)+1)
)
);
This tries to insert our #newName into table under IF condition, and that is if our #check is empty then he will insert #newName, if not it will take word test out of string and append a highest #number from earlier and add + 1 too it.
So result for #newName = 'test1' is below. If you change this into #newName = 'test3' result wold be same new insert test4.
**Schema (MySQL v5.7)**
SET #newName = 'test1';
---
**Query #1**
SELECT * FROM testT
ORDER BY id;
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
| 3 | test4 |
---
And if you change it in ANY test* that number does not already exists it will insert it normally. In case below: #newName = 'test6'
SET #newName = 'test6';
**Query #1**
SELECT * FROM testT
ORDER BY id;
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
| 3 | test6 |
This way an insert will always be made.
You can play with this here : View on DB Fiddle just by changing SET #newName = 'test6'
I am no expert and it took me couple of hours to figure this way out, as I wanted to know if this was even possible.
And I would appreciate if any other user can suggestion any other way or improve my method.

How to use MySQL aes_encrypt and aes_decrypt?

I'm simply testing MySQL AES_ENCRYPT() and AES_DECRYPT() before I start using it in my app. So I write a simple query to test it like:
SELECT AES_DECRYPT(AES_ENCRYPT('SERV92','TESTTTTTTT'),'TESTTTTTTT') AS `TEST`
I get an error because there are to few parameters in AES_ENCRYPT()
I do some research and find that my version(5.6) of MySQL does indeed take an extra parameter so I rewrite the query
SELECT AES_DECRYPT(AES_ENCRYPT('SERV92','TESTTTTTTT',RANDOM_BYTES(16)),'TESTTTTTTT',RANDOM_BYTES(16)) AS `TEST`
Result:
+-----------+
| TEST |
|-----------|
| NULL |
+-----------+
Important MySQL Variables:
block encryption mode=aes-256-cbc
I'm trying to use AES 256
You apparently need to use the same init_vector 3rd argument as this works:
> set #a=RANDOM_BYTES(16);
> SELECT AES_DECRYPT(AES_ENCRYPT('SERV92','TESTTTTTTT',#a),'TESTTTTTTT',#a) AS `TEST`;
+--------+
| TEST |
+--------+
| SERV92 |
+--------+
In your case you used RANDOM_BYTES(16) twice so that different values are used in encrypt and decrypt.
Okay I found the problem, AES_DECRYPT() returns data as a blob. Basically I just needed to tell it that it was utf8 text, as show below.
SET #a=RANDOM_BYTES(16); #Thanks Hartmut Holzgraefe
SELECT CONVERT(AES_DECRYPT(AES_ENCRYPT('SERV92','TESTTTTTTT',#a),'TESTTTTTTT',#a) USING utf8) AS `TEST`
+----------+
| TEST |
+----------+
| SERV92 |
+----------+

mysql simple where clause not working?

Database changed
mysql> select * from userinfo;
+-----------+----------+-----------------------+------------+
| firstname | lastname | username | password |
+-----------+----------+-----------------------+------------+
| asif | kolu | ashufound | 123456 |
| faisal | samad | tfhgfhgfh#gmail.com | 123456 |
| kamran | shafat | kamthemaam | kamoos |
| ubaid | mir | sadfsfsff#yahoo.com | qwertasd |
| majid | mir | zsffsa | afdfdsf |
+-----------+----------+-----------------------+------------+
5 rows in set (0.00 sec)
mysql> SELECT * from userinfo WHERE lastname = 'mir';
Empty set (0.10 sec)
mysql> SELECT * from userinfo WHERE lastname='mir';
Empty set (0.00 sec)
what is wrong with this code simple where clause not working?actually problem is in the code for insert i think
your last name in your table may have a space before or after mir
mir
^---^---look and remove spaces from here in your table
I m usign select Query in this the where clause doesnt working query is
select * from table_t where id = '96'
this query is resulting 0 rows but
when i try
select * from table_t where id like '96'
this query is working fine.
and when i try like with column name like
select id from table_t where id like '96'
returning 0 rows
the id is auto generated primary key not have white spaces
why????
is there any database issue???
this query is working fine on my local machine but when i try it online it is misbehaving.
Thanx.
1- You may have space before or after "mir".
2- You may have special (invisible) characters before or after 'mir' or even between its characters.
To solve this problem, I suggest to do this first:
Update userinfo
set lastname = 'mir'
where (username = 'sadfsfsff#yahoo.com') or (username = 'zsffsa')
And then, run this to check:
Select * from userinfo where lastname = 'mir'
I see some good answers but in the case that you couldn't update leading or trailing spaces in your entire db for each name, you could write the select a little differently. If it is a space issue, try this.
SELECT * from userinfo WHERE TRIM(lastname) = TRIM('mir')
If that doesn't work, try LIKE and see if you get results. That could help with debugging.
SELECT * FROM userinfo WHERE lastname LIKE '%mir%'

Mysql (php) update or replace shopping bag content in one query, with multiple values

I've got the flowing tables in mysql db for a shopping bag:
BAGS
-----
| bagID | date_added |
| primary Key | |
------------------------------
| 1 | 2012-01-04 |
BAGS_CONTENT
-----
| ID | productID | qyt |
| foreign key->bagID | | |
-----------------------------------------
| 1 | 103 | 4 |
// $sql Could contain this:
$sql = "(1,103,5),
(1,101,3)";
INSERT INTO BAGS_CONTENT
( ID, product_id, qty)
VALUES
".$sql."
I like the BAGS_CONTENT to update an existing record (if exists ID and product_id) and add a new row if not exists (the ID and product_id).
I've tried using REPLACE INTO and ON DUPLICATE KEY UPDATE but I can't get it to work.
May be its has something to do with the keys?
How should you query the db in a situation like this?
ON DUPLICATE KEY UPDATE triggers the UPDATE statement when the unique value is already existing in the table. Make sure that you've set the right fields to be unique. I think you have to put an UNIQUE on ID and productID (both in one combined unique):
ALTER TABLE BAGS_CONTENT ADD UNIQUE (ID, product_id)
Your query should look like this:
INSERT INTO BAGS_CONTENT (ID, product_id, qty) ".$sql." ON DUPLICATE KEY UPDATE qty = VALUES(qty);
Here's more information about 'on duplicate key':
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
To use ON DUPLICATE KEY UPDATE you first need to create a unique key on BAGS_CONTENT(ID,productID)
Then use
INSERT INTO BAGS_CONTENT (ID,productID,qyt) VALUES(1,101,3) ON DUPLICATE KEY UPDATE qyt=VALUES(qyt);
You need to create a key error in order to trigger the special behavior of REPLACE. To get the proposed behavior add an index:
mysql> create table bag_content (id INT,productID INT,qty SMALLINT);
mysql> create unique index baggy on bag_content (id,productID);
mysql> replace bag_content values(1,111,5);
mysql> replace bag_content values(1,112,5);
mysql> replace bag_content values(1,111,500);
mysql> select * from bag_content;
+------+-----------+------+
| id | productID | qty |
+------+-----------+------+
| 1 | 111 | 500 |
| 1 | 112 | 5 |
+------+-----------+------+
Also watch out: you're using SQL supported in mysql only. The pejorative is 'their sql'... because of all the innovations of mysql that don't go through a standards process. The replace functionality is great, but it will increase the barrier to moving your code to other databases.