按列对行进行排序,然后按其他列对已排序的行进行排序

时间:2015-06-10 21:13:54

标签: mysql sql sorting

我有一个包含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>

1 个答案:

答案 0 :(得分:0)

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