Limit number of records and sort them

1

I have two tables one of clients and one of sales where I must select the 30 LATEST records added to the sales table, show the information of the clients that made those transactions and order them upwards with respect to the id of sales . the fields of both tables are:

customers

id , nombre, apellido_paterno,apellido_materno, rut, dv

sales

id, cliente_id, fecha

I have the following query that returns the information of the clients that have been registered in the sales table, but I need only to select the last 30 sales made and order them by their id.

 select nombre,apellido_paterno,apellido_materno,rut,dv,venta.id,venta.fecha
 from cliente inner join venta on cliente.id = venta.cliente_id 
    
asked by Pedro Montenegro 18.07.2018 в 01:32
source

1 answer

1

You can add the following to the end of your SQL query

SELECT nombre,apellido_paterno,apellido_materno,rut,dv,
       venta.id,venta.fecha
FROM cliente INNER JOIN venta ON cliente.id = venta.cliente_id 
ORDER BY id DESC LIMIT 30;

Where:

  
  • ORDER BY indicates that we will sort the dataset of results by column id , along with DESC
  •   
  • LIMIT 30 indicates that together with DESC of the previous condition only limit to take the 30 records that are in that range
  •   

    You put the name of the id column that is appropriate for the sales table

        
    answered by 18.07.2018 / 01:46
    source