mysql根据条件问题选择查询顺序

时间:2013-11-19 08:30:06

标签: mysql select

这是我的选择查询:

select 
    b.S_SORT,
    a.FABRIC1,
    c.BRNCH_CODE,
    a.XS_ALL,
    a.S_ALL,
    a.M_ALL,
    a.L_ALL,
    a.XL_ALL,
    c.LABEL_CODE,
    c.STATUS
from
    jo_det as a
        inner join
    jo_hdr as c ON a.TRAN_NO = c.TRAN_NO
        inner join
    branch as b ON b.BRNCH_CODE = c.BRNCH_CODE
where
    c.JO_NO = '1398'
ORDER BY B.S_SORT ASC

这就是结果:

enter image description here

如您所见,列 S_SORT 的排序工作正常。我想知道的是我如何安排 FABRIC1 的列10-2218-0110将是第一个,40-9515-0402将是每个BRNCH_CODE的最后一个。

S_SORT :1到3,FABRIC1的排列很好,但是在S_SORT:4中,第一个结构是40-9515-0402,最后一个结构是10-2218-0110。 / p>

有没有办法让我可以像S_SORT中的arrang一样订购FABRIC1:1到3?

2 个答案:

答案 0 :(得分:1)

您只需将其添加到ORDER BY子句

select 
    b.S_SORT,
    a.FABRIC1,
    c.BRNCH_CODE,
    a.XS_ALL,
    a.S_ALL,
    a.M_ALL,
    a.L_ALL,
    a.XL_ALL,
    c.LABEL_CODE,
    c.STATUS
from
    jo_det as a
        inner join
    jo_hdr as c ON a.TRAN_NO = c.TRAN_NO
        inner join
    branch as b ON b.BRNCH_CODE = c.BRNCH_CODE
where
    c.JO_NO = '1398'
ORDER BY B.S_SORT ASC, FABRIC1 ASC

答案 1 :(得分:1)

您可以为 FABRIC1 再添加一个订单,如下所述:

select 
    b.S_SORT,
    a.FABRIC1,
    c.BRNCH_CODE,
    a.XS_ALL,
    a.S_ALL,
    a.M_ALL,
    a.L_ALL,
    a.XL_ALL,
    c.LABEL_CODE,
    c.STATUS
from
    jo_det as a
        inner join
    jo_hdr as c ON a.TRAN_NO = c.TRAN_NO
        inner join
    branch as b ON b.BRNCH_CODE = c.BRNCH_CODE
where
    c.JO_NO = '1398'
ORDER BY 
B.S_SORT ASC, 
a.FABRIC1 ASC