Answer the question
In order to leave comments, you need to log in
How to sort by ORDER BY using also rowid?
Please help sorting.
when requested:
nume = cursor.execute("SELECT cash, rowid FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[1]
nume = cursor.execute("SELECT cash, rowid FROM users WHERE id = {} ORDER BY cash".format(ctx.author.id)).fetchone()[1]
Answer the question
In order to leave comments, you need to log in
But I would like to somehow sort it by cash and already get the rowid .
SELECT id, name, cash, row_number() over(order by cash desc) as rownum FROM users
cursor.execute("SELECT cash, rowid, rownum FROM (SELECT id, name, cash, row_number() over(order by cash desc) as rownum FROM users) WHERE id = ?", (ctx.author.id, )).fetchone()[2]
If you need a rowid for some reason, you are most likely doing something wrong. What do you need it for?
In relational databases, the position in the table (not in the selection, but in the table) does not mean anything. You do not have to manage it and there is nothing to get this position for. I'm sure there are other ways to achieve what you're trying to achieve there. For example, if you need to find out how many users have a cache less than yours, then ask like this:
SELECT count(*)
FROM users u
WHERE u.cash < (SELECT cash FROM users WHERE id = :my_user_id)
SELECT u.id, u.cash
FROM users u
WHERE u.cash <= (SELECT cash FROM users WHERE id = :my_user_id)
ORDER BY u.cash
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question