English 中文(简体)
How to store a list of users, and let the users know they are on that list
原标题:

Im building a site where a user can add people to their "list". This list belongs to the user, but im wondering whats the best way to store/retrieve/loop through this list in a function, to show the people who are on the list, that they are there and who created it.

So once the list of usernames is created, i would assume store it in a mysql table field, then write a function that would search that table to find any lists that affect the currently logged in user.

My question is two fold. First, assuming the list is just a-z 0-9 and hyphens and underscores (no spaces in the usernames, but separated with spaces), what would be the best field type to store this as in mysql. Secondly, what would be the best/fastest method for searching for a username across many lists?

最佳回答

Varchar, indexing and additionally caching.

You re creating a usertable with a username and userid field. Then create a table user_relation where you combine userid s so you have matches. Place indexes on all foreign key relations. That should give a pretty decent performanceboost :)

edit some additional info for setting it up

Table: User UserID - Int (8) - Primary Key, Auto increment Name - Varcahr (30) EmailAddress - Varchar (90) - Unique

Table: UserRelation ID - Int (8) - Primary Key, Auto increment UserID_Source - Int (8), Index (this is the user) UserID_Friend - Int (8), Index (this is a friend / relation from the user)

This way you can easily store relations to a certain user.

To get some results use a query like:

SELECT `User`.*, `User`.`UserID` AS `UserID_Source`
FROM   `User`
WHERE  `User`.`UserID` = `user_relation`.`user_id_source`

You can extend the query by a JOIN to direct fetch the relation matches or use another query for that ;)

问题回答

You best option would be to store each name in its own row.

If your list contains references to other members (each person has their own list) your best bet is to create the data in an MySQL table of some kind (as MySQL is optimized for this kind of thing), for example you would need 2 simple tables, table one (user table) would have 2 columns. ID (INT or BIGINT), UserName (VARCHAR).

Then your second table (user links) would have 2 entries depending on if linking is done bidirectionally or unidirectionally see bellow.

Bidirectionally: The table would have 2 columns, User1(INT or BIGINT), User2(INT or BIGINT). To retrieve a list, simply query this table for all entries where the user id is in either User1 or User2.

Unidirectionally: The table would have 2 columns, Owner(INT or BIGINT), Person(INT or BIGINT). To retrieve a list for a user simply query this for all entries that are owned by such user.

The difference between the two is, with bidirectionally if i add you to my list, i am automatically on your list. With Unidirectionally that is removed, so you have to add me to your list for me to appear on your list, even if your on my list.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签