查询的前半部分有效,但尾端没有

时间:2011-10-26 01:52:36

标签: php sorting

我之前发过这个帖子,但无法清楚地解释这样重新发布。

我正在使用此查询。我需要通过$ variis对它进行排序,但似乎排序不起作用。我仔细检查了字段名称和表格。查询有效,但不会按$ variis对其进行排序。我也试过使用help_box.status周围的括号,但它不起作用。

代码:

//Unseen
$variis = "Need Help";
$myid = <the user id>;

$sql = "select car_help.car_id, agent_names.agent_name, help_box.status, 
car_help.why_car, car_help.date_time_added, car_help.just_date, 
car_help.type, agent_names.agent_id
from car_help LEFT JOIN agent_names on car_help.agent_whois = agent_names.agent_id 
LEFT JOIN help_box on car_help.somefield= help_box.somefield
where agent_names.system_id = '$myid' and car_help.system_id='$myid' 
and added_by <> '$myid' and help_box.status = '$variis'
UNION
select magazine_help.note_id, agent_names.agent_name, help_box.status, 
magazine_help.note_name, magazine_help.date_time_added, 
magazine_help.just_date, magazine_help.type, agent_names.agent_id
from magazine_help LEFT JOIN agent_names on 
magazine_help.agent_id = agent_names.agent_id 
LEFT JOIN help_box on car_help.somefield= help_box.somefield 
where agent_names.system_id='$myid' and 
magazine_help.system_id = '$myid' and added_by <> '$myid' 
and help_box.status = '$variis'
UNION
select motorcycle_help.rand_id, agent_names.agent_name, 
help_box.status, motorcycle_help.rand_name, motorcycle_help.date_time_added,     
motorcycle_help.just_date, motorcycle_help.type, agent_names.agent_id
from motorcycle_help LEFT JOIN agent_names ON 
motorcycle_help.by_who = agent_names.agent_id
LEFT JOIN help_box on car_help.somefield= help_box.somefield
where agent_names.system_id = '$myid' and 
motorcycle_help.system_id='$myid' and added_by <> '$myid' 
and help_box.status = '$variis'
UNION
select mobile_questions.bal_test_id, agent_names.agent_name, 
help_box.status, mobile_questions.bal_why, mobile_questions.date_time_added,   
mobile_questions.just_date, mobile_questions.type, agent_names.agent_id
from mobile_questions LEFT JOIN agent_names ON 
mobile_questions.agent_who_ordered = agent_names.agent_id
LEFT JOIN help_box on car_help.somefield= help_box.somefield
where agent_names.system_id = '$myid' and 
mobile_questions.system_id='$myid' and added_by <> '$myid' 
and help_box.status = '$variis'
ORDER BY date_time_added DESC LIMIT $startrow, 20";

$result = mysql_query($sql);

$query = mysql_query($sql) or die ("Error: ".mysql_error());


if ($result == "")
{
echo "";
}
echo "";


$rows = mysql_num_rows($result);

if($rows == 0)
{
print("");

}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{

$row1 = $row['row_name'];


print("$row1");
}

}

1 个答案:

答案 0 :(得分:1)

如果要对结果集进行排序,则需要在第一个选择之前和最后一个选择结束时放置括号,然后在结束的parantheses之后放置order by语句。

但是我不太明白你的问题。您希望按$ variis排序,但是您正在选择该值。

更新:

如果您没有获得预期的行,那么选择可能过于严格。尝试使用LIKE而不是'=',甚至使用通配符。