0

ファイルに次のクエリがあります。

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 によって画像を注文することは可能ですか?

ありがとう。

4

1 に答える 1

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;

順序がどのように機能するかによって、MAX の代わりに MIN を使用したい場合があります。

于 2013-03-08T09:32:01.497 に答える