I already have a query which results like this
+-----------+------------+---------------+--------+
| DetailsNo | SaleDate | Country | Value |
+-----------+------------+---------------+--------+
| 277 | 2012-02-10 | PHI | 42 |
| 279 | 2012-02-10 | PHI | 10 |
| 280 | 2012-02-10 | USA | 30 |
| 281 | 2012-02-10 | USA | 25 |
| 282 | 2012-02-10 | FRA | 65 |
| 283 | 2012-02-10 | FRA | 36 |
| 284 | 2012-02-10 | GER | 47 |
+-----------+------------+---------------+--------+
What i need is to add another column and display a sort of order number like the following :
+-----------+------------+---------------+--------+--------+
| DetailsNo | SaleDate | Country | Value | Order |
+-----------+------------+---------------+--------+--------+
| 277 | 2012-02-10 | PHI | 42 | 1 |
| 279 | 2012-02-10 | PHI | 10 | 2 |
| 280 | 2012-02-10 | USA | 30 | 1 |
| 281 | 2012-02-10 | USA | 25 | 2 |
| 282 | 2012-02-10 | FRA | 65 | 1 |
| 283 | 2012-02-10 | FRA | 36 | 2 |
| 284 | 2012-02-10 | GER | 47 | 1 |
+-----------+------------+---------------+--------+--------+
Is this possible?
You can use variables for this:
SELECT DetailsNo, SaleDate, Country, Value,
@rn := IF(@c = Country, @rn + 1,
IF(@c := Country, 1, 1)) AS 'Order'
FROM mytable
CROSS JOIN (SELECT @rn := 0, @c := '') AS vars
ORDER BY Country, DetailsNo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.