I am finding inputted user_id in accepted_join_id above query gives error as
syntax error, unexpected '$user_id' (T_VARIABLE)
$query=DB::select('select activity_id,accepted_join_id from table_user_create_activity WHERE FIND_IN_SET('$user_id',?) and activity_id=?',[$user_id,$accepted_join_id]);
if you want to inject $user_id value from php into your query try this:
$query=DB::select('select activity_id,accepted_join_id from table_user_create_activity WHERE FIND_IN_SET('.$user_id.',?) and activity_id=?',[$user_id,$accepted_join_id]);
or if $user_id is in query it should be like:
$query=DB::select('select activity_id,accepted_join_id from table_user_create_activity WHERE FIND_IN_SET("$user_id",?) and activity_id=?',[$user_id,$accepted_join_id]);
Related
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 below code to get the customers whose term is ending today.
$customers = CompanyModel::where(("term_ending") ,"=", "CURDATE()")->get();
But when I run this it returns null.
Same query I run in Mysql it gives me results.
select * from `companies` where `term_ending` = CURDATE()
What is wrong I am doing?
I solved it using Carbon.
$customers = CompanyModel::where(("term_ending") ,"=", Carbon::today())->get();
Thanks :)
I need to join two tables where a substring of the base table's join column's value is used to join with the other table.
I'm using zend framework 2 and I've written the query as follows.
$select = new Select($this->table);
$select->columns(array('match_code'))
->join('countries','countries.cid = SUBSTR(matches.teams,1,3)', 'name')
->where(array('round'=>1));
This gives an error.
SQLSTATE[42000]: Syntax error or access violation: 1630 FUNCTION mydatabase.SUBSTR does not exist.
Check the 'Function Name Parsing and Resolution' section in the Reference Manual
I've run the mysql query corresponds to this and it works perfectly.
SELECT `matches`.`match_code`, `countries`.`name`
FROM `matches` INNER JOIN `countries`
ON `countries`.`cid`=SUBSTR(`matches`.`teams`,1,3)
WHERE `round` = 1
I cannot use Zend\Db\Sql\Expression in here as the join() expects only a string.
Any suggestions??
I have solved this issue by using Zend\Db\Sql\Expression in the ON clause of join().
$select->columns(array('match_code'))
->join('countries',new \Zend\Db\Sql\Predicate\Expression('countries.cid = SUBSTR(matches.teams,1,3)'), 'name')
->where(array('round'=>1));
Thanks to #Crisp for the suggestion.
I am trying to write a query in Laravel using Eloquent, but only want the last 5-ongoing fields in it. This is the query:
public static function past_profile_fan_likes($id) {
$latest_profile_fan_likes = DB::table('fanartists')
->join('artists', 'fanartists.artist_id', '=', 'artists.id')
->orderBy('fanartists.created_at', 'DESC')
->skip(4)
->where('fanartists.fan_id', '=', $id)
->select('artists.id', 'artists.fbid', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')
->get();
return $latest_profile_fan_likes;
}
When I call this, I am getting this error:
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 'offset 4' at line 1 (SQL: select `artists`.`id`, `artists`.`fbid`, `artists`.`stage_name`, `artists`.`city`, `artists`.`state`, `artists`.`image_path`, `artists`.`description` from `fanartists` inner join `artists` on `fanartists`.`artist_id` = `artists`.`id` where `fanartists`.`fan_id` = ? order by `fanartists`.`created_at` desc offset 4) (Bindings: array ( 0 => '1', ))
Is there something I am doing wrong here? Maybe something wrong with the skip usage? Thanks for your help.
You need to add a take query in order for it to add a LIMIT query and turn it into the correct syntax;
DB::table('fanartists')
->join('artists', 'fanartists.artist_id', '=', 'artists.id')
->orderBy('fanartists.created_at', 'DESC')
->skip(4)
->take(100)
->where('fanartists.fan_id', '=', $id)
->select('artists.id', 'artists.fbid', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')
->get();
You will need to suppliy a limit if using an offset, even if you don't want to limit. See here: http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990
This looks more like fluent which I'm not quite as familiar with. but if you have your models and relationships defined, using Eloquent, you could reverse the order and take(5) if you're just looking to get the last 5 records:
like this...
$latest_profile_fan_likes = fanartists::with('artists')->where('fan_id', '=', $id)->orderBy('created_at', 'ASC')->take(5)->get('id', 'fbid' .......);
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[]>.