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[]>.
Related
I am writing tests for my database in mysql.
con.query(“SELECT `list`.* FROM `list` WHERE `list`.`place` = 86 AND `list`.`person` = \"#{person_id}\" AND (( (list.state = 'open' AND num=\"#{num}\") || (list.state = ?) )) AND (list.updated_at > \"2018-06-05\") ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC LIMIT 50 OFFSET 1”)
list is a table, and num, place, and person are values of a single item from list. I'm also using this in a ruby script.
I get a syntax error. The error message is:
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 ‘`?) )) AND (list.update_at > “2018-06-05”)) ORDER BY list.person, or`’
I cannot figure out the issue. Please help me.
Look at the ORDER BY part of the query
ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC
list.state should not have an ='closed'
Just try changing the it all to
ORDER BY list.person, list.state DESC, list.updated_at DESC
I think you are using the wrong character for double quotes. You have “. Try " instead.
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 just want to delete the row with the lowest ID.
I'm trying this:
$query = 'DELETE FROM PATH\TO\ENTITY ORDER BY id ASC LIMIT 1';
$query = $this->entityManager->createQuery($query);
$query->execute();
And getting this error:
[Syntax Error] line 0, col 53: Error: Expected end of string, got 'BY'
Maybe I'm using the wrong approach.
Any suggestions how to delete the entry with the lowest id in one database call?
As Kwido said, you miss the entity alias. But the query will still not be able to execute.
First, DQL does not support LIMIT expression. It is MySQL-specific feature, is not an ANSI SQL. Other platform drivers have an own implementations of this behavior, all of them provided by common interface: setFirstResult()/setMaxResult() of Query object.
Second, DQL does not support DELETE with ORDER BY clause (see language EBNF). It is non-standard feature too, but can not be implemented for other drivers, so Doctrine does not allow it.
If you need to execute this query, you will have to use a native SQL.
Define an alias for your entity as you use DQL. See: Doctrine - By DQL
$query = $this->entityManager->createQuery('SELECT e FROM MyEntity e ORDER BY e.id ASC');
$query->setMaxResults(1); // LIMITS 1
$entities = $query->getResult();
if (count($entities) > 0) {
$query = $this->entityManager->createQuery('DELETE FROM MyEntity e WHERE e.id = :identifier');
$query->setParameter("identifier", $entities[0]->getId());
$query->execute();
}
Replace entityAlias with the first letter of your entity classname, which is the most common practice with Doctrine.
// Edit - as #Timurib stated DQL doesn't know the LIMIT. Should've used setMaxResults.
// Edit2 - As ORDER BY is not supported by the DELETE statement, but only the WHERE clause. We're now using another query to return the identifier before deleting. See DQL - Statements
$query = 'DELETE FROM table ORDER BY id ASC LIMIT 1';
$stmt = $this->entityManager->getConnection()->prepare($query);
$stmt->execute();
You cannot delete or update from entityManager. First you have to select/find the entity from Repository and then remove it. My suggestion works for raw SQL query instead.
I have a query like this:
SELECT n.*, c.*, u.nickname
FROM news n
JOIN ref_news_category c USING (id_category)
JOIN ref_user u ON n.who_post = u.uid
WHERE n.type IN ('member','public')
ORDER BY last_update DESC
I want the output in descending order, but after running on my browser I get this error message
Warning: pg_query(): Query failed: ERROR: column reference "last_update" is ambiguous
Then I change ORDER BY last_update DESC to be ORDER BY n.last_update DESC
after which I get a different error message in my browser.
Warning: pg_query(): Query failed: ERROR: column "n.last_update" must appear in the GROUP BY clause or be used in an aggregate function LINE 7: n.last_update desc
If I use mysql this query runs OK but in PostgreSQL it is not working...
What is wrong in my query?
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));