我不确定这或多或少是你的意思 - 匆忙拼凑起来。
用于构建示例的数据库架构:
create table if not exists `friends` (
`id` int(10) unsigned not null auto_increment,
`user_id` int(10) unsigned not null,
`friend_id` int(10) unsigned not null,
`accepted` tinyint(3) unsigned not null default '0',
primary key (`id`),
key `user_id` (`user_id`),
key `friend_id` (`friend_id`)
) engine=innodb auto_increment=3 default charset=utf8;
insert into `friends` (`id`, `user_id`, `friend_id`, `accepted`) values
(1, 1, 2, 1),
(2, 1, 3, 0);
create table if not exists `messages` (
`id` int(10) unsigned not null auto_increment,
`message` varchar(50) not null default '0',
`time` timestamp not null default current_timestamp,
`sender_id` int(11) not null,
`recipient_id` int(11) not null,
primary key (`id`),
key `sender_id` (`sender_id`),
key `recipient_id` (`recipient_id`)
) engine=innodb auto_increment=3 default charset=utf8;
insert into `messages` (`id`, `message`, `time`, `sender_id`, `recipient_id`) values
(1, 'hi fuddwhack', '2018-02-20 11:32:41', 1, 2),
(2, 'hello yersel tattyheid', '2018-02-20 11:32:57', 2, 1),
(3, 'yellow banana', '2018-02-20 11:45:32', 3, 1),
(4, 'green apple', '2018-02-20 11:45:43', 1, 3),
(5, 'orange shoes', '2018-02-20 10:46:12', 2, 1),
(6, 'red pineapple', '2018-02-20 11:46:27', 2, 3);
create table if not exists `users` (
`uid` int(10) unsigned not null auto_increment,
`name` varchar(50) not null default '0',
primary key (`uid`)
) engine=innodb auto_increment=4 default charset=utf8;
insert into `users` (`uid`, `name`) values
(1, 'daphne'),
(2, 'velma'),
(3, 'wilma');
显示表格和数据的基本查询
mysql> describe friends;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| friend_id | int(10) unsigned | NO | MUL | NULL | |
| accepted | tinyint(3) unsigned | NO | | 0 | |
+-----------+---------------------+------+-----+---------+----------------+
mysql> describe messages;
+--------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+-------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| message | varchar(50) | NO | | 0 | |
| time | timestamp | NO | | CURRENT_TIMESTAMP | |
| sender_id | int(11) | NO | MUL | NULL | |
| recipient_id | int(11) | NO | MUL | NULL | |
+--------------+------------------+------+-----+-------------------+----------------+
mysql> describe users;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| uid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | 0 | |
+-------+------------------+------+-----+---------+----------------+
mysql> select * from messages;
+----+------------------------+---------------------+-----------+--------------+
| id | message | time | sender_id | recipient_id |
+----+------------------------+---------------------+-----------+--------------+
| 1 | Hi Fuddwhack | 2018-02-20 11:32:41 | 1 | 2 |
| 2 | hello yersel tattyheid | 2018-02-20 11:32:57 | 2 | 1 |
| 3 | Yellow banana | 2018-02-20 11:45:32 | 3 | 1 |
| 4 | green apple | 2018-02-20 11:45:43 | 1 | 3 |
| 5 | orange shoes | 2018-02-20 10:46:12 | 2 | 1 |
| 6 | red pineapple | 2018-02-20 11:46:27 | 2 | 3 |
+----+------------------------+---------------------+-----------+--------------+
mysql> select * from friends;
+----+---------+-----------+----------+
| id | user_id | friend_id | accepted |
+----+---------+-----------+----------+
| 1 | 1 | 2 | 1 |
| 2 | 1 | 3 | 0 |
+----+---------+-----------+----------+
查询以查找从用户到收件人的消息,反之亦然
select * from messages m
where
m.sender_id=( select f.user_id from friends f where f.friend_id=m.recipient_id and f.accepted=1 )
or
m.recipient_id=( select f.user_id from friends f where f.friend_id=m.sender_id and f.accepted=1 )
order by m.time;
+----+------------------------+---------------------+-----------+--------------+
| id | message | time | sender_id | recipient_id |
+----+------------------------+---------------------+-----------+--------------+
| 5 | orange shoes | 2018-02-20 10:46:12 | 2 | 1 |
| 1 | Hi Fuddwhack | 2018-02-20 11:32:41 | 1 | 2 |
| 2 | hello yersel tattyheid | 2018-02-20 11:32:57 | 2 | 1 |
+----+------------------------+---------------------+-----------+--------------+