我有一个正在使用的查询。我需要按$variis对其进行排序,但这种排序似乎不起作用。我仔细检查了字段名称和表。所有内容都是正确的,但不会按$variis排序
代码:
//Unseen
$variis = "Need Help";
$myid = This is the user's 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
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
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
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
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");
}
}Ok a update:我选择了status字段并显示了它。对于要返回的所有这些行,当$variis实际上为need help时,状态为no help needed
发布于 2011-10-25 04:38:33
请阅读手册:http://dev.mysql.com/doc/refman/5.1/en/union.html
若要使用ORDER BY或LIMIT子句对整个UNION结果进行排序或限制,请将各个SELECT语句括起来,并将ORDER BY或limit置于最后一个语句之后。下面的示例使用这两个子句:
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;发布于 2011-10-25 05:08:02
试试这个:
from magazine_help LEFT JOIN agent_names on
magazine_help.agent_id = agent_names.agent_id **and help_box.status = '$variis'**
where agent_names.system_id='$myid' and
magazine_help.system_id = '$myid' and added_by <> '$myid' 不要在where子句中使用*和help_box.status = '$variis'*,而是在连接中使用它
发布于 2011-10-25 04:40:00
我相信你说的“排序”是指“过滤器”。每次在WHERE子句中需要help_box.status时,都应该在FROM子句中包含help_box表。只有这样它才能运行。
https://stackoverflow.com/questions/7881587
复制相似问题