Numerical sequence

Asked

Viewed 632 times

0

I have a database (Mysql) with about 100 registered clients and am creating a table (ranking) of purchases of these customers, I know that the bank creates an ID autoincrement, would like to put in this table only the first 20 customers who buy more products and then would have the number of placement in the ranking and the name, but I’m not getting it.

1 answer

2

Probably solving in the database is the best way to what you asked.

In the part you query:

SELECT cliente FROM clientes ORDER BY totalcompras DESC LIMIT 20

If purchases are on another basis (which is probably the case), you can use / JOIN grouping functions or even subquery.

To number the lines in the output, you can use part of this solution:

Rank database [#1 of 10]

  • Hello Bacco, Valew by tip. I wanted to put to each customer selected from the bank the number from 1 to 20 in front. Ex. 1 - Joäo (Being that the ID in the BD is 33), 2 - Maria (Being that ID in the BD is 55) and so on until reaching the 20.

  • 1

    As Bacco himself recommended in another answer, you can include a counter in the query: SELECT @linha := @linha + 1 AS contador, cliente FROM clientes ORDER BY totalcompras DESC LIMIT 20.

  • @Jorgetoledo came to see the answer that the bfavaretto indicated? It is more or less what you need?

  • And oh guys, Bacco, I already did and did: $query_clients = mysql_query ( "select * from clients " ); while ( $client = mysql_fetch_array ( $query_clients ) ) { $id_client = $client['id']; $name_client = $client['name']; if ($query_clients){ $sequence++; And it all worked out, thanks a lot for the help..

Browser other questions tagged

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