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
Related
I'm facing this ambiguously defined error. I understand this is due to alias not defined properly but I am not able to understand in the below query why this error is coming. I'm using Oracle.
SELECT
papf.person_number as personnumber,
paam.position_id as position,
fabu.bu_name
FROM
(SELECT
papf.person_number,
paam.person_id,
fabu.bu_name,
fabu.bu_id,
paam.assignment_id,
paam.assignment_number,
paam.action_code,
paam.effective_start_date,
wft.outcome,
htd.state,
htd.status,
Count(paam.assignment_id)
over (
PARTITION BY paam.assignment_id) rowcnt
FROM
per_all_assignments_m paam,
per_all_people_f papf,
hrc_txn_header hth,
hrc_arm_process_vl hapv,
hrc_txn_data htd,
fa_fusion_soainfra.wftask wft,
fun_all_business_units_v fabu
WHERE
paam.primary_assignment_flag = 'Y'
AND paam.effective_latest_change = 'Y'
AND paam.assignment_status_type = 'ACTIVE'
AND paam.action_code IN ( 'GLB_TRANSFER', 'TRANSFER' )
AND hth.subject = 'PER_ALL_PEOPLE_F'
AND hth.subject_id = paam.person_id
AND hth.object IN ( 'PER_ALL_ASSIGNMENTS_M', 'PER_ALL_PEOPLE_F' )
AND hth.object_id = Decode(hth.object, 'PER_ALL_ASSIGNMENTS_M', paam.assignment_id,
'PER_ALL_PEOPLE_F', paam.person_id)
AND hapv.process_id = hth.process_id
AND hapv.txn_module_identifier IN ( 'Transfers', 'ManageEmployment', 'AddWorkRelationship' )
AND htd.transaction_id = hth.transaction_id
AND wft.identificationkey = To_char(hth.transaction_id)
AND SYSDATE BETWEEN papf.effective_start_date AND papf.effective_end_date
AND paam.person_id = papf.person_id
and sysdate between fabu.date_from and fabu.date_to
and fabu.status='A'
and paam.business_unit_id=fabu.bu_id
GROUP BY papf.person_number,
paam.person_id,
paam.assignment_id,
fabu.bu_name,
fabu.bu_id,
paam.assignment_number,
paam.action_code,
paam.effective_start_date,
wft.outcome,
htd.state,
htd.status) t1,
per_all_people_f papf,
per_all_assignments_m paam,
fun_all_business_units_v fabu
WHERE rowcnt = 1
AND outcome = 'APPROVE'
AND state = 'COMPLETE'
AND status = 'APPROVED'
AND paam.person_id = t1.person_id
AND paam.action_code = t1.action_code
AND paam.assignment_status_type = 'INACTIVE'
AND paam.assignment_sequence = 1
AND paam.effective_start_date = t1.effective_start_date
AND paam.assignment_type = 'E'
and papf.person_id = t1.person_id
AND papf.effective_start_date = t1.effective_start_date
AND fabu.bu_id = t1.bu_id
I'm defining the paam table and papf table the same way and not facing issue in them.
Why I am facing this error when I try to add fabu table in end ?
You forgot to add t1. when refer to some columns that can be found in other tables:
WHERE t1.rowcnt = 1
AND t1.outcome = 'APPROVE'
AND t1.state = 'COMPLETE'
AND t1.status = 'APPROVED'
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();
}
I want to add following where clause to Linq query. How subquery like below using linq
WHERE (Restaurants.[IsActive] = 1)
AND exists
(
select 1 from APIKeys
where ApiKey = 'on35e5xbt3m4cbcef4e4448t6wssg11o'
and (KeyType = 1
and fk_RestaurantsID = [t2].[RestaurantsID]
or KeyType = 2
and fk_RestaurantGroupID = RG.RestaurantGroupsID
and [t1].[fk_RestaurantsID] in
(SELECT RestaurantsID
FROM Restaurants
WHERE RestaurantGroupsID = RG.RestaurantGroupsID))
)
AND (0 = (COALESCE([t0].[fk_MembersID],0)))
AND (1 = [t0].[fk_BookingStatusID])
AND ([t0].[Email] = 'nike.s#gmail.com')
AND (([t0].[Phone] = '9999999990') OR ([t0].[MobilePhone] = '9999999990'))
Use Any() to produce subquery which translated to EXISTS. E.g. with AdventureWorks database sample:
from p in Products
where p.FinishedGoodsFlag &&
SalesOrderDetails.Any(od => od.ProductID == p.ProductID)
select new { p.ProductID, p.Name }
Will produce following query to database:
SELECT [t0].[ProductID], [t0].[Name]
FROM [Production].[Product] AS [t0]
WHERE ([t0].[FinishedGoodsFlag] = 1) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM [Sales].[SalesOrderDetail] AS [t1]
WHERE [t1].[ProductID] = [t0].[ProductID]
))
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?
How can I migrate this stored procedure into LINQ
DECLARE #customerIDs TABLE (customerID INT)
DECLARE #c INT
if (#customerName != '')
BEGIN
INSERT INTO #customerIDs
select id from customer
where name like '%' + #customerName + '%'
SELECT #c = COUNT(*) FROM #customerIDs
END
else
SET #c = 0
-- Insert statements for procedure here
SELECT DISTINCT l.*, dbo.GetListOfBranches(l.id) as 'list_of_branches', dbo.GetTopLevelCustomer(cid.customerID) FROM [login] l
LEFT JOIN login_to_customer ltc ON ltc.login_id = l.id
LEFT JOIN #customerIDs cid ON cid.customerID = ltc.customer_id
WHERE
(#c = 0 OR ltc.customer_id = cid.customerID) AND
(l.surname LIKE '%' + ISNULL(#surname, l.surname) + '%') AND
(l.forename LIKE '%' + ISNULL(#forename, l.forename) + '%') AND
(user_type = ISNULL(#userType, user_type)) AND
(l.active = ISNULL(#active, l.active))
ORDER BY surname ASC
I have got this so far:
List<Int32> customerIds = new List<int>();
if (!String.IsNullOrEmpty(customerName))
{
CustomerDataContext custDataContext = new CustomerDataContext();
customerIds = (from a in custDataContext.Customers
where a.name.Contains(customerName)
select a.id).ToList<Int32>();
}
LoginDataContext dataContext = new LoginDataContext();
var query = (from t in dataContext.logins
join t2 in dataContext.login_to_customers on t.id equals t2.login_id into a
from subquery in a.DefaultIfEmpty()
select new { t, listOfBranches = dataContext.GetListOfBranches(t.id), topLevelCustomer = dataContext.GetTopLevelCustomer(t.id) });
I need would then do a
if (customerIds.Count() > 0)
{
query = query.Where(a => customerIds.Contains(a.t.customer_id))
}
but as I need to perform the distinct I cant have the customer_id column in the query. Is there a way to include the
LEFT JOIN #customerIDs cid ON cid.customerID = ltc.customer_id
WHERE
(#c = 0 OR ltc.customer_id = cid.customerID)
Within the LINQ?
Thanks,
Richard