Choose the order in which items are listed in Mysql

Asked

Viewed 44 times

0

I have a table with three types of order, ordem1, ordem2 and ordem_especial . How can I list items in that order:

EXAMPLE:

ordem2
ordem_especial
ordem1

I tried unsuccessfully something like :

REAL SELECT OF THE APPLICATION

EDIT: SOLVED WITH THE CODE BELOW

$sql = "SELECT * 
FROM  `IMAGEMCONTROLE`
INNER JOIN  `PRODUTOS_PROPOSTA_LOCAL` ON  `PRODUTOS_PROPOSTA_LOCAL`.`PRODUTO_PROPOSTA` =  `IMAGEMCONTROLE`.`IMAGEMCONTROLE_PRODUTO`
INNER JOIN  `IMAGEMCONTROLESITE` ON  `PRODUTOS_PROPOSTA_LOCAL`.`PRODUTO_PROPOSTA` =  `IMAGEMCONTROLESITE`.`IMAGEMCONTROLE_PRODUTO`
WHERE `IMAGEMCONTROLE`.`IMAGEMCONTROLE_LINHA` > 0 
ORDER BY 
       (
            CASE 
                WHEN   `PRODUTOS_PROPOSTA_LOCAL`.`ESPECIAL` ='1' THEN 0 
                WHEN   `PRODUTOS_PROPOSTA_LOCAL`.`ESPECIAL` ='2' THEN 1
                WHEN   `PRODUTOS_PROPOSTA_LOCAL`.`ESPECIAL` ='3' THEN 2 
                WHEN   `PRODUTOS_PROPOSTA_LOCAL`.`ESPECIAL` ='4' THEN 3 
                WHEN   `PRODUTOS_PROPOSTA_LOCAL`.`ESPECIAL` ='5' THEN 4 
                WHEN   `PRODUTOS_PROPOSTA_LOCAL`.`ORDEM_LANCAMENTO` THEN 5 
                WHEN   `PRODUTOS_PROPOSTA_LOCAL`.`ORDEM_PROPOSTA` THEN 6
            END
        ) , 
        `PRODUTOS_PROPOSTA_LOCAL`.`ORDEM_PROPOSTA`,
        `PRODUTOS_PROPOSTA_LOCAL`.`ORDEM_LANCAMENTO`
";

I need to list IMAGEMCONTROLE_MARCA in the order above (First mark 19, then mark 23 and so on), then ORDEM_LANCAMENTO and then ORDEM_PROPOSTA.

RESOLVED

2 answers

1


Understanding that:

ordem2
ordem_especial
ordem1

be the content of a given field, you can do something like:

SELECT ..., (CASE WHEN campo='ordem2' THEN 1 
                  WHEN campo='ordem_especial' THEN 2 
                  WHEN campo='ordem1' THEN 3 
                  ELSE 0
              END) AS n
...
ORDER BY n;

You can also put the CASE expression directly in the ORDER BY clause.

====================

I didn’t understand not only your CASE clause as these ASC interspersed in the clause, they seem to me syntactically invalid.

Are you sure that INNER JOIN between the tables is what you really want?

Define better what you want to do because, I believe, I misunderstood your problem.

  • I couldn’t implement CASE if I could help edit the question with real code.

  • I got this solved thanks. I edited the code with the solution.

-1

According to the link would be just that:

SELECT column1, column2,...
FROM tbl
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...

Are you making a mistake? You can post more information to help solve your problem?

  • No error, just reverses the listing and if I put DESC hits the order of the list but the listed items get downhill and need ascending, I will test this notice more background !

  • It doesn’t work, this is just an example indicating that you can use ASC or DESC only.

Browser other questions tagged

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