如何在JOIN Codeigniter中进行子查询?

时间:2015-03-15 09:21:26

标签: php codeigniter

我尝试使用 Active Records CI SELECT中进行子查询LEFT JOIN,但是它出错了:

near 'b.MedicalFacilitiesIdUser FROM medicalfacilities AS b WHERE b.MedicalFacilitiesI'

如何看,查询被扭曲:b.MedicalFacilitiesI

查询是:

$this->db->join('stocks AS stn', 'stn.stocksIdMF = 
(SELECT b.MedicalFacilitiesIdUser FROM medicalfacilities AS b 
WHERE b.MedicalFacilitiesIdUser = stn.stocksIdMF AND stn.stocksEndDate >= UNIX_TIMESTAMP()
ORDER BY stn.stocksId DESC LIMIT 1)', 'LEFT');

现在完整查询是:

$this->db->select("MedicalFacilitiesName, MedicalFacilitiesAdres, MedicalFacilitiesDescription,
            MedicalFacilitiesView, medicalfacilities.country, medicalfacilities.city, MedicalFacilitiesPhoto,
            MedicalFacilitiesIdUser, idMedicalFacilities, id_specialization, id_MF, idUsers, UsersTypeAccount,
            COUNT(commentstomedicalfacilities.idCommentsToMedicalFacilities) AS CNT,
            COUNT(DISTINCT(stocks.stocksId)) AS count_stocks,
            COUNT( su.idSubscrubeToUsers ) AS SBS,
            coalesce(ROUND((SUM(ev.evaluationinstitutionEvalTotal) / SUM(ev.evaluationinstitutionEvalTotalRate))), 0) AS rate,
            stocks.stocksName, stocks.stocksId, stocks.stocksEndDate, stocks.stocksStartDate,
            typesmedicalfacilities.name AS nameType", FALSE);

        $this->db->from('medicalfacilities');
        $this->db->join('users', 'users.idUsers = medicalfacilities.MedicalFacilitiesIdUser');
        $this->db->join('medicalfacilities_directions', 'medicalfacilities_directions.id_MF = medicalfacilities.MedicalFacilitiesIdUser', 'LEFT');
        $this->db->join('subscrubetousers su', 'su.SubscrubeToUsersIdNote = medicalfacilities.MedicalFacilitiesIdUser AND su.SubscrubeToUsersType = 9', 'LEFT');
        $this->db->join('thematicspecialization', 'thematicspecialization.idSpecialization = medicalfacilities_directions.id_specialization', 'LEFT');
        $this->db->join('evaluationinstitution ev', 'ev.evaluationinstitutionIdInst = medicalfacilities.MedicalFacilitiesIdUser', 'LEFT');
        $this->db->join('stocks', 'stocks.stocksIdMF = medicalfacilities.MedicalFacilitiesIdUser AND stocks.stocksEndDate >= UNIX_TIMESTAMP()', 'LEFT');
        $this->db->join('commentstomedicalfacilities', 'commentstomedicalfacilities.CommentsToMedicalFacilitiesIdMedical = medicalfacilities.MedicalFacilitiesIdUser', 'LEFT');
        $this->db->join('medicalfacilities_type', 'medicalfacilities_type.idMF = medicalfacilities.MedicalFacilitiesIdUser');
        $this->db->join('typesmedicalfacilities', 'typesmedicalfacilities.type = medicalfacilities_type.type');
        $this->db->join('stocks AS stn', 'stn.stocksIdMF = (SELECT b.MedicalFacilitiesIdUser FROM medicalfacilities AS b WHERE b.MedicalFacilitiesIdUser = stn.stocksIdMF AND stn.stocksEndDate >= UNIX_TIMESTAMP()
        ORDER BY stn.stocksId DESC LIMIT 1)', 'LEFT');

0 个答案:

没有答案