【问题标题】:Sort rows by a column, then sort the sorted rows by another column按列对行进行排序,然后按另一列对已排序的行进行排序
【发布时间】:2015-06-11 05:51:23
【问题描述】:

我有一个包含 3 列的表格:

name | cat | time
a      1      0
b      2      1
c      3      2
d      1      3
e      2      4

我需要输出如下内容:

name | cat | time
d      1      3
a      1      0
e      2      4
b      2      1
c      3      2

我需要按cat 列升序对行进行排序,然后按time 降序对排序后的行进行排序。

我尝试使用ORBER BY cat ASC, time DESC,但结果只是按cat排序。

我该如何解决这个问题?

____ 更新_____

这是我给出的 PHP 查询

$query = "SELECT fr_promo.id as promo_id,
                 fr_promo.name as promo_name,
                 fr_promo.description as promo_desc,
                 fr_promo.description2 as promo_desc2,
                 fr_promo.price as promo_price,
                 fr_promo.discount as promo_discount,
                 fr_promo.image as promo_image,
                 fr_promo.cat as promo_cat,
                 fr_shop.id as shop_id,
                 fr_shop.latitude as shop_latitude,
                 fr_shop.longitude as shop_longitude,
                 fr_shop.name as shop_name,
                 fr_shop.description as shop_description,
                 fr_shop.phone as shop_phone,
                 fr_shop.image as shop_image,
                 fr_cat.color as cat_color,
                 fr_shop.home as shop_home FROM fr_promo, fr_shop, fr_cat WHERE";

$query .= " fr_promo.status = ".$PROMO_STATUS['active']; // Only Active Promotion

// Relations Start
// $query .= " AND fr_promo.cat = $cat"; // Only relative to a cat
// Relations End

$query .= " AND fr_shop.status = ".$SHOP_STATUS['active']; // Shop Active
$query .= " AND fr_promo.id_shop = fr_shop.id"; // Relation
$query .= " AND fr_promo.cat = fr_cat.id"; // Relation
$query .= " ORDER BY fr_promo.cat ASC, fr_promo.date DESC LIMIT 50"; // Order

