Skip to content
Advertisement

Count of line item in MySQL

I have a complex query joined to multiple tables to show a “transaction history”, I want a quick and easy way to count the occurrences of order_id to make it as the line_number. I had it done with code but for performance reason I want to have it done with MySQL. Here is an example of what I have:

This is what I want it to look like:

I tried COUNT(order_id) but that didn’t work, unless there is a good way to do it. Thanks!

MySQL version 5.5.43

Advertisement

Answer

In MySQL 8.0, just use row_number():

In earlier versions, one solution (other than using variables) is to do an inline count with a subquery:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement