我是php新手。我有一个问题,我无法按降序排列。
这是我的代码
<?php
include ("connection.php");
$q_opinion="SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id IN (SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50)
Group By r.client_id
ORDER BY r.client_id DESC
";
$result = mysql_query($q_opinion) or die;
$rating = array();
while($row = mysql_fetch_assoc($result))
{
$id[] = $row['client_id'];
$action[] = $row['atitle'];
$opinion[] = $row['opinion'];
}
for ($i=0; $i<count($rating); $i++)
{
if ($rating[$i] == "")continue;
?>
<table border="1">
<tr>
<td><?= $id[$i] ?> </td>
<td><?= $opinion[$i] ?> </td>
<td><?= $action[$i] ?> </td>
</tr>
</table>
<?php
}
?>
现在我解释一下我的代码和我的问题
我有多个表,我使用左连接加入了它们。 首先,我将解释我的子查询。此查询包含多个id:
的结果之后我有一张表og_ratings
,其中我记录了id's
在og_ratings
表中,client_id
列用作opinion_id
我的代码运作良好但我的降序有问题
当我运行此代码时,我的降序子句仅适用于id
而不是数据
此处仅适用于我的变量$id
和$opinion
,它不适用于$action
。我想在$action
上应用降序。
我希望你能理解我的问题。请帮帮我。
答案 0 :(得分:0)
是。我按照代码
做到了<?php
include ("connection.php");
$q_opinion="SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r
inner join
(
select max(notification_date) notification_date,
client_id
from og_ratings
group by client_id
ORDER BY notification_date DESC
) r2
on r.notification_date = r2.notification_date
and r.client_id = r2.client_id
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id IN (SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50)";
$result = mysql_query($q_opinion) or die;
$rating = array();
while($row = mysql_fetch_assoc($result))
{
$rating[] = $row['client_id'];
$action[] = $row['atitle'];
$opinion[] = $row['opinion'];
$date[] = $row['notification_date'];
$lrating[] = $row['ltitle'];
$srating[] = $row['stitle'];
}
for ($i=0; $i<count($rating); $i++) {
if ($rating[$i] == "")continue;
?>
<table border="1">
<tr>
<td><?= $rating[$i] ?> </td>
<td><?= $date[$i] ?> </td>
<td><?= $opinion[$i] ?> </td>
<td><?= $action[$i] ?> </td>
<td><?= $lrating[$i] ?> </td>
<td><?= $srating[$i] ?> </td>
</tr>
</table>
<?php
}
?>