I'm having alot of trouble using a subquery in an update statement in doctrine 1.2
I want to set a field to the result of a subquery, but it seems impossible.
This is what i tried sofar.
$query = Doctrine_Query::create()->from('Users_Model_Book b');
$subSelect = $query->createSubquery()->select('ROUND(SUM(br.rating) / COUNT(br.id))')->from('b.BookRating')->where('b.BookRating.book_id = b.id');
$query->update()->set('bookrating', '('.$subSelect->getDql().')')->where('b.id = ?', $this->id)->getRawSql();
Will give 'Unknown component alias br'
$q = new Doctrine_RawSql();
$q ->addComponent('b', 'Users_Model_Book')
->addComponent('br', 'Users_Model_BookRating')
->update('b')
->set('b.bookrating', 'ROUND(SUM(br.rating) / COUNT(br.id)')
->where('b.id = ' . (bool) $this->id);
echo $q->getSqlQuery();
Will return SELECT b.id AS b_id, br.id AS br_id FROM b WHERE b.id = 1
Anybody that can help me?
Related
I want to do query both result and count with group_by.
$id= 2;
$limit = 10;
$offset = ($id - 1) * $limit;
$this->db->group_by('BE.etiket_id');
$this->db->from('bulten_icerigi_etiketler as BE');
$this->db->join('etiketler as E', 'BE.etiket_id=E.id', 'left');
$this->db->join('bulten_icerigi as BI', 'BE.bulten_id=BI.id', 'left');
$count = $this->db->count_all_results('', false);
$this->db->limit($limit, $offset);
$this->db->order_by('E.adi');
$results = $this->db->get()->result();
However, I have error
Error Number: 1054
Unknown column 'tbl_`E.adi' in 'field list'
Can I use count_all_results() and result() together ?
Thank you.
You can't group by columns which you didn't selected in select.
Try to use E.adi and E.id instead of BE.etiket_id in your group by.
Your query should looks like:
SELECT
COUNT(*) as count,
tbl_E.adi,
tbl_E.id
FROM tbl_bulten_icerigi_etiketler as BE
LEFT JOIN tbl_etiketler as E ON BE.etiket_id=E.id
LEFT JOIN tbl_bulten_icerigi as BI ON BE.bulten_id=BI.id
GROUP BY tbl_E.adi, tbl_E.id // here you need to change
ORDER BY tbl_E.adi
$this->db->group_by(array("E.adi", "E.id"));
I need assistance to build up the query like below in laravel:
SELECT *
FROM table t
WHERE t.a = 1
OR (t.a=0
AND t.id IN (
SELECT o.a_id
FROM other_table o
WHERE o.x > 3
)
);
You could try to build your exact current query, and in fact it might even be the most efficient to write it. But, if we rephrase your query using a left join, it becomes somewhat easier to express in Laravel code.
SELECT *
FROM your_table t
LEFT JOIN other_table o
ON t.id = o.a_id AND o.x > 3
WHERE
t.a = 1 OR
(t.a = 0 AND o.a_id IS NOT NULL);
This would translate to the following Laravel code:
$result = DB::table('your_table t')
->leftJoin('other_table o', function($join) {
$join->on('t.id', '=', 'o.a_id');
$join->on('o.x', '>', '3');
})
->where('t.a', '=', '1')
->orWhere(function($query) {
return $query->where('t.a', '=', '0')
->whereNotNull('o.a_id')
})
->get();
I've created MySQL query for searching results. The query works properly but if I use laravel pagination on this query I get an error
Call to a member function paginate() on array ,
How to solve this with pagination?
$search = \DB::SELECT("SELECT l.*,
r.id AS reservation_id,
r.status AS res_status,
r.start_date,
r.end_date,
Avg(r.rating) AS rating
FROM listings l
LEFT JOIN reservations r ON r.listing_id = l.id
WHERE ( ( Cast('" . $search_date . "' AS date) NOT BETWEEN r.start_date AND r.end_date ) OR r.status = 'Cancel' )
AND l.city_id = $city_id
AND l.persons >= $guests
AND l.listing_type_id = $type
GROUP BY l.id
ORDER BY r.rating DESC
");
DB::select() will return an array. You could probably convert your query to a Query Builder and then use paginate() on it. For example:
$search = DB::table('listings')
->selectRaw('listings.*, reservations.id AS reservation_id, reservations.status AS res_status, reservations.start_date, reservations.end_date, AVG(reservations.rating) as rating')
->leftJoin('reservations', 'reservations.listing_id', 'listings.id')
->whereRaw("(CAST(? AS date) NOT BETWEEN reservations.start_date AND reservations.end_date) or reservations.status = 'Cancel'", [$search_date])
->where('listings.city_id', '=', $city_id)
->where('listings.persons', '>=', $guests)
->where('listings.listing_type_id', '=', $type)
->groupBy('listings.id')
->orderBy('reservations.rating', 'DESC')
->paginate(10);
I need to reproduce with doctrine a AND statement from an sql query
AND ((SELECT e2.candidature_id_ca FROM evaluateur_candidature e2 WHERE
e.candidature_id_ca = e2.candidature_id_ca AND e2.role_evaluateur =
'evaluateur' ) IS NULL )
It's so easy to reproduce this with doctrine i tried :
$query->andWhere('(SELECT e2.candidature_id_ca FROM evaluateur_candidature e2 WHERE
e.candidature_id_ca = e2.candidature_id_ca AND e2.role_evaluateur = ?)' , 'evaluateur' , 'IS NULL ');
But i get
Couldn't find class e2
Someone know how i could do it ? thanks in advance
UPDATE here the full doctrine query :
$query = Doctrine_Query::create()
->from('Candidature c')
->innerJoin('c.RoleActeur ra_cand ON c.id_ca = ra_cand.id_ca AND ra_cand.id_role = 4')
->innerJoin('ra_cand.Acteur ac_cand')
->innerJoin('c.CandidatureActeur ca ON c.id_ca = ca.id_ca')
->where('c.id_stca = 5');
$query->leftJoin('c.EvaluateurCandidature ec');
//here i would like to get the "Candidature collection" who not have records in the table EvaluateurCandidature when the column role_evaluateur is set to "evaluation".
//$query->andWhere('ec.role_evaluateur = ? ) ? )', 'evaluateur' ,'IS NULL ');
$query->andWhere('(SELECT e2.candidature_id_ca FROM evaluateur_candidature e2 WHERE
e.candidature_id_ca = e2.candidature_id_ca AND e2.role_evaluateur = ?)' , 'evaluateur' , 'IS NULL ');
As per your description you could include another condition on join EvaluateurCandidature to join only records where role_evaluateur is set to "evaluation" after that select only records where id of EvaluateurCandidature is null
$query = Doctrine_Query::create()
->from('Candidature c')
->innerJoin('c.RoleActeur ra_cand ON c.id_ca = ra_cand.id_ca AND ra_cand.id_role = 4')
->innerJoin('ra_cand.Acteur ac_cand')
->innerJoin('c.CandidatureActeur ca ON c.id_ca = ca.id_ca')
->leftJoin("c.EvaluateurCandidature ec WITH ec.role_evaluateur = 'evaluation'")
->where('c.id_stca = 5')
->andWhere('ec.id IS NULL');
WITH keyword
I tried to send SQL request with LEFT JOIN but it doesn't display data from table2 table.
public static function top($limit)
{
return self::findBySql("
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON (g1.id = s1.g_id AND s1.id = (
SELECT MAX(id)
FROM table2 s2 WHERE s2.g_id = g1.id
))
LIMIT :limit",
[':limit' => $limit]
)->all();
}
It seems you are adding this function to the model and self represents the model itself.
Yii will not return results from another table and will be limited to the model only if you are calling the find on a model, instead you need to use a db query as below:
$query = new \yii\db\Query;
$query->select('*')
->from('table 1 g1')
->leftJoin('table2 s1', 's1.g_id AND s1.id = (SELECT MAX(id) FROM table2 s2 WHERE s2.g_id = g1.id')
->limit($Limit);
$command = $query->createCommand();
$resp = $command->queryAll();
The correct SQL query is
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON g1.some_field = s1.some_field
where g1.some_field = s1.some_field are the fields that define the join.
I have working code something like...:)
with user and user_friend_list
$query = new Query;
$query ->select(['user.id AS user_id', 'user_friend_list.id'])
->from('user')
->innerJoin('user_friend_list', 'user.email = user_friend_list.email');
$command = $query->createCommand();
$data = $command->queryAll();
foreach($data as $datakey)
{
//echo $datakey['id'];
$friendfind = UserFriendList::findOne($datakey['id']);
$friendfind->is_app_using_user_id=$datakey['user_id'];
$friendfind->save();
}