CakePHP对多个表的不同查询

时间:2012-10-11 09:55:01

标签: mysql cakephp left-join distinct

我需要在以下查询中进行不同的搜索:

$listings = $this->Listing->query("SELECT DISTINCT(Listing.listnumber),Listing.*, ClientEvent.*, Client.*, UserLog.*, Agent.* FROM listings Listing
                        LEFT JOIN client_events ClientEvent ON ClientEvent.listnumber = Listing.listnumber
                        LEFT JOIN clients Client ON Client.clnt_id = ClientEvent.clnt_id
                        LEFT JOIN user_logs UserLog ON UserLog.ul_item_id = Listing.listnumber
                        LEFT JOIN agents Agent ON Agent.agnt_id = Listing.agnt_id
                        WHERE Listing.off_id = '{$off_id}' AND (UserLog.ul_type = 'listing.new_listing' OR Listing.listnumber LIKE '3330%')");

然而,这给了我正确的结果,但重复。我需要DISTINCT结果。

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

    $listings = $this->Listing->query("SELECT Listing.listnumber FROM listings Listing
                            LEFT JOIN client_events ClientEvent ON ClientEvent.listnumber = Listing.listnumber
                            LEFT JOIN clients Client ON Client.clnt_id = ClientEvent.clnt_id
                            LEFT JOIN user_logs UserLog ON UserLog.ul_item_id = Listing.listnumber
                            LEFT JOIN agents Agent ON Agent.agnt_id = Listing.agnt_id
                            WHERE Listing.off_id = '{$off_id}' AND (UserLog.ul_type = 'listing.new_listing' OR Listing.listnumber LIKE '3330%') group by Listing.listnumber");
----------------------
Put your extar column not a tablename.*...

    $listings = $this->Listing->query("SELECT Listing.listnumber, colname1,.. FROM listings Listing
                            LEFT JOIN client_events ClientEvent ON ClientEvent.listnumber = Listing.listnumber
                            LEFT JOIN clients Client ON Client.clnt_id = ClientEvent.clnt_id
                            LEFT JOIN user_logs UserLog ON UserLog.ul_item_id = Listing.listnumber
                            LEFT JOIN agents Agent ON Agent.agnt_id = Listing.agnt_id
                            WHERE Listing.off_id = '{$off_id}' AND (UserLog.ul_type = 'listing.new_listing' OR Listing.listnumber LIKE '3330%') group by Listing.listnumber");