How to Increment Column value mysql php - php

I am having problem in increment and decresing a column in value please help me...
Error at,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
UPDATE stock SET stock=stock+$quantity WHERE ID=1;
UPDATE stock SET stock=stock-$quantity WHERE ID=1;

try static first
UPDATE `stock` SET `stock`=`stock` + 200 where `ID` = 1

try this one is work for me.
UPDATE `stock` SET `stock`=`stock`+$quantity where ID=1
also check you table field Type, incremental operator work in Integer or float type field only, in text or Varchar it will not work

Related

Laravel DB run two queries at once (SELECT/UPDATE)

I have one table which stores some records as JSON. I'm trying to increment the numeric value of a key from JSON column. Below is the query I'm executing in Laravel using DB facade.
DB::connection()
->select("
SELECT CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) INTO #tmp
FROM subscriptions
WHERE `id` = '2';
UPDATE subscriptions
SET consumed = JSON_SET(consumed, '$.max_users', #tmp+1)
WHERE `id` = '2';
");
It works perfectly ok in PhpMyAdmin however when attempting to execute it from Laravel it throws an exception, that SQL syntax is incorrect (where is it incorrect?).
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE subscriptions SET consumed = JSON_SET(consumed, '$.max_users', ' at line 5 (SQL: SELECT CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) INTO #tmp FROM subscriptions WHEREid= '2'; UPDATE subscriptions SET consumed = JSON_SET(consumed, '$.max_users', #tmp+1) WHEREid= '2'; )
My suspicion is select() does not allow two different queries to be ran at once. However in this particular case I need them so in order to make use of #tmp variable. I could retrieve the value of first query and pass it to the second one using PHP, but for the sake of argument, how to run two DB selects at once in Laravel? 🤔
You can use a single update query to increment this value:
UPDATE subscriptions
SET consumed = JSON_SET(consumed, '$.max_users', CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) + 1)
WHERE `id` = '2';

Scrambling(randomize) row id's in mysql table

I have table images with ID's starting from 1 to 5000. On the page they are showed straight from 1 to 5000. What I wonder is it possible to scramble the records inside database table not to query them by RAND() on the page because they will be random on every refresh. I don't want this.
For example id(1) to become id(343).. id(453)->id(4444) and so on.. just scrambling them.
UPDATE:
This is the query which I'm trying
alter table images add column randorder double;
update table images
set randorder = rand();
create index idx_table_randorder on table(randorder);
When is executed I got column randorder with NULL but the query return again
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table images set randorder = rand()' at line 1
How about this?
alter table add column randorder double;
update table
set randorder = rand();
create index idx_table_randorder on table(randorder);
Then you can "shuffle" the records by doing:
select t.*
from table t
order by randorder;
This will use the index so it will be fast. It is stable, so it will work by paging. When you want to change the ordering, you can use the update.
Save the rows into an array. Then run:
shuffle($array);
See http://php.net/manual/en/function.shuffle.php

What’s causing the syntax error in the IF EXISTS line in my SQL query?

I’m running this SQL query:
IF EXISTS (SELECT * FROM score WHERE username='ms')
UPDATE score SET score='30' WHERE username='ms'
ELSE
INSERT INTO score (username,score) VALUES ('ms','30')
but I keep getting this syntax error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near IF EXISTS (SELECT * FROM score WHERE username='ms') UPDATE score SET score='30' at line 1
What am I doing wrong?
if as control flow is only allowed in programming blocks in stored procedures, functions, and triggers. This should not be confused with the function if(), a non-standard MySQL extension that is allowed in any expression.
However, you can do what you want using replace or on duplicate key update. First, create a unique index on username:
create unique index idx_score_username on score(username);
Then do the insert as:
INSERT INTO score (username, score)
VALUES ('ms', 30)
ON DUPLICATE KEY UPDATE score = VALUES(score);
Note that I removed the single quotes from '30'. If score1 is numeric (a reasonable assumption I think), then use a numeric constant rather than a string constant.

Rename column name with php and MySql

I am trying to change the name of the column c to novaC with php and mysql. Everywhere I look seems to give the same solution but it doesn't seem to work:
if(isset($_GET["rename"])){
mysql_query("ALTER TABLE myTable
RENAME COLUMN c to novaC");
}
If I type: ALTER TABLE aaa RENAME COLUMN c to novaC directly in MySql it gives:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COLUMN c to novaC' at line 2
alter table tablename change oldColumn newColumn varchar(10) ;
Reference : Alter Table - MySQL Command
if(isset($_GET["rename"])){
mysql_query("ALTER TABLE myTable CHANGE c novaC varchar(9999)");
}
The MySQL documentation

error when try to update table name "order" [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 4 years ago.
i try to update simple data to table name "order" but i still get error.
i try to many version query but still same ;
first try :
$result = mysql_query("UPDATE order SET order_status_id=200 WHERE order_id=75") or die(mysql_error());
second try :
$result = mysql_query("UPDATE order SET order_status_id='200' WHERE order_id='75'") or die(mysql_error());
error ;
first try :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order SET order_id=200 WHERE order_id=75' at line 1
second try :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order SET order_status_id='200' WHERE order_id='75'' at line 1
Table structure
order_id int(11)
order_status_id int(11)
i try to update others table just to make sure my query correct and all table can update.
*Im using Opencart and my site use https.
Thanks.
order is a reserved word in MySQL. You need to escape it with backticks:
UPDATE `order` SET order_status_id=200 WHERE order_id=75
See MySQL reserved words

Categories