我是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的多个结果:

php - 使用GROUP BY和ORDER BY子句打印降序的记录-LMLPHP

之后,我有一个表og_ratings在其中记录了该id's

php - 使用GROUP BY和ORDER BY子句打印降序的记录-LMLPHP

og_ratings表中,列client_id用作opinion_id的外键

我的代码运行良好,但是我在降序中遇到问题

当我运行此代码时,我的降序子句仅适用于id而不适用​​于数据

我的代码输出是
php - 使用GROUP BY和ORDER BY子句打印降序的记录-LMLPHP

在这里,它仅对我的变量$id有效,对$opinion不起作用。我想对$action应用降序。

希望你能理解我的问题。请帮我。

最佳答案

是。我通过以下代码做到了

<?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
}
?>

关于php - 使用GROUP BY和ORDER BY子句打印降序的记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32449881/

10-10 18:52