How to write the update query by using Arduino code in order to update (overwrite) the previous data in MYSQL.
Below is the example for FIXED VALUE QUERY. Its work well but how to change to variable value?
char UPDATE_SQL[] = "update [tableName] set [column_name] = [new_fixed_value] where [column_name] = [previous value] ;
update[database] it's wrong. It should be update[tableName]. For an example If I want to change in students table student sam's marks to 90 I can write as below
UPDATE students SET marks=90
WHERE name='sam'
Use sprintf to put your value into the string like this:
sprintf(UPDATE_SQL,"UPDATE students SET marks=%d WHERE name='%s'",
yourval,yourstrin);
Related
I need to write a sequence of queries where I want to sometimes use a different table or database and don't want to keep writing them over and over again which can lead to a mistake.
SET #NAME = "Fred";
SELECT pid FROM old_table.people WHERE name = #NAME INTO #PID;
INSERT new_table.address SELECT *
FROM old_table.address o WHERE o.pid = #PID;
...
Is there a way todo a global SET or ALIAS so I don't have to repeat the database.table names again and again with the chance of a typo.
ie looking for something like this - this is not real sql
ALIAS old_table.people #OP;
ALIAS new_table.people #NP;
ALIAS old_table.address #OA;
ALIAS new_table.address #NA;
SET #NAME = "Fred";
SELECT pid FROM #OP WHERE name = #NAME INTO #PID;
INSERT #NA SELECT * FROM #OA o WHERE o.pid = #PID;
The closest thing to what you ask for is a VIEW. But I don't see how this would help avoid typos. You'd just be swapping one name for another name. You could make a typo on the view name just as easily.
You definitely cannot use a user-defined variable as a table identifier. When you use a variable in a query, it is as if you used a string value.
I created a field name is result and type is text. I just want to update 'lat' in column. When I use this query I get syntax error. How can I do?
The column data is
"{"lat":"48.00855","lng":"58.97342","referer":"https:\/\/abc.com\/index.php"}"
Query is
update public.log set (result::json)->>'lat'=123 where id=6848202
Syntax error is
ERROR: syntax error at or near "::"
Use the jsonb concatenation operator (Postgres 9.5+):
update log
set result = result::jsonb || '{"lat":"123"}'
where id = 6848202
In Postgres 9.4 use json_each() and json_object_agg() (because jsonb_object_agg() does not exists in 9.4).
update log
set result = (
select json_object_agg(key, case key when 'lat' then '123' else value end)
from json_each(result)
)
where id = 6848202
Both solutions assume that the json column is not null. If it does not contain the lat key, the first query will create it but the second will not.
In PostgreSQL 13, You can:
update public.log set result = jsonb_set(result,'{lat}','"123"') where id=6848202;
In case the column is still null, you can use coalesce. The answer is provided here: PostgreSQL 9.5 - update doesn't work when merging NULL with JSON
I also tried to update json value in json type field, but couldn't find appropriate example. So I've connected to postgres DB using PgAdmin4, opened desired table and changed desired field's value, then looked at Query History to see what command it uses to change it.
So, finally, I've got the next simple python code for own purposes to update json field in postgres db:
import psycopg2
conn = psycopg2.connect(host='localhost', dbname='mydbname', user='myusername', password='mypass', port='5432')
cur = conn.cursor()
cur.execute("UPDATE public.mytable SET options = '{\"credentials\": \"required\", \"users\": [{\"name\": \"user1\", \"type\": \"string\"}]}'::json WHERE id = 8;")
cur.execute("COMMIT")
I need advices in order to make a process on my list of values. I have a table llx_societe and some fields where one of them is code_client. This field looks like :
0099
00100
00101
00102
...
00998
00999
001000
I want to remove the first zero for all values between 00100 and 00999 in order to get 0100 until 0999.
I wrote this command :
UPDATE `llx_societe`
SET `code_client`= SUBSTR(code_client,1)
WHERE `code_client` BETWEEN '00100' AND '00999';
But nothing, none lines are proceed.
Have you an explanation ?
SQL starts counting from 1 and not 0. Try this:
UPDATE `llx_societe`
SET `code_client`= SUBSTR(code_client,2)
WHERE `code_client` BETWEEN '00100' AND '00999';
Try this:
UPDATE llx_societe
SET code_client= SUBSTR(code_client, 2)
WHERE code_client between '00100' AND '00999'
MySQL SUBSTR() function
You can use the following SQL:
UPDATE TABLENAME SET data = SUBSTR(FIELD, 2);
for example if there is table(userinfo) and field is username
UPDATE users SET username = SUBSTR(username, 2);
I have the following MySQL statement:
$stmt = $conn->prepare('UPDATE `equipment` SET `currentHours`= :unitHours, `lastUpdate`= :lastUpdate WHERE `equipType`= :equipType AND `unitNumber`= :unitNumber');
$stmt ->execute(array(':lastUpdate'=> $dateToday, ':unitHours' => $unitHours, ':equipType'=> $equipType, ':unitNumber'=> $unitNumber));
I need to only update the currentHours if the value is equal to or greater than the value currently stored in that row. Is this possible with a MySQL statement or do I have to do it in PHP? I am new to programming so sorry in advance if this sounds like a simple question.
UPDATE `equipment` SET `currentHours`= :unitHours, `lastUpdate`= :lastUpdate
WHERE `equipType`= :equipType AND `unitNumber`= :unitNumber
AND
currentHours<:unitHours
The above updates only records with current hours lower than the input, it won't update all last update field that matches the rest of the condition.
If u want to update the field currenthours only when it is lower than the input, but update the lastupdate field regardless, then:
UPDATE `equipment`
SET `currentHours`= IF(currentHours<:unitHours,:unitHours,currentHours),
`lastUpdate`= :lastUpdate
WHERE `equipType`= :equipType AND `unitNumber`= :unitNumber
How can I modify this statement to set the region value where there are multiple values in the location_town column?
UPDATE `wp_em_locations` SET `location_region` = 'The-Valley'
WHERE `location_town` = 'Bond'
IE: Bond is not the only town, I have many town names but all should be used to set location_region = The-Valley.
How do I accomplish this using one statement? Or do i need to run the same statement for each town name?
You can use either an OR or IN statement
WHERE `location_town` IN ('Bond', 'OtherTown')
or (no pun intended)
WHERE `location_town` = 'Bond' OR `location_town` = 'OtherTown'
Is the statement more complex than this? if you're trying to set every row to have location_region set to 'The-Valley' why use the where clause, which only serves to limit the scope of the update query?
Would this do what you're looking for, are am I misunderstanding?
UPDATE `wp_em_locations` SET `location_region` = 'The-Valley' WHERE 1;