【问题标题】:Query is sorting properly but is not showing all results properly查询正在正确排序,但未正确显示所有结果
【发布时间】:2011-10-27 13:39:39
【问题描述】:

下面的这个查询有效,但我似乎有一个奇怪的错误。 users 表中的第一个用户可以看到所有其他用户与他们关联的所有帮助问题。但是数据库中的第二个用户只能通过他们在表中的第一个人获得帮助问题,并且查询会忽略该表中其他人为第二个人提供的结果。

我猜它与括号有关?

代码:

//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");
}

}

【问题讨论】:

    标签: php sorting filter


    【解决方案1】:
    where agent_names.system_id = '$myid' and 
    motorcycle_help.system_id='$myid' and added_by <> '$myid' 
    and help_box.status = '$variis'
    

    只有在全部为true 时才返回数据。如果第二个人在所有三个字段中都没有匹配的 id,它将不会返回您的期望。如果$variis 为空,则创建一个应替换调用变量的默认ID。可能需要 or 声明。如果你打算使用回声棒,打印做同样的事情。

    【讨论】:

    • 它确实有效。我从每个查询中删除了 where agent_names.system_id='$myid' 。选择你的答案,因为你是唯一一个至少试图回答它的人。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多