使用 GROUP BY 和 ORDER BY 子句以降序打印记录

Posted

技术标签:

【中文标题】使用 GROUP BY 和 ORDER BY 子句以降序打印记录【英文标题】:Print Record In Descending ORDER using GROUP BY and ORDER BY Clause 【发布时间】:2015-12-03 16:06:41 【问题描述】:

我是 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 上应用降序。

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

【问题讨论】:

【参考方案1】:

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

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

?>

【讨论】:

以上是关于使用 GROUP BY 和 ORDER BY 子句以降序打印记录的主要内容,如果未能解决你的问题,请参考以下文章

使用 GROUP BY 和 ORDER BY 子句以降序打印记录

在 sql 查询中使用 group/order by 和 union 子句

我们如何在具有GROUP BY子句的查询中选择非聚合列,而GROUP BY子句在功能上不依赖于GROUP BY子句中的列?

GROUP BY 和 ORDER BY一起使用时,要注意的问题!

group by / order by 子句有特殊名称吗?

ORDER BY 子句的表达式 #1 不在 GROUP BY 子句中,并且包含非聚合列