0

我有下一个代码,但使用 ORDER BY 没有得到想要的结果:

// OrderBy WHERE Conditions
if ($sort_by == "0")    $sortby_condition = "ORDER BY mv_user_ranking.ranking_date DESC ";
if ($sort_by == "1")    $sortby_condition = "ORDER BY mv_user_info.age DESC ";
if ($sort_by == "2")    $sortby_condition = "ORDER BY mv_user_info.sex DESC ";

$query ="SELECT * FROM mv_user_info 
         LEFT JOIN mv_user_lang_interested ON mv_user_lang_interested.uid = mv_user_info.uid        
         LEFT JOIN mv_user_disponibility   ON mv_user_disponibility.uid   = mv_user_info.uid    
         LEFT JOIN mv_user_ranking         ON mv_user_ranking.uid         = mv_user_info.uid   

         WHERE country ='$country' AND city = '$city' AND
               mv_user_lang_interested.english = '1' AND
               mv_user_lang_interested.english_level = '2' AND 
               mv_user_info.uid != '$uid'"

         .$sortby_condition.

         "LIMIT 0, 50";

echo $query;

$result = mysql_query($query) or die(mysql_error());

我做错了什么?

4

2 回答 2

1

这看起来很有趣,好吧让我们试试

// OrderBy WHERE Conditions
//i don't know what was passed as $sort_by value
//perhaps you may try 
$sort_by = (integer)$sort_by;  //casting 

if ($sort_by == 0){$sortby_condition = "ORDER BY mv_user_ranking.ranking_date DESC ";}//if () if() three times not an optimize way, try if, elseif
elseif ($sort_by == 1){    $sortby_condition = "ORDER BY mv_user_info.age DESC ";}//write your code in block "{}" this may prevent your block of statement mix
elseif ($sort_by == "2"){    $sortby_condition = "ORDER BY mv_user_info.sex DESC ";}

$query ="SELECT * FROM mv_user_info 
         LEFT JOIN mv_user_lang_interested ON mv_user_lang_interested.uid = mv_user_info.uid        
         LEFT JOIN mv_user_disponibility   ON mv_user_disponibility.uid   = mv_user_info.uid    
         LEFT JOIN mv_user_ranking         ON mv_user_ranking.uid         = mv_user_info.uid   

         WHERE country ='$country' AND city = '$city' AND
               mv_user_lang_interested.english = '1' AND
               mv_user_lang_interested.english_level = '2' AND 
               mv_user_info.uid != '$uid'"

    .$sortby_condition.

    "LIMIT 0, 50";

echo $query;

$result = mysql_query($query) or die(mysql_error());

如果有任何问题,请随时提出更好的问题,您可以正确地给出整行文本,那么我们可以更好地回答您:)

于 2013-09-29T14:35:58.420 回答
0

从技术上讲,我没有看到查询有任何问题,但您可以做一件事:在此查询上创建另一个查询,以便此查询成为子查询,然后从上述查询中排序。您的代码将如下所示:

// OrderBy WHERE Conditions
if ($sort_by == "0")    $sortby_condition = "ORDER BY ranking_date DESC ";
if ($sort_by == "1")    $sortby_condition = "ORDER BY age DESC ";
if ($sort_by == "2")    $sortby_condition = "ORDER BY sex DESC ";

//Note I have removed the table name from order by clause because in the super-query we will only have column name.

$query ="
Select * From (
 SELECT * FROM mv_user_info 
     LEFT JOIN mv_user_lang_interested ON mv_user_lang_interested.uid = mv_user_info.uid        
     LEFT JOIN mv_user_disponibility   ON mv_user_disponibility.uid   = mv_user_info.uid    
     LEFT JOIN mv_user_ranking         ON mv_user_ranking.uid         = mv_user_info.uid   

     WHERE country ='$country' AND city = '$city' AND
           mv_user_lang_interested.english = '1' AND
           mv_user_lang_interested.english_level = '2' AND 
           mv_user_info.uid != '$uid'
     LIMIT 0, 50) as a ".$sortby_condition;

echo $query;

$result = mysql_query($query) or die(mysql_error());

我希望这有帮助

于 2013-09-29T14:35:55.633 回答