我在哪里放置ORDER BY?

时间:2012-01-09 04:24:33

标签: php mysql

我无法弄清楚下面的ORDER BY语句的位置......非常感谢任何帮助。

// Load all products
$ProdPackQ = "SELECT prod_name AS Name, days_span, CONCAT(LEFT(prodID,2),ID) AS ID,     geo_targeting FROM products WHERE status = 'Active' AND vendID = ".$resort['vendID'];
if(isset($_GET['dz']))
$ProdPackQ .= " AND days_span = ".$_GET['dz'];
/*else
$ProdPackQ .= " AND days_span = 1"; */
$ProdPackQ .= " AND prod_type = ".$thisprodtype;
if(count($packarr) != 0)

{ $ProdPackQ .= " UNION SELECT pack_name AS Name, days_span, CONCAT(LEFT(packID,2),ID), NULL as geo_targeting FROM packages WHERE ID IN(".$mypacks.")";
    if(isset($_GET['dz']))
    $ProdPackQ .= " AND days_span = ".$_GET['dz'];
/*else
    $ProdPackQ .= " AND days_span = 1"; */
$ProdPackQ .= " ORDER BY prod_name, days_span, ID";
}

2 个答案:

答案 0 :(得分:0)

规则缩略图在查询的最后部分按顺序排列。所以把它放在你的if语句块之外。

答案 1 :(得分:0)

ORDER BY位于正确的位置,但您需要选择此类

// Load all products
$ProdPackQ = "SELECT prod_name AS Name, days_span, CONCAT(LEFT(prodID,2),ID) AS ID,                 
geo_targeting FROM products WHERE status = 'Active' AND vendID = ".$resort['vendID'];
if(isset($_GET['dz']))
$ProdPackQ .= " AND days_span = ".$_GET['dz'];
/*else
$ProdPackQ .= " AND days_span = 1"; */
$ProdPackQ .= " AND prod_type = ".$thisprodtype;
if(count($packarr) != 0)

{ $ProdPackQ .= " UNION SELECT pack_name AS Name, days_span, CONCAT(LEFT(packID,2),ID) AS ID,      
 NULL as geo_targeting FROM packages WHERE ID IN(".$mypacks.")";
    if(isset($_GET['dz']))
     $ProdPackQ .= " AND days_span = ".$_GET['dz'];
/*else
$ProdPackQ .= " AND days_span = 1"; */
$ProdPackQ .= " ORDER BY Name, days_span, ID";
}