由mysql复杂的顺序

时间:2016-04-28 09:46:54

标签: mysql sql-order-by

如果我有这样的表

+----+----------------+
| id | id_alternativo |
+----+----------------+
| 15 |        18      |      
+----+----------------+
| 16 |        0       |      
+----+----------------+
| 17 |        0       |      
+----+----------------+
| 18 |        0       |
+----+----------------+

如何在id 15之后命令记录显示id 18?

6 个答案:

答案 0 :(得分:3)

查看MySQL文档,您可以在排序中使用多个列,并在每列中使用DESC / ASC。

http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html

如果我理解正确您的意思,您的查询将如下所示:

SELECT id, id_alternativo FROM table ORDER BY id_alternativo DESC, id DESC;

+------+----------------+
| id   | id_alternativo |
+------+----------------+
|   15 |             18 |
|   18 |              0 |
|   17 |              0 |
|   16 |              0 |
+------+----------------+

答案 1 :(得分:1)

一种解决方案是使用这样的自联接:

select t.*
from
  yourtable t left join yourtable o
  on t.id = o.id_alternativo
order by
  coalesce(o.id, t.id), t.id

这将把所有替代id放在主id之后(在这种情况下18将跟随15)。

请看小提琴here。请注意,除非替代id具有另一个替代ID,否则这将起作用(例如,如果18本身具有另一个替代ID),但这不能完全用MySQL解决,因为它还不支持递归查询。

答案 2 :(得分:0)

根据我的理解,只需使用order by id (18, 15)以及您需要的任何其他ID。

答案 3 :(得分:0)

data-is

答案 4 :(得分:0)

您可以使用以下订单:

<div class="content">
    <div class="content">
        <div class="wrapper">
            <div class="component">
                <section class="block1">
                    <div class="container">
                        <header class="flex-between-center">
                            <div class="left">
                                <p><span class="icon icon-map"></span>1620 Pebblewood Ln. Ste. 132
                                    <br> Naperville, IL 60563
                                </p>
                                <a class="tel" ><span class="icon icon-phone"></span><b>(630)741-4445</b></a>
                                <div class="mob-tel">
                                    <ul>
                                        <li><a href="tel:(630)741-4445">(630) 741-4445</a></li>
                                        <li><a href="tel:(630)741-4445">(630) 741-4445</a></li>
                                        <li><a href="tel:(630)741-4445">(630) 741-4445</a></li>
                                        <li><a href="tel:(630)741-4445">(630) 741-4445</a></li>
                                    </ul>
                                </div>
                            </div>
                            <div class="logo">
                                <a href="/">
                                    <img src="<?=CONFIG::TEMPLATE_ROOT?>design/img/logo.jpg" alt="">
                                </a>
                            </div>
                            <div class="right">
                                <form action="/search">
                                <span class="input input--hoshi search-wrapp">
                                    <input class="input__field input__field--hoshi" type="text" id="search" name="keyword" placeholder="Search..."/>
                                    <label class="input__label input__label--hoshi input__label--hoshi-color-2" for="input-5">
        <!--                                <span class="input__label-content input__label-content-hoshi">Search...</span>-->
                                    </label>
                                        <span class="icon-serch"></span>

                                </span>
                                <input id="search-btn" type="submit" style="display: none;"/>
                                </form>
                                <script type="text/javascript">
                                    $('.search-wrapp .icon-serch').click(function(){
                                        $('#search-btn').trigger('click');
                                    })

                                </script>
                                <p class="in-cart">
                                    <span class="icon"></span>
                                    <span class="title">In cart:</span>
                                    <a href="/card" id="cart-header">
                                        <b class="Bouquet">Bouquet</b><span class="nubmer"> <?=$_SESSION['sum_count']?$_SESSION['sum_count']:0 ?></span>
                                        <b class="price-title">Price</b>
                                        <span class="price">$<?=$_SESSION['sum_count']?$_SESSION['sum_price']:0 ?></span>
                                    </a>
                                </p>
                            </div>
                        </header>
                    </div>
                    <hr>
                    <div class="container">
                        <nav>
                            <div class="menu-icon">
                                <span></span>
                                <span></span>
                                <span></span>
                            </div>
                            <ul class="flex-between-center menu">
                                <? $menu = $this->_db->select('catalog ', ' category = "main" AND active = 1 ORDER BY sort'); ?>
                                <? foreach ($menu as $item) {
                                if($item['id'] != 229){?>
                                        <li><a href="/catalog/<?= $item['link'] ?>" class="<?if($url->id == $item['link'])echo "active-menu";?>" ><?= $item['title_en'] ?></a></li>
                                <? }}?>
                            </ul>
                        </nav>
                    </div>
                </section>
                <!-- end block1 -->

<强>样品

SELECT id 
FROM mytab
ORDER BY IF(id=18,155,id*10);

答案 5 :(得分:0)

抱歉,我使用了一个简单的例子,但我的表格更复杂。 id_alternativo可以是递归的(id 18可能有id_alternativo = 19)等等,而id_alternativo不能是表格中最高的id,因此ORDER BY id_alternativo DESC, id DESC不起作用。这是我桌子上的查询:

SELECT 
    a.id, a.compatibile, a.id_alternativo
FROM
    ordini_righe AS a
WHERE
    intestazione IN (398010) AND a.canc = 0
        AND a.stato_ordine = 0

这是结果

+-------+-------------+----------------+
|  id   | compatibile | id_alternativo |
+-------+-------------+----------------+
|828924 |      0      |     828931     |
+-------+-------------+----------------+
|828925 |    828932   |        0       |
+-------+-------------+----------------+
|828926 |      0      |        0       |
+-------+-------------+----------------+
|828927 |      0      |        0       |
+-------+-------------+----------------+
|828931 |      0      |     828933     |
+-------+-------------+----------------+
|828932 |    828932   |        0       |
+-------+-------------+----------------+
|828933 |      0      |        0       |
+-------+-------------+----------------+

我必须订购兼容的desc,然后通过id_alternativo和id之间的关系来订购其他记录。所以我解决了使用像这样的新列

SELECT 
    a.id, a.compatibile, a.id_alternativo, IF(id_alternativo = 0, a.id, id_alternativo) ordine
FROM
    ordini_righe AS a
        JOIN
    locazioni AS b ON a.locazione = b.id
        JOIN
    stati_righe AS c ON a.stato_ordine = c.id
WHERE
    intestazione IN (398010) AND a.canc = 0
        AND a.stato_ordine = 0
ORDER BY compatibile DESC, ordine, a.id ASC

我得到了理想的结果

+-------+-------------+----------------+--------+
|  id   | compatibile | id_alternativo | ordine |
+-------+-------------+----------------+--------+
|828925 |    828932   |     828931     | 828925 |
+-------+-------------+----------------+--------+
|828932 |    828932   |        0       | 828932 |
+-------+-------------+----------------+--------+
|828926 |      0      |        0       | 828926 |
+-------+-------------+----------------+--------+
|828927 |      0      |        0       | 828927 |
+-------+-------------+----------------+--------+
|828924 |      0      |     828931     | 828931 |
+-------+-------------+----------------+--------+
|828931 |      0      |     828933     | 828933 |
+-------+-------------+----------------+--------+
|828933 |      0      |        0       | 828933 |
+-------+-------------+----------------+--------+