Finding shared list IDs in a MySQL table using bitwise operands

Posted by landons on Stack Overflow See other posts from Stack Overflow or by landons
Published on 2010-06-07T16:27:30Z Indexed on 2010/06/07 16:42 UTC
Read the original article Hit count: 226

Filed under:
|

I want to find items in common from the "following_list" column in a table of users:

+----+--------------------+-------------------------------------+
| id | name               | following_list                      |
+----+--------------------+-------------------------------------+
|  9 | User 1             | 26,6,12,10,21,24,19,16              | 
| 10 | User 2             | 21,24                               | 
| 12 | User 3             | 9,20,21,26,30                       | 
| 16 | User 4             | 6,52,9,10                           | 
| 19 | User 5             | 9,10,6,24                           | 
| 21 | User 6             | 9,10,6,12                           | 
| 24 | User 7             | 9,10,6                              | 
| 46 | User 8             | 45                                  | 
| 52 | User 9             | 10,12,16,21,19,20,18,17,23,25,24,22 | 
+----+--------------------+-------------------------------------+

I was hoping to be able to sort by the number of matches for a given user id. For example, I want to match all users except #9 against #9 to see which of the IDs in the "following_list" column they have in common.

I found a way of doing this through the "SET" datatype and some bit trickery:
http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html#bits

However, I need to do this on an arbitrary list of IDs. I was hoping this could be done entirely through the database, but this is a little out of my league. Any bit gurus out there?

Thanks, Landon

© Stack Overflow or respective owner

Related posts about mysql

Related posts about bit-manipulation