MySql PHP Add Values from fields - php

Scenario:
I have a MySql Database called "tblreqslipdetails" it has a field "subtotals" which has a value = Integer (ex. 4500.50, 2500, 3500.57.. so on..) it also has a field which has "idcategory" which has a value of (2, or 4 or 5).
Question:
How can I create a query base on my "idcategory" and add the value in my field "subtotals"?
Like:
From Where ID="idcategory" add array??? "not sure really" "subtotal" = Total
To cut it short, I would like to create a simple script where I can add the subtotals from my fields..
Thanks in advance.

Like what Vijay said i think you are looking for an UPDATE.
UPDATE `tblreqslipdetails` SET `subtotals`=`subtotals`+2000 WHERE `idcategory`=2

Related

How to make a multiple update on mysql with string replace with value of another column

Hello i'm new in mysql and i have to run a multiple update on my table.
I have 700 records in the table and i have to update them all this way:
table example :
store_id: 1
store_email: storename#gmail.com
for single update i use
UPDATE stores SET email = '1#gmail.com' WHERE id = 1;
i need to update all the emails and replace their name with their id, so it would be like this:
storename#gmail.com --> 1#gmail.com
storename#gmail.com --> 2#gmail.com
storename#gmail.com --> 3#gmail.com
those numers have to be the ID for each store.
Hope you can understand
Thanks for help.
P.S. i need to run it on magento 2
you can use CONCAT() and RIGHT() function for manipulating strings like this:
UPDATE stores SET email = CONCAT(id, RIGHT(email, 9));
The RIGHT('string', n) function extracts n characters (storemail = 9 chars in your case) from a string (starting from right).
Since you are adding id to String column gmail, you can use contact() fucntion like below :
UPDATE stors SET email=CONCAT(id, "#gmail.com") where id=2;

how to update array in crate

I would like to know how I can update a value stored in an array, in crate.io
I have a blog table - blog_tbl
A column, with data type array - tags
A id column
Inside the tags column I have - ["tag1","tag2","tag3"]
I would to know how I would go about changing 'tag1' to 'tag99'
I tried
update blog_tbl set tags['tag1'] = 'tag99' where id = '1';
Also how would I add one the the end? so making it -
["tag1","tag2","tag3","tag4"]
many thanks
Unfortunately it's not possible currently. Array elements can only be selected using the subscript notation (e.g. select tags[1] from blog_tbl;) but not updated. Maybe add a GH issue requesting that feature.
You can use the pattern found here: https://crate.io/docs/reference/sql/occ.html#optimistic-update
However, that requires you to perform the modification on client side. Pseudo code:
updated = False
while not updated:
cursor.execute('SELECT array_field, "_version" FROM table WHERE id=1')
row = cursor.fetchone()
current_array_field = row[array_field]
current_array_field.append('newtag')
cursor.execute('UPDATE array_field = current_array_field WHERE id=1 AND "_version" = row[version]')
if cursor.rowcount > 0:
updated = True
This will make your update semi safe for concurrent updates of the same field.

how to mask data fields in mysql?

I have a table containing special data and I need to hide those data after I backup them
for example I have a filed containing phone number like this
0020158578939
I need to make it like this
002015*******
whats is the sql command I must use to do this.
I searched here but didn't find a suitable answer
thank you in advance
Assuming the column name is phone, this SQL statement will do:
SELECT CONCAT(SUBSTR(phone, 1, 6), REPEAT('*', CHAR_LENGTH(phone) - 6)) AS masked_phone
FROM `yourTable`
This will only show the first 6 characters and will mask the rest with *.
Update (as per your comment):
Upon backup, you could generate the following UPDATE statement to mask the phone number:
UPDATE `yourTable`
SET phone = CONCAT(SUBSTR(phone, 1, 6), REPEAT('*', CHAR_LENGTH(phone) - 6))
If you need this phone number again then use :
base64_encode('0020158578939');
and when you need it then
base64_decode('your mysql field value of phone');
Otherwise use :
md5('0020158578939'); sha1('0020158578939');
You can use this to generate the obscured string if the field is a character string:
SELECT RPAD(SUBSTR(PhoneNumber, 1, 6), Length(PhoneNumber), '*')
See this link how the RPAD function works specificly: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_rpad

PHP MySQLi insert get last column value add 1

I'm trying to run an INSERT query into a table with 3 columns. The first column is where I'm having the issue.
It is called COMM_CODE with VARCHAR value of 10 length, and is the primary key, ALLOW NULL is unchecked.
The values for COMM_CODE look like this:
COMM_CODE
c20188
c20189
c20190
// and so on
What I would like to do, is when a new record is inserted, to basically add 1 to the most recent record.
Therefore, the most recent record is:
c20190
So when I add a new record, the COMM_CODE for the new record will be:
c20191
I tried this:
INSERT INTO table_c
(COMM_CODE, COMM_DESC, DATE)
VALUES
(''+1, 'VIDEO GAMES', NOW());
But that just adds a number 1 to that column.
How can I make this happen?
Here the solution for your query :
To generate the new code I'hv created get_new_code function in mysql. I hope you know how funcions work in mysql.
CREATE FUNCTION `get_new_code`() RETURNS varchar(11)
BEGIN
Declare var_code VARCHAR(11);
SELECT max(`COMM_CODE`) INTO var_code FROM table_c;
RETURN (CONCAT('c',(convert(substr(var_code,2,length(var_code)), SIGNED INTEGER)+1)));
END
Just to verify your logic you can use :
select get_new_code();
So that you will get the clear picture.
Call this get_new_code function in insert query like this :
INSERT INTO
`table_c`(`COMM_CODE`, `COMM_DESC`, `COMM_DATE`)
VALUES
(get_new_code(),'Description text',NOW());
This should solve your problem. :)

MySQL query to return json name value

I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html

Categories