多个LEFT JOINS和ORDER BY

时间:2013-03-08 08:58:43

标签: mysql left-join joomla1.5

我的文件中有以下查询:

function getItems($ShowFeatured,$OrderBy,$Order,$amountShow,$ShowPropertyID)

{        
switch($OrderBy) {
case 0 :
$OrderBy = 'p.id';
break;
case 1 :
$OrderBy = 'p.name';
break;
case 2 :
$OrderBy = 'p.price';
break;
case 3 :
$OrderBy = 'p.hits';
break;
case 4 :
$OrderBy = 'p.refresh_time';
break;
}
switch($Order) {
case 0 :
$Order = 'ASC';
break;
case 1 :
$Order = 'DESC';
break;
}   
if($ShowPropertyID){$where= 'WHERE p.id IN ('.$ShowPropertyID.')';}else{$where=        'WHERE p.published = 1 ';}
    if($ShowFeatured){$sqlFeatured= ' AND p.featured = 1';}
    $db = &JFactory::getDBO();     
    $query = 'SELECT p.*, i.name as imagename,c.id as key1,c.name as name_category,t.id as key5,t.name as name_type,cy.id as key2,cy.name as name_country,s.id as key3,s.name as name_state,l.id as key4,l.name as name_locality,'
            . ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
    . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
    . ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
    . ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'      
    . ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, ' 
    . ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
            . ' FROM #__properties_products AS p '
            . ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '               
            . ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
            . ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
            . ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
            . ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
            . ' LEFT JOIN #__properties_images AS i ON  p.id = i.parent '
            . $where                
            . $sqlFeatured
            . ' GROUP BY p.id ' 
            . ' ORDER BY '.$OrderBy.' '.$Order
            . ' LIMIT '.$amountShow;

    $db->setQuery($query);  
    $items = $db->loadObjectList();



   // $items = ($items = $db->loadObjectList())?$items:array(); 
//echo str_replace('#_','jos',$query);


    return $items;

获取图片的部分如下:

. ' LEFT JOIN #__properties_images AS i ON  p.id = i.parent '

此查询通过i.parent命令图像 - 我得到的图像我不喜欢。在我的数据库中,我有行i.ordering - 是否可以通过i.ordering在上面的查询中订购图像?

感谢。

1 个答案:

答案 0 :(得分:0)

对你来说这样的问题我认为会做你想做的事。

$query = 'SELECT p.*, i.name as imagename,c.id as key1,c.name as name_category,t.id as key5,t.name as name_type,cy.id as key2,cy.name as name_country,s.id as key3,s.name as name_state,l.id as key4,l.name as name_locality,'
            . ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
    . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
    . ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
    . ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'      
    . ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, ' 
    . ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
            . ' FROM #__properties_products AS p '
            . ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '               
            . ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
            . ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
            . ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
            . ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
            . ' LEFT JOIN (SELECT parent, MAX(ordering) AS MostImportant FROM #__properties_images GROUP BY parent) x ON  p.id = x.parent '
        . ' LEFT JOIN #__properties_images AS i ON  x.parent = i.parent AND x.MostImportant = i.ordering '
            . $where                
            . $sqlFeatured
            . ' GROUP BY p.id ' 
            . ' ORDER BY '.$OrderBy.' '.$Order
            . ' LIMIT '.$amountShow;

您可能希望使用MIN而不是MAX,具体取决于排序的工作方式