我有一个 SQL 查询,我正试图将其转换为 CakePHP 查找,但我不知道如何构建它......有人可以提供一些帮助吗?
SELECT texts.*, people.id, people.first_name, people.last_name FROM texts
NATURAL JOIN (
SELECT person_id, MAX(id) AS id
FROM texts
WHERE texts.status = 'received'
GROUP BY person_id
) t RIGHT JOIN people ON people.id = t.person_id
WHERE texts.body is not null
AND texts.created > '$since'
AND person.counselor_id = '2'
ORDER BY texts.created DESC
这是我所拥有的
$texts = $this->find('all', array(
'recursive' => -1,
'joins' => array(
array(
'table' => 'texts',
'alias' => 't',
'type' => 'NATURAL',
'conditions' => array('t.status' => 'received')
),
array(
'table' => 'people',
'alias' => 'Person',
'type' => 'RIGHT',
'conditions' => 'people.id = t.person_id'
)
),
'conditions' => array('AND' => array('Text.body IS NOT NULL', 'Text.created > 0000-00-00 00:00:00')),
'order' => 'Text.created DESC'
));
这是它编写的 SQL
SELECT Text.id, Text.person_id, Text.sid, Text.to, Text.from, Text.body, Text.status,
Text.direction, Text.owner, Text.counselor_read, Text.created, Text.modified
FROM admissionsedge_penfield.texts AS Text
NATURAL JOIN admissionsedge_penfield.texts
AS t ON (t.status = 'received')
RIGHT JOIN admissionsedge_penfield.people AS Person ON (people.id = t.person_id)
WHERE ((Text.body IS NOT NULL) AND (Text.created > 0000-00-00 00:00:00))
ORDER BY Text.created DESC
谢谢!