Make SELECT display a certain value of a column last, keeping sorting by another column

Asked

Viewed 66 times

0

I am doing the following select in a database and displaying the result in a php table:

SELECT ID_PRDC, PRDC_ID_PRDT, PRDC_ID_SEM, DATA_PRDC, LINHA.NM_LN, PRODUTO.NM_PRDT,
        SEMI_ACABADO, NUM_OP, LOTE, QTD, PRDC_NUM_SEQ_LN, SITUACAO.DESC_SIT, SITUACAO.ID_SIT, PRDC_ID_LN, PRDC_ID_SIT
        FROM PRODUCAO
        INNER JOIN produto on (PRODUCAO.PRDC_ID_PRDT = PRODUTO.ID_PRDT)
        INNER JOIN linha on (PRODUCAO.PRDC_ID_LN = LINHA.ID_LN)
        INNER JOIN situacao on (PRODUCAO.PRDC_ID_SIT = SITUACAO.ID_SIT)
        WHERE PRDC_ID_SEM = $ult_sem AND DATE_FORMAT( DATA_PRDC, '%Y' ) = $ano_atual 
        ORDER BY PRDC_ID_LN, PRDC_NUM_SEQ_LN

Today the table is mounted and ordered in PHP by the sequence of line id what is ideal. Each insert in the table production has a status that are related to table situation, what I would like is that when a production is updated to the status concluded or canceled it is displayed at the bottom of the list, keeping the ordering of the rest by line id.

I tried with 2 SELECTS (one sorted by line and one sorted by status) with UNION ALL but it does not recognize an ORDER BY for each select, I tried sorting first by id_status and then by id_line but the result sorts only by id_status and practically ignores the sort by id_line.

Current result example:

 +------------------------------------------------------------+  
 |                              PRODUCAO                      |
 +------------------+--------------+----------+---------------+
 |    COD PRODUTO   |    PRODUTO   |   LINHA  |     STATUS    |
 +------------------------------------------------------------+
 |      1000001     |     prod1    | 1-linha1 |  2-Produzindo |
 |      2000002     |     prod2    | 1-linha1 |  6-Concluido  |
 |      2000007     |     prod7    | 2-linha2 |  7-Cancelado  |
 |      2000003     |     prod3    | 2-linha2 |  1-Aguardando |
 |      2000005     |     prod5    | 3-linha3 |  6-Concluido  |
 |      3000006     |     prod6    | 4-linha4 |  2-Produzindo |
 |      4000004     |     prod4    | 4-linha4 |  1-Aguardando |
 +------------+-----------------------------------------------+

Example of ideal result:

 +------------------------------------------------------------+  
 |                          PRODUCAO                          |
 +------------------+--------------+----------+---------------+
 |    COD PRODUTO   |    PRODUTO   |  LINHA   |    STATUS     |
 +------------------------------------------------------------+
 |      1000001     |     prod1    | 1-linha1 |  2-Produzindo |
 |      2000007     |     prod7    | 2-linha2 |  1-Aguardando |
 |      2000005     |     prod5    | 3-linha3 |  2-Produzindo |
 |      2000004     |     prod4    | 4-linha4 |  1-Aguardando |
 |      2000003     |     prod3    | 2-linha2 |  6-Concluido  |
 |      3000006     |     prod6    | 4-linha4 |  6-Concluido  |
 |      4000002     |     prod2    | 1-linha1 |  7-Cancelado  |
 +------------+-----------------------------------------------+

If anyone can help I appreciate I’ve researched enough but I haven’t found any similar example.

  • Have a look http://stackoverflow.com/a/21211388/6405917 Try in your order by ORDER BY PRDC_ID_LN DESC, PRDC_NUM_SEQ_LN DESC

  • So face this way it displays only by decreasing sequence the line id’s, in vdd I need the column PRDC_ID_LN to be ordered crescently and the other column would be the PRDC_ID_SIT also crescently, but thanks for the attention, I’ll go from the line read on that other theme

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.