Updating user rank - mysql

I'm trying to update users' rankings in a single query but it crashes with this error:
ER_PARSE_ERROR: 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 users SET rank = #r:= (#r+1) ORDER BY score DESC' at line 1
Is there anything wrong with my query?
SET #r=0;UPDATE users SET rank = #r:= (#r+1) ORDER BY score DESC
I got the query from this answer
I'm executing this query on a node.js mysql library.
db.query('SET #r=0;UPDATE users SET rank = #r:= (#r+1) ORDER BY score DESC', function(err){
if (err){
console.log(err);
return;
}
console.log("Ranking updated successfully.");
});

How about:
SET #r=0;UPDATE users SET rank = (#r+1) ORDER BY score DESC
I'm not sure as why you would want to reassign the var again.

Ok, i've found the problem.
I forgot to set multipleStatements to true as described in here.

Related

Error in sql syntax value ? but it's showing the value?

the title isn't very specific but I don't know how to make it better. I've got this error in my sql telling me something is wrong but I don't understand why it's wrong, currently using mysql 8.0.24. If you wonder why it looks strange it's because I use it in lua
Sql:
local q = [[SELECT id, hex_id, steam_id, community_id, name, ip, rank FROM users WHERE hex_id = #id;]]
local v = {["id"] = hexId}
Error:
An error happens for query "SELECT id, hex_id, steam_id, community_id, name, ip, rank FROM users WHERE hex_id = ?; : ["steam:*****"]": ER_PARSE_ERROR: 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 'FROM users WHERE hex_id = 'steam:*****'' at line 1
RANK is a keyword in MySQL 8. You need to enclose it inside backticks:
SELECT id, hex_id, steam_id, community_id, name, ip, `rank` FROM users ...

LIMIT in MySQL Update Row

I am trying to Update Some row in my database. If I run without limit its working fine but if I run it with limit its giving me error like below
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 '35' at line 1
My Query is like below
UPDATE number_list SET sync = 0 WHERE server = 1 ORDER by id ASC LIMIT 0,35
Let me know if someone can correct me.
You can use limit in an update (in MySQL) but not an offset. So just do:
UPDATE number_list
SET sync = 0
WHERE server = 1
ORDER by id ASC
LIMIT 35;
This is a bit subtle, because SELECT supports offsets. However, it is clear in the syntax diagram for UPDATE.

right syntax to update mysql highest id

this is how i select highest id for a specific username
SELECT * FROM messages WHERE user='me' ORDER BY id DESC LIMIT 0, 1
i have column named "send" and a user named me
now i want to update it like this :
UPDATE messages SET `send`='ok' WHERE user='me' ORDER BY id DESC LIMIT 0, 1
i get 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 ' 1' at line 1
The limit in update only allows a row count, not an offset:
UPDATE messages
SET `send`='ok'
WHERE user='me'
ORDER BY id DESC
LIMIT 1;
I feel like you're setting yourself up for failure here.
If you're going to select the highest ID before updating, then:
SELECT max(id) FROM messages WHERE user='me'
Your UPDATE should be using that same ID you just retrieved as the WHERE clause. Otherwise, you can potentially update the wrong row with "ok". Full code, as I would do it:
// Get ID to update
$idToUpdate_q = $pdoConn->prepare("SELECT max(messages.id) FROM messages WHERE user = :user");
$idToUpdate_q->bindValue(':user','me');
$idToUpdate_q->execute();
$idToUpdate = $idToUpdate_q->fetchColumn();
// Update Row
$updateRow_sql = $pdoConn->prepare("UPDATE messages
SET
messages.send = :send
WHERE
messages.id = :idToUpdate");
$updateRow_sql->execute(array(
':send' =>'ok',
':idToUpdate' =>$idToUpdate));

User-defined MySQL variables in Laravel 3?

I want to update the "rank" for a group of MySQL records with sequential numbers using a user-defined variable. The following query runs fine via the MySQL command line:
SET #rank:=0; UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC
But if I try and run it as a Fluent query using Laravel, it fails.
DB::query("SET #rank:=0; UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC");
Error message:
SQLSTATE[42000]: Syntax error or access violation: 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 'UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY' at line 1
SQL: SET #rank:=0; UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC
Bindings: array (
)
[SOLVED]
DB::raw() to the rescue! The following works:
DB::query(DB::raw("SET #rank:=0"));
DB::query("UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC");
It is not possible to execute multiple statements in one query. Laravel uses PDO under the hood which prevents this. You could attempt to call this over 2 queries instead, since #rank should be available for the duration of the connection.
DB::query("SET #rank:=0");
DB::query("UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=? ORDER BY score DESC", array(4));

error in updating mysql table with filters

i have a mysql table with the follwoing fields :
id, desc, value, people, amount, weight
in the above order i run the follwoing
update match1 set weight = 5 where desc = 'fat' and id != '6';
follwoing is the error message i get :
**#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 'desc = 'bat' and id = 6' at line 1
can someone please let me know whats wrong with this?
the column desc is a keyword in mysql. use backquotes i.e.
where `desc` = 'fat'