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));
Related
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.
I'm writing a PHP program and wanna to implement row-level locking to avoid concurrent user update/delete for the same record.
But I hit error "Unrecognised keyword" when using SELECT FOR UPDATE. Table type is innoDB.
Am i missing any setup for my database?
SELECT * FROM companyTable
WHERE companyId = "0000001"
FOR UPDATE;
Error
Static analysis:
1 errors were found during analysis.
Unrecognized keyword. (near "FOR" at position 57)
SQL query: Documentation
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
MySQL said: Documentation
#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 'LIMIT 0, 30' at line 3
There Is an Syntax Error Try this:
Its Select All Records From 1 - 30
SELECT * FROM companyTable WHERE companyId = "0000001" ORDER BY id LIMIT 30;
First problem seems to be, that in your SQL query is missing keyword UPDATE
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
And the second issue can be, that syntax SELECT ... FOR UPDATE in MySQL doesn't support LIMIT.
So your SQL query should be:
SELECT * FROM companyTable WHERE companyId = "0000001" FOR UPDATE
In phpmyadmin, it is difficult to remove auto added LIMIT clause - try another MySQL client.
PhpMyAdmin is not build to handle(Test) transaction control, use Mysql Console or php session instead
and use "begin" to start the Transaction before the code
begin;
select * from `tblx` where `idx`=1 for update;
update `tblx` set `field`='xx' where `idx`=1;
commit;
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.
I have such sql-query:
DELETE `LINK_LA_TYP`
FROM
`LINK_LA_TYP`
JOIN `LINK_ART` ON `LINK_LA_TYP`.LAT_LA_ID = `LINK_ART`.LA_ID
JOIN `ARTICLES` ON `LINK_ART`.LA_ART_ID = `ARTICLES`.ART_ID
WHERE
(
`ARTICLES`.ART_SUP_ID IN(
10008,
10439,
11005,
...
...
441
)
)
LIMIT 50000;
But i get error.... From mysql-doc i get that with delete+join+limit i will get errors....
But how can i change my code? (new to mysql and sql in all). How to change my code? To limit rows to delete....
Also in phpmyadmin 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 'limit 50' at line 1
From the docs For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used. dev.mysql.com/doc/refman/5.0/en/delete.html – Michael Berkowski 2 mins ago
I concur with this. Additionally,
The DELETE command cannot have a LIMIT clause. Also, generally speaking you cannot DELETE from multiple JOINed tables.
It MIGHT be possible to rewrite the statement using a subselect something like:
delete from LINK_LA_TYP
where LAT_LA_ID in
(select LA_ID
from LINK_ART
join ARTICLES on ARTICLES.ART_ID = LINK_ART.LA_ART_ID
where ARTICLES.ART_SUP_ID in (...));
List<Lahetys> last = session.createQuery("from lahetys order by lahetysNro DESC LIMIT 1").list();
and in the log I got:
INFO: Hibernate: select from order by lahetysNro DESC LIMIT 1
WARN: SQL Error: 1064, SQLState: 42000
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 order by lahetysNro DESC LIMIT 1' at line 1
What has happend to "from LAHETYS"? What is the best practice to handle that with HQL or/and with SQL?
Another problem:
Lahetys last = (Lahetys)session.createSQLQuery("select * from lahetys order by lahetysNro DESC LIMIT 1").uniqueResult();
session.getTransaction().commit();
and I get a exception:
Ljava.lang.Object; cannot be cast to Lahetys
So I can't cast an object to my Lahetys-object, weird?
Thank you!
Sami
Your HQL query is invalid. LIMIT is not a valid HQL clause. To do that in Hibernate, just do
Query query = session.createQuery("from lahetys order by lahetysNro DESC");
query.setMaxResults(1);
Lahetys last = (Lahetys) query.uniqueResult();
When you're using HQL, you should specify fully qualified className instead of tableName. The same way you should specify propertyName instead of columnName. Also keep in mind that both are case - sensitive.
Looking at your queries & the exception you're getting, I'm assuming that lahetys is your table name & lahetysNro is your column name.
You should use for example: If your Lahetys class is located at com folder:
List<Lahetys> last = session.createQuery("from com.Lahetys order by lahetysNro DESC LIMIT 1").list();
For your 2nd question:
Here you have used SQL instead of HQL.
When you use SQL with hibernate in such a way, it always returns List<Object[]> & not List<Lahetys[]>.