Query with two sorts in descending order

Asked

Viewed 309 times

-1

There is the possibility of executing a query searching by contract number, ordering by descending date and their respective service orders?
If so, can you give me an example for knowledge??

Query used:

SELECT * FROM field WHERE contrato LIKE '%$buscar%' ORDER BY Data,Num_OS DESC
  • Supposedly as you have would already do that. Where’s the problem?

  • I think you put data desc, num_os would solve... see this example in SQL Fiddle

  • Gentlemen, settled... Thanks,.

  • what solution? it would be good [besides helping] to help others

  • SELECT * FROM field WHERE contract LIKE '%$buscar%' ORDER BY Data,Num_os DESC or SELECT * FROM field WHERE contract LIKE '%$buscar%' ORDER BY Data DESC, Num_os

  • 1

    Put an answer with the solution, avoid putting it in comment only.

  • how do I do that??

Show 2 more comments

1 answer

-1


Yes, it is possible. You just need to put the fields you want to sort in the sort command mysql: order by:

SELECT * --campos que deseja retornar
FROM nome_tabela
WHERE num_contrato = @num_contrato --@num_contrato será o contrato a ser pesquisado
ORDER BY data, ordem_servico desc --desc é palavra reservada e indica que a ordenação é decrescente

It’s important you take a look at link I put and study a little about ordering. There is a lot of good material on the net, worth researching!!

  • forgive the ignorance dear, how would you make this result be displayed in different lines? that is in one line the contract number and a date and a service order, in the other line the same contract and another date and another service order??? can explain me better??

  • the way it is I believe it is already returned this way. you must have on a line something like contract 123, date 01/10/2018, the 1 and the other contract 123, date 01/10/2018, the 2. So, it will already bring the result as expected. Do the test ;)

  • this is the query: "SELECT * FROM field WHERE contract LIKE '%$search%' ORDER BY Data,Num_os DESC"

  • and what’s the problem?

  • really according to what you wrote I did but it does not show me, displays only a date and its respective order of service and on the other lines does not display the other dates and your service orders.

  • how do I upload a table image??? I believe that will help you better understand.. below the table structure: Login, Date, Service Order, Type of Os, Agenda, Contract

  • If it only displays one value, then your contract number is unique. Anyway, click edit (in question) and add more information, table structure type, return error, data (if applicable)

  • how do I upload a table image??? I believe that will help you better understand.. follows below the structure of the table: Login, Date, Service Order, Type of Os, Agenda, Contract , when researching it tells me it has 5 records of this contract.

Show 3 more comments

Browser other questions tagged

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