Let's say we have this request
SELECT * FROM table
And this result from that.
id | user_id
------------
1 | 1
------------
2 | 1
------------
3 | 2
------------
4 | 1
How can I get a counter on how often user_id appears as another field (without any serious SQL query)
id | user_id | count
--------------------
1 | 1 | 3
--------------------
2 | 1 | 3
--------------------
3 | 2 | 1
--------------------
4 | 1 | 3
We have this value currently in the code, but we are implementing sorting by this table, and I would like to be able to sort in the SQL query.
By the way, if this is not possible without any serious trick, we just skip sorting by this field.
source
share