____ 更新 HTML 结果 ______

                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="3" data-timestamp="1433884559" href="http://www.goubaoapp.com/p/6" target="_blank" ><img style="width: 72px; background: #009900;" src="http://www.goubaoapp.com/upload/promo/icon/2-1430993987-243820-.webp.jpg" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="4" data-timestamp="1433885898" href="http://www.goubaoapp.com/p/35" target="_blank" ><img style="width: 72px; background: #009900;" src="http://www.goubaoapp.com/upload/promo/icon/2-1433509658-828387-image.jpg" /></a>



                <a data-cat="4" data-timestamp="1433885627" href="http://www.goubaoapp.com/p/45" target="_blank" ><img style="width: 72px; background: #009900;" src="http://www.goubaoapp.com/upload/promo/icon/2-1433880871-507885-image.jpg" /></a>



                <a data-cat="4" data-timestamp="1433880393" href="http://www.goubaoapp.com/p/44" target="_blank" ><img style="width: 72px; background: #009900;" src="http://www.goubaoapp.com/upload/promo/icon/2-1433880393-71521-image.jpg" /></a>



                <a data-cat="4" data-timestamp="1433510267" href="http://www.goubaoapp.com/p/36" target="_blank" ><img style="width: 72px; background: #009900;" src="http://www.goubaoapp.com/upload/promo/icon/2-1433510267-565361-image.jpg" /></a>



                <a data-cat="4" data-timestamp="1430998573" href="http://www.goubaoapp.com/p/7" target="_blank" ><img style="width: 72px; background: #009900;" src="http://www.goubaoapp.com/upload/promo/icon/2-1430998573-535602-niuzai.webp.jpg" /></a>



                <a data-cat="4" data-timestamp="1430412118" href="http://www.goubaoapp.com/p/3" target="_blank" ><img style="width: 72px; background: #009900;" src="http://www.goubaoapp.com/upload/promo/icon/2-1430412118-874494-tb1bv1ygfxxxxxgapxxxxxxxxxx_!!0-item_pic.jpg" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="7" data-timestamp="1431019740" href="http://www.goubaoapp.com/p/8" target="_blank" ><img style="width: 72px; background: #926239;" src="http://www.goubaoapp.com/upload/promo/icon/2-1430998729-101842-tb1xryihpxxxxxyxxxxxxxxxxxx_!!0-item_pic.jpg_400x400.jpg_.webp.jpg" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="8" data-timestamp="1433362165" href="http://www.goubaoapp.com/p/30" target="_blank" ><img style="width: 72px; background: #926239;" src="http://www.goubaoapp.com/upload/promo/icon/189-1433362164-996579-dd.png" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="11" data-timestamp="1433965209" href="http://www.goubaoapp.com/p/40" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/193-1433965209-755481-anm.jpg" /></a>



                <a data-cat="11" data-timestamp="1433965129" href="http://www.goubaoapp.com/p/41" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/193-1433965129-675286-2-120i11p056.jpg" /></a>



                <a data-cat="11" data-timestamp="1433925696" href="http://www.goubaoapp.com/p/46" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/196-1433925696-165442-.jpg" /></a>



                <a data-cat="11" data-timestamp="1433869431" href="http://www.goubaoapp.com/p/43" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/1981433773456667791imagejpg" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="12" data-timestamp="1433886056" href="http://www.goubaoapp.com/p/11" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/4-1431676405-817577-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1433418988" href="http://www.goubaoapp.com/p/32" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/5-1433418987-133297-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431776307" href="http://www.goubaoapp.com/p/19" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/4-1431776307-129447-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431776225" href="http://www.goubaoapp.com/p/18" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/5-1431776225-248962-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431776118" href="http://www.goubaoapp.com/p/17" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/6-1431776118-174199-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431775678" href="http://www.goubaoapp.com/p/16" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/6-1431775678-59578-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431775545" href="http://www.goubaoapp.com/p/15" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/4-1431775545-383464-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431775374" href="http://www.goubaoapp.com/p/14" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/5-1431775374-927887-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431677121" href="http://www.goubaoapp.com/p/13" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/6-1431677121-21520-image.jpg" /></a>



                <a data-cat="12" data-timestamp="1431676948" href="http://www.goubaoapp.com/p/12" target="_blank" ><img style="width: 72px; background: #376bc7;" src="http://www.goubaoapp.com/upload/promo/icon/5-1431676948-971379-image.jpg" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="19" data-timestamp="1433962164" href="http://www.goubaoapp.com/p/38" target="_blank" ><img style="width: 72px; background: #e38503;" src="http://www.goubaoapp.com/upload/promo/icon/2-1433511139-897757-image.jpg" /></a>



                <a data-cat="19" data-timestamp="1433962134" href="http://www.goubaoapp.com/p/37" target="_blank" ><img style="width: 72px; background: #e38503;" src="http://www.goubaoapp.com/upload/promo/icon/2-1433511057-806543-image.jpg" /></a>



                <a data-cat="19" data-timestamp="1433962094" href="http://www.goubaoapp.com/p/9" target="_blank" ><img style="width: 72px; background: #e38503;" src="http://www.goubaoapp.com/upload/promo/icon/2-1431031328-244270-2013-02-0111.21.24.jpg" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="23" data-timestamp="1433594897" href="http://www.goubaoapp.com/p/39" target="_blank" ><img style="width: 72px; background: #bf40ba;" src="http://www.goubaoapp.com/upload/promo/icon/191-1433581684-870981-.jpg" /></a>



                    <!-- <div style="clear: both;"></div> -->


                <a data-cat="24" data-timestamp="1433764970" href="http://www.goubaoapp.com/p/42" target="_blank" ><img style="width: 72px; background: #bf40ba;" src="http://www.goubaoapp.com/upload/promo/icon/1971433764792561100imagejpg" /></a>

【问题讨论】:

  • ORBER BY cat ASC, time DESC 应该解决它。这是您的实际数据吗?
  • 时间列是什么类型的?
  • 如果都是小于 10 的整数,那么数据类型是什么无关紧要;即使它是一个角色,它仍然会被正确排序...... OP 没有告诉我们@Tony。该问题无法从给出的示例中重现,因此需要一个更好的示例,以便任何人都能够最终提供帮助。
  • "time" 列是时间戳 linux "cat" 列是整数
  • @Ben 好吧,我将在这里进行疯狂的猜测,并建议我们没有被告知的内容实际上是一个带有数字的字符串。这就是我问的原因。

标签: mysql sql sorting


【解决方案1】:

答案是我将子类别组与主要类别组混淆了。 很抱歉浪费了您的时间,每个想法都是正确的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-28
    • 2014-03-12
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多