2012-01-05 5 views
1

私の結果はSQL文で "ORDER BY prod_name"でソートする必要がありますが、動作させることはできません。私はORDER BYをMYSQLクエリに挿入するには

$thisProduct .= " AND prod_type = 1 ORDER BY prod_name"; 

後も

$thisProduct .= " AND ID = '" . mysql_real_escape_string($_GET['product']) . "' ORDER BY prod_name"; 

後にしようとしたしかし、私は私の結果が正しくソートすることができません。間違った場所に注文したのですか、DBを間違ってクエリしましたか?

進んでいただきありがとうございます。MYSQLのクエリではまだかなり新しいです。

$thisProduct = "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['product']) AND is_numeric($_GET['product'])) { 
$thisProduct .= " AND ID = '" . mysql_real_escape_string($_GET['product']) . "'"; 
} 
else { 
    $thisProduct .= " AND prod_type = 1"; 
} 
$thisProduct .= " LIMIT 1"; 

$getThisProduct = mysql_query($thisProduct); 
if (!$getThisProduct/* OR mysql_num_rows($getThisProduct) == 0 */) { 
    header("HTTP/1.0 404 Not Found"); 
    require APP_PATH . '/404.html'; 
    die(); 
} 

$thisProductData = mysql_fetch_assoc($getThisProduct); 

答えて

0

クエリが正しいと仮定すると、あなたが名前で最初の製品が欲しい:

$thisProduct .= " ORDER BY prod_name LIMIT 1"; 
1

あなたが持っている必要があります。

$ thisProductを= "PROD_NAME BY ORDER";。
$ thisProduct。= "LIMIT 1";

(LIMIT 1は1レコードしか取得できないことに注意してください)。

0

私はのように、それは右のあなたの "LIMIT 1" の前に行くべきと考えている:

$thisProduct .= " ORDER BY prod_name LIMIT 1"; 
0

で選択構文SELECTクエリは通常以下の形式をとります。

SELECT which_all_to_select 
FROM which_table/tables 
WHERE criteria 
ORDER BY column_name ASC/DESC; 

ASC昇順、及びDESCこの注文がORDER BY句で指定column_nameによって結果を問い合わせるため

下降されます。

関連する問題