Actually there is but not where you'd intuitively think of looking for it. Have a look at SORT
- this should do what you need:
SORT myset BY nosort STORE mylist
Plan A: First get 1 row per user:
SELECT user_id,
GROUP_CONCAT(priv ORDER BY priv) AS privileges
FROM tbl
GROUP BY user_id
Then look for dups
SELECT GROUP_CONCAT(user_id) AS users,
privileges
FROM ( the select above ) x
GROUP BY privileges
HAVING COUNT(*) > 1;
Plan B: This involves changing the data structure. By using the datatyps SET
instead of multiple rows, you can have
CREATE TABLE foo(
user_id ...,
privs ENUM('read', 'write', ...) NOT NULL
)
Then do
SELECT GROUP_CONCAT(user_id) AS users
privs
FROM foo
GROUP BY privs
HAVING COUNT(*) > 1;
If this fails to map to something in the original question, maybe I have given you some things to think about. Or maybe "normalization" of the original table(s) would help make it look more like my example.
Notes: GROUP_CONCAT
has, by default, a limit of 1024 characters; this can be increased. There may be a limit on how long a string can be for use in GROUP BY privileges
.
Best Answer
Redis doesn't come out of the box with the ability to search/index inside hashes, but it gives you all the tools you need to accomplish your task. Your challenge could be solved, for example, by keeping a key in the database called last_user_registered or similar, and SETting it every time that a user registers to that user's ID. When you want to fetch the last user that had registered at any given moment - just GET it, get it?
But you (and Redis!) can do much better: for example, you could use a Redis list and LPUSH/RPOP members to keep scores with the users who last joined. Or, you could use a sorted set where each member is a user id and the score is the timestamp... ah, the possibilities are endless and you're only limited by your imagination (and time, resources, etc...) ;)
I think this is exactly why Redis 'aficionados' lovingly refer to it as a database building tool rather than as just a database - you can actually make it do whatever you want and if what you want isn't in Redis already, there's always Lua that you can use to extend with or even developing your own fork and putting in a pull request to the github.