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

标签 php mysql join subquery sql-order-by

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

enter image description here

之后我有一个表og_ratings,其中我有针对那个id's的记录

enter image description here

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

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

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

我的代码输出是 enter image description here

这里它只对我的变量 $id$opinion 起作用,它对 $action 不起作用。我想对 $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/

相关文章:

php - 在我的 PHP 页面中,提交按钮未将商品添加到购物车

php - 这个基于 PHP 的 SQL 语句的正确语法是什么?

php - PHP使用Redis存储Session需要多少个socket?

sql - 如何在jsp Restful Web服务中进行连接查询?

php - 在单个查询中查询 2 个表

php - 将 Evercookie 值传递出函数

php - Laravel/MYSQLcreated_at和updated_at正在应用不同的时区

php - 使用 git 版本控制进行 PHP 开发

php - 在 mySQL 数据库和 Android 应用程序之间建立连接

mysql - 多重连接 Mysql 将 SUM 值加倍