I did this Query in SQL but I can't make it work in Eloquent.
Please help me.
SELECT
tags.desc,
COUNT(planificacion_info.id_area) as cantidad_intervenciones
FROM
tags
INNER JOIN planificacion_info ON planificacion_info.id_area = tags.id_tag
WHERE
tags.grupo = 'area' and tags.estado = true
GROUP BY
tags.desc
You can do it like this :
$result = \DB::table('tags')->selectRaw('tags.desc, COUNT(planificacion_info.id_area) as cantidad_intervenciones')
->join('planificacion_info', 'planificacion_info.id_area', '=', 'tags.id_tag')
->where('tags.grupo', 'area')
->whereRaw('tags.estado = true')
->groupBy('tags.desc')
->get();
which gives you this request :
SELECT
tags.desc,
COUNT(planificacion_info.id_area) AS cantidad_intervenciones
FROM
`tags`
INNER JOIN `planificacion_info` ON `planificacion_info`.`id_area` = `tags`.`id_tag`
WHERE
`tags`.`grupo` = 'area'
AND tags.estado = TRUE
GROUP BY
`tags`.`desc`
Would be something liek this
$tags= DB::table('tags')
->select('tags.desc',DB::raw("COUNT(planificacion_info.id_area) AS cantidad_intervenciones")
->innerJoin('planificacion_info','planificacion_info.id_area','=','tags.id_tag')
->where('tags.grupo','area')
->where('tags.estado',true)
->groupBy('tags.desc')
->get();
Related
I need to write select statement like that:
SELECT co.id FROM client_order co
INNER JOIN client_order_status cos ON cos.id = co.order_status_id AND cos.name IN ('for_shipping', 'to_be_shipped_later')
WHERE NOT EXISTS (SELECT 1 FROM dpackage dp WHERE dp.order_id = co.id AND dp.is_spec_label_generated = 1)
ORDER BY co.id
In Eloquent my expression looks like that:
$clientOrderEntities = ClientOrder::join('client_order_status', 'client_order_status.id', '=', 'order_status_id')
->whereIn('client_order_status.name', ['for_shipping', 'to_be_shipped_later'])
->whereNotExists(function($query) use($orderId) {
$query->select(DB::raw(1))
->from('dpackage')
->where([
['order_id', '=', $orderId]
['is_spec_label_generated', '=', 1]
]);
})->get();
I don't know how to pass orderId from first part of query into whereNotExists sub query
At the moment it looks:
$clientOrderEntities = ClientOrder::join('client_order_status', 'client_order_status.id', '=', 'order_status_id')
->whereIn('client_order_status.name', ['for_shipping', 'to_be_shipped_later'])
->whereNotExists(function($query) {
$query->select(DB::raw(1))
->from('dpackage')
->where([
['order_id', '=', 'client_order.id'],
['is_spec_label_generated', '=', 1]
]);
})
->select('client_order.*')
->get();
This query works:
$clientOrderEntities = ClientOrder::join('client_order_status', 'client_order_status.id', '=', 'order_status_id')
->whereIn('client_order_status.name', ['for_shipping', 'to_be_shipped_later'])
->whereRaw(' NOT EXISTS (SELECT 1 FROM dpackage dp WHERE dp.order_id = client_order.id AND dp.is_spec_label_generated = 1) ')
->select('client_order.*')
->get();
This query works too and is faster than eloquent statements, as #Newbie wrotes, but I don't know why his answer has been deleted:
$clientOrderEntities = DB::select('SELECT co.* FROM client_order co INNER JOIN client_order_status cos ON cos.id = co.order_status_id AND cos.name IN ("for_shipping", "to_be_shipped_later") WHERE NOT EXISTS (SELECT 1 FROM dpackage dp WHERE dp.order_id = co.id AND dp.is_spec_label_generated = 1) ');
Maybe defining name for the tables help you with this.
$clientOrderEntities = \DB::table('client_order as co')
->join('client_order_status as cos', 'cos.id', '=', 'co.order_status_id')
->whereIn('cos.name', ['for_shipping', 'to_be_shipped_later'])
->whereNotExists(function($query) {
$query->select(DB::raw(1))
->from('dpackage')
->where([
['order_id', '=', 'co.id'],
['is_spec_label_generated', '=', 1]
]);
})->get();
I need to convert this query that I got from Access to an Laravel eloquent for my remake database project, and this is what i did but i haven't the same result with the origin sql thanks.
SELECT T_Sites.Code_Region, T_Sites.Nom_Site, T_CLM.N_CLM, T_Port.N_Port,
T_Vlan.IP_Vlan, T_Ref_Vlan.Id_Referentiel, T_Ref_Vlan.Nom_Vlan,
T_Vlan.Dt_Creat, T_Vlan.Id_Vlan, T_Vlan.IP_HSRP, T_Vlan.IP_SousReseaux,
T_CLM.Id_CLM, T_Vlan.Masque
FROM (T_CLM
INNER JOIN (
(T_Port INNER JOIN T_Vlan ON T_Port.Id_Port = T_Vlan.Id_Port)
LEFT JOIN T_Ref_Vlan ON T_Vlan.Id_Referentiel = T_Ref_Vlan.Id_Referentiel
)
ON T_CLM.Id_CLM = T_Port.Id_CLM)
INNER JOIN T_Sites ON T_CLM.Id_Site = T_Sites.Id_Site
ORDER BY T_CLM.Id_CLM DESC
WITH OWNERACCESS OPTION;
// what i did
$SuiviVlan = DB::table('T_CLM')
->join('T_Sites', 'T_CLM.Id_Site', '=', 'T_Sites.Id_Site')
->join('T_Port', 'T_CLM.Id_CLM' , '=', 'T_Port.Id_CLM')
->join('T_Vlan', 'T_Vlan.Id_Port', '=', 'T_Port.Id_Port')
->leftjoin('T_Ref_Vlan', 'T_Ref_Vlan.Id_Referentiel', '=', 'T_Vlan.Id_Referentiel')
->select('T_Sites.Code_Region','T_Sites.Nom_Site','T_CLM.N_CLM','T_Port.N_Port','T_Vlan.IP_Vlan',
'T_Ref_Vlan.Id_Referentiel', 'T_Ref_Vlan.Nom_Vlan', 'T_Vlan.Dt_Creat', 'T_Vlan.Id_Vlan', 'T_Vlan.IP_HSRP',
'T_Vlan.IP_SousReseaux', 'T_CLM.Id_CLM', 'T_Vlan.Masque')
->orderBy('T_CLM.Id_CLM', 'desc')
->get();
// It was the correct laravel eloquent , thanks btw for help.
$SuiviVlan = DB::table('T_CLM')
->join('T_Sites', 'T_CLM.Id_Site', '=', 'T_Sites.Id_Site')
->join('T_Port', 'T_CLM.Id_CLM' , '=', 'T_Port.Id_CLM')
->join('T_Vlan', 'T_Vlan.Id_Port', '=', 'T_Port.Id_Port')
->leftjoin('T_Ref_Vlan', 'T_Ref_Vlan.Id_Referentiel', '=', 'T_Vlan.Id_Referentiel')
->select('T_Sites.Code_Region','T_Sites.Nom_Site','T_CLM.N_CLM','T_Port.N_Port','T_Vlan.IP_Vlan',
'T_Ref_Vlan.Id_Referentiel', 'T_Ref_Vlan.Nom_Vlan', 'T_Vlan.Dt_Creat', 'T_Vlan.Id_Vlan', 'T_Vlan.IP_HSRP',
'T_Vlan.IP_SousReseaux', 'T_CLM.Id_CLM', 'T_Vlan.Masque')
->orderBy('T_CLM.Id_CLM', 'desc')
->get();
I would like to execute the following query using Laravel's eloquent query builder:
SELECT a.*, b.day, c.sex FROM hours as a
left join reserves as b
on a.id = b.hour_id and b.day = '2015-12-12'
left join doctors as c
on a.doctor_id = c.id
where a.doctor_id = 1;
I have tried the following:
Hour::leftJoin('reserves', function($join) {
$join->on('hours.id' , '=' , 'reserves.hour_id')
->where('reserves.day' , '=' , '2015-12-12');
})
->leftJoin('doctors', function($join) {
$join->on('hours.doctor_id', '=', 'doctors.id');
})
->where('hours.doctor_id', '=', $doctorId)
->get();
Unfortunately, I am not getting the same results.
Everything looks fine, however you miss selecting columns, after Hour:: you should add:
select('hours.*'.'reserves.day','doctors.sex')->
You could also make second join simpler and use aliases, so the final code could look like this:
Hour::select('hours.*'.'reserves.day','doctors.sex')
->from('hours AS a')
->leftJoin('reserves AS b', function($join) {
$join->on('a.id' , '=' , 'b.hour_id')
->where('b.day' , '2015-12-12');
})
->leftJoin('doctors AS c', 'a.doctor_id', '=', 'c.id')
->where('a.doctor_id', $doctorId)
->get();
Of course as $doctorId you should set 1 to get exact same result
you can use DB::raw :
Hour::select(DB::raw('hours.*'),reserves.day, c.doctors)
->leftJoin('reserves',DB::raw("hours.id = reserves.hour_id AND reserves.day='2015-12-12'") ,DB::raw(''), DB::raw(''))
->leftJoin('doctors','hours.doctor_id', '=', 'doctors.id')
->where('hours.doctor_id', '=', $doctorId)
->get()
the 2 empty DB::raw is because leftJoin expect 4 arguments(table,columnA,operator,columnB)
I have this working query:
$q = $this->db->query('SELECT u.name FROM users u
JOIN user_group ug ON u.id = ug.user_id
JOIN groups g ON g.id = ug.group_id WHERE ug.group_id = "4" ');
And I want to replace it with aactive record. I come up with something like this but obviously it doesn't work :
$this->db->select('name');
$this->db->from('users');
$this->db->join('user_group', 'user_group.user_id = users.id);
$this->db->join('groups', 'groups.id = user_group.group_id');
$q = $this->db->get();
Thanks
Leron
I think you forget add where clause. And there was single quote missing.
$this->db->select('name');
$this->db->from('users');
$this->db->join('user_group', 'user_group.user_id = users.id');
$this->db->join('groups', 'groups.id = user_group.group_id');
$this->db->where('user_group.group_id', 4);
$q = $this->db->get();
I have this query:
SELECT g.title, g.asin, g.platform_id, r.rank
FROM games g
INNER JOIN ranks r ON ( g.id = r.game_id )
ORDER BY r.rank DESC
LIMIT 5`
Now, this is my JOIN using Zend_Db_Select but it gives me array error
$query = $this->select();
$query->from(array('g' => 'games'), array());
$query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('g.title', 'g.asin', 'g.platform_id', 'r.rank'));
$query->order('r.rank DESC');
$query->limit($top);
$resultRows = $this->fetchAll($query);
return $resultRows;
Anyone know what I could be doing wrong? I want to get all the columns in 'games' to show and the 'rank' column in the ranks table.
I am going to assume you've solved this, but it would be nice to leave the answer for others.
Add this below the instantiation of the select object.
$query->setIntegrityCheck(false);
You could also type fewer characters....
$query = $this->select()
->from(array('g' => 'games'), array('title', 'asin', 'platform_id'))
->join(array('r' => 'ranks'), 'g.id = r.game_id', array('rank'))
->order('r.rank DESC')
->limit($top);
return $this->fetchAll($query);
Good luck!
Here's how I'd write it:
$query = $this->select();
$query->from(array('g' => 'games'), array('title', 'asin', 'platform_id'));
$query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('rank'));
$query->order('r.rank DESC');
$query->limit($top);
$resultRows = $this->fetchAll($query);
return $resultRows;
Other example:
select n.content, n.date, u.mail
from notes n, users u
where n.id_us=u.id and reminder=current_date
$query = $this->select()
->from(array('n'=>'notes'),
array('content', 'date'))
->join(array('u'=>'users'), 'n.id_us=u.id and n.reminder=current_date',
array('mail'))
->setIntegrityCheck(false);
return $this->fetchAll($query);
That's work fine :)