MySQL排序按多列并具有混合结果

时间:2014-10-03 22:32:14

标签: php mysql

说我想通过3列获取我的数据和订单

我目前有:

 ORDER BY e.HomePage DESC, e.FeaturedProfile DESC, e.DateModified DESC

这将返回HomePage 1st,然后返回FeaturedProfile 2nd这些,其余结果按DateModified排序(某些配置文件既不是HomePage也不是FeaturedProfile,因此这些显示在下面并仅在DateModified上排序)

我想要的第一个结果是由DateModified排序的HomePage和FeaturedProfile的混合,然后是任何其他结果(以下都不是HomePage或FeaturedProfile)

Idealy我想在1个查询中完成所有这些

编辑:

下面是完整的查询(包含php代码+我已经替换了绑定变量以便于阅读): HomePage和FeaturedProfile的字段是布尔值 (这是我的主要搜索功能所以对不起,很安静大) 理想情况下,我希望返回所有结果,但如果HomePage和FeaturedProfile = 1,我首先需要这些以及下面的任何其他内容。我希望HomePage和FeaturedProfile混合在一起,但仍然高于其他结果(我可以用2个查询做到这一点,但我希望在单个查询中做到整洁)

$query = "SELECT DISTINCT e.EscortID,
                e.EscortName,
                e.EscortEmail,
                e.EscortTelephone,
                e.EscortWebsite,
                e.InCallLocation,
                e.HairColour,
                g.GenderDescription,
                n.NationalityName,
                et.EthnicityName,
                ((date_format(now(),'%Y') - date_format(e.DateOfBirth,'%Y')) - (date_format(now(),'00-%m-%d') < date_format(e.DateOfBirth,'00-%m-%d'))) AS Age,
                SUBSTRING_INDEX(e.EscortProfile,' ', 24) AS Description,
                p.PhotoAlt,
                p.PhotoURL,
                e.ShowReviews
                FROM tEscort e
                INNER JOIN tEscortArea ea ON e.EscortID = ea.tEscort_EscortID
                INNER JOIN tEthnicity et ON e.tEthnicity_EthnicityID = et.EthnicityID
                INNER JOIN tNationality n ON  e.tNationality_NationalityID = n.NationalityID
                INNER JOIN tGender g ON e.tGender_GenderID = g.GenderID AND g.Enabled=1
                LEFT JOIN tEscortAgency eag ON e.EscortID = eag.tEscort_EscortID
                LEFT JOIN tAgency a ON a.AgencyID = eag.tAgency_AgencyID 
                LEFT JOIN (tEscortPhoto ep INNER JOIN tPhoto p ON (ep.tPhoto_PhotoID = p.PhotoID AND p.Enabled=1)) 
                    ON e.EscortID = ep.tEscort_EscortID AND ep.ProfilePhoto = 1
                LEFT JOIN (tEscortEnjoyments ee INNER JOIN tEnjoyment en ON (ee.tEnjoyment_EnjoymentID = en.EnjoymentID AND en.Enabled=1))
                    ON e.EscortID = ee.tEscort_EscortID
                LEFT JOIN (tEscortServices es INNER JOIN tService s ON (s.ServiceID = es.tService_ServiceID AND s.Enabled=1)) 
                    ON e.EscortID = es.tEscort_EscortID
                WHERE e.Enabled=1
                AND e.Active=1
                AND e.AvctivePayments=1";

            if(!empty($areas)){$query .= " AND ea.tArea_AreaID IN($areas)";}
            if(!empty($enjoyments)){$query .= " AND ee.tEnjoyment_EnjoymentID IN($enjoyments)";}
            if(!empty($ethnicities)){$query .= " AND e.tEthnicity_EthnicityID IN($ethnicities)";}
            if(!empty($genders)){$query .= " AND e.tGender_GenderID IN($genders)";}
            if(!empty($nationalities)){$query .= " AND e.tNationality_NationalityID IN($nationalities)";}
            if(!empty($services)){$query .= " AND es.tService_ServiceID IN($services)";}
            if(!empty($hair)){$query .= " AND e.HairColour LIKE '%$hair%'";}
            if(!empty($braSize)){$query .= " AND e.tGender_GenderID != 1 AND e.BraSize IS NOT NULL AND e.BraSize REGEXP '$braSize'";}
            if(!empty($dressSize)){$query .= " AND e.tGender_GenderID != 1 AND e.DressSize IS NOT NULL AND e.DressSize >= $dressSize";}
            if(!empty($escortType))
            {
                switch ($escortType)
                {
                    case 'Agency' :
                        $query .= " AND eag.tAgency_AgencyID IS NOT NULL ";
                        break;
                    case 'Independent' :
                        $query .= " AND eag.tAgency_AgencyID IS NULL";
                        break;
                }
            }

            if(!empty($ageFrom) && !empty($ageTo)){
                $i = 0;             
                $query .= " AND (";     
                foreach($ageFrom  as $from)  {  
                    if($i > 0 ){
                        $query .= " OR ";   
                    }                   
                    $query .= " e.DateOfBirth BETWEEN '$ageTo[$i]' AND '$from' ";               
                    $i++;                   
                }
                $query .= " ) ";                
            }

            if(!empty($breasts)){
                $i = 0;             
                $query .= " AND (";     
                foreach($breasts  as $cupSize)   {  
                    if($i > 0 ){
                        $query .= " OR ";   
                    }                   
                    $query .= " e.BraSize IS NOT NULL AND e.BraSize REGEXP '$cupSize' ";                
                    $i++;                   
                }
                $query .= " ) ";                
            }

            if(!empty($incallAreas)){
                $i = 0;             
                $query .= " AND (";     
                foreach($incallAreas  as $incallArea)    {  
                    if($i > 0 ){
                        $query .= " OR ";   
                    }
                    $incallAreaSub = substr($incallArea, 0, 5);             
                    $query .= " e.InCallLocation IS NOT NULL AND (e.InCallLocation LIKE '%$incallArea%' OR e.InCallLocation LIKE '%$incallAreaSub%') ";             
                    $i++;                   
                }
                $query .= " ) ";                
            }

            if(!empty($freeText)){ $query .= " AND (e.EscortName LIKE '%$freeText%' OR a.AgencyName LIKE '%$freeText%')"; }

            switch ($orderBy)
            {
                case 'created' :
                    $query .= " ORDER BY e.DateCreated DESC";
                    break;
                case 'age' :
                    $query .= " ORDER BY e.DateOfBirth DESC";
                    break;
                case 'popularity' :
                    $query .= " ORDER BY e.ViewCount DESC";
                    break;
                default :
                    $query .= " ORDER BY e.DateModified DESC";
                    break;
            }

            $query .= " LIMIT $start, $pageSize";

0 个答案:

没有答案