查询未正确排序

发布于 2024-12-11 19:39:22 字数 2448 浏览 0 评论 0原文

我正在使用这个查询。我需要按 $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");
}

}

好的更新:我选择了 status 字段并显示了它。对于返回的所有这些行,状态为不需要帮助,而 $variis 实际上是需要帮助

I have this query I am using. I need to sort it by $variis, but it seems the sort is not working. I double checked field names and tables. All is correct but it won't sort it by the $variis:

Code:

//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 an update: I selected the status field and displayed it. For all of these rows that are being returned teh the status is no help needed when the $variis is in fact need help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

維他命╮ 2024-12-18 19:39:22

请阅读手册: 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;

Please read the manual: http://dev.mysql.com/doc/refman/5.1/en/union.html

To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. The following example uses both clauses:

(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;
叹梦 2024-12-18 19:39:22

试试这个:

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 子句中使用 *and help_box.status = '$variis'* ,而是在连接中使用它

try this:

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' 

instead of using the *and help_box.status = '$variis'* in the where clause, use it in the join

饮惑 2024-12-18 19:39:22

我相信“排序”是指“过滤”。
每次在 WHERE 子句中需要 help_box.status 时,都应该在 FROM 子句中包含 help_box 表。只有这样它才能运行。

I believe by "sort", you mean "filter".
You should include help_box table in your FROM clause every time you need help_box.status in your WHERE clause. Only then it can run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文