我在cakePHP中有兩個表。CakePHP嵌套兩個select查詢
competencies
------------
id
name
competenceRatings
-----------------
id
competence_id
user_id
rating
我需要一種方法來寫在蛋糕的方式下面的查詢:
SELECT * FROM能力WHERE ID NOT IN(SELECT competence_id FROM competence_ratings WHERE僱員= $用戶id)
有人請幫我!!
我做了什麼,纔去這個子查詢方法:
我試圖competencies-> hasMany-> competenceRatings,competenceRatings-> belongsTo->競爭力的關係。
$competencies = $this->Competence->CompetenceRating->find('all',array('CompetenceRating.user_id' => $userId,'CompetenceRating.competence_id !=' => 'Competence.id'));
我希望能夠獲得用戶沒有對能力評分表進行任何評分的能力的名稱。即,我需要能力表中沒有條目的能力表中的名稱列表(對於給定的user_id)。
編輯
我試着表中還加入:
$options['joins'] = array(
array(
'table' => 'competence_ratings',
'alias' => 'CompetenceRating',
'type' => 'LEFT OUTER',
'conditions' => array(
'Competence.id = CompetenceRating.competence_id'
)
)
);
$options['conditions'] = array('CompetenceRating.employee_id' => $employee['Employee']['id']);
$competencies = $this->Competence->find('all',$options);
你到目前爲止做了什麼? – mark 2012-08-04 18:33:31
我正在談論實際的代碼。而不是隻發佈預期的結果,你也應該發佈你到目前爲止的代碼。你可以編輯並完成你的問題。 – mark 2012-08-04 18:44:19
你再問同樣的問題2或3小時你問「CakePHP表加入兩個表問題」這個問題 – 2012-08-04 18:44:50