我一直在努力建立一个供用户登录和发布消息的小型站点。我正在设置一个用于存储联系人的表(以便用户可以在板上查看和向彼此发送消息)。我几乎完成了配置,但遇到了障碍。
When a user registers for the first time, an entry is created in each of two tables; users and contacts. The users table stores username and password stuff, the contacts table stores a directory of people on the system and whether they've been added to other people's contact lists. The tables look like this;
mysql> select userid, firstname, lastname, username from users;
+--------+-----------+----------+----------+
| userid | firstname | lastname | username |
+--------+-----------+----------+----------+
| 63 | Chris | Smith | csmith |
| 64 | Susan | Smith | ssmith |
| 65 | Roger | Smith | rsmith |
| 66 | Diane | Smith | dsmith |
+--------+-----------+----------+----------+
4 rows in set (0.00 sec)
mysql> select * from contacts;
+----------+---------+-----------+
| username | contact | confirmed |
+----------+---------+-----------+
| csmith | csmith | 0 |
| ssmith | ssmith | 0 |
| rsmith | rsmith | 0 |
| dsmith | dsmith | 0 |
| csmith | dsmith | 2 |
| dsmith | csmith | 2 |
| dsmith | ssmith | 1 |
| dsmith | rsmith | 1 |
+----------+---------+-----------+
8 rows in set (0.00 sec)
用户能够进行身份验证,他们可以毫无问题地相互添加和删除。“已确认”列存储联系人的状态。0用于指示注册时为用户建立的联系人中的初始条目。1表示邀请是否已从用户名发送给联系人,但尚未确认。2表示已确认联系。通过在站点的不同部分进行选择查询,我可以显示用户的联系人列表,或者仅显示他们可以添加但尚未添加的系统上的人员列表。除了不请自来的联系人的目录列表以外,几乎所有内容都可以正常工作。
联系人的初始列表应由状态为0的用户的选择查询输出组成,应省略用户自己的条目(因此,不应该添加自己的名字),以及(令人烦恼的部分)我),它也应省略使用自己的用户名确认为1(已发送邀请)或2(已确认联系)的任何人。
因此,csmith应该在其用户目录ssmith和rsmith中看到ssmith和rsmith,但不应看到dsmith,因为dsmith已被添加到他的联系人中(在联系人中第5个条目中确认为2)。
因此,在考虑到这些规则的情况下,我一直在想办法简化对目录查找的查询。我该如何创建一个能做到的规则?
select contact from contacts where username!=$authenticated_user and confirmation='0'
这将为我提供系统上除我自己之外尚未被邀请的人之外的用户列表。
但随后也退出了
select contact from contacts where username=$authenticated_user and confirmation='1' and confirmation='2'
从输出中删除,以便不再显示已邀请和/或已添加用户的列表?
任何帮助表示赞赏。
SELECT DISTINCT `contact`
FROM test.test
WHERE `contact`
NOT IN
(SELECT `contact`
FROM test.test
WHERE `username` = "csmith");
相互友谊的任何条目均以(a-> b)和(b-> a)格式给出,所有邀请均以(a-> b)格式给出,从而简化了测试-我们只需要寻找能够联系人条目中没有$ username(例如“ csmith”)作为用户名。
(这也排除了默认情况下通过(a,a,0)连接到他自己的用户。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句