在这里我会试一试。我不得不稍微改变一下 sql 表
继承人SQL:
CREATE TABLE IF NOT EXISTS `status` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`user_id` int(3) NOT NULL,
`user_status` text NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `status` (`id`, `user_id`, `user_status`, `date`) VALUES
(1, 1, 'Hi, Im Dave, User One.', '2012-05-03'),
(2, 2, 'Hi, Im Amy, User Two.', '2012-05-01'),
(3, 3, 'Hi, Im Lisa user 3', '2012-05-01'),
(4, 4, 'Hi, Im Joe user 4', '2012-05-02');
CREATE TABLE IF NOT EXISTS `users` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`following` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `users` (`id`, `username`, `following`) VALUES
(1, 'Dave', '2:3'),
(2, 'Amy', '1:4'),
(3, 'Lisa', '1:4'),
(4, 'Joe', '1:2:3');
继承人是php(这假设您已经连接到数据库):
// Get user 2 (id 2) details from user table
$res = mysql_query("SELECT * FROM users WHERE id='2'");
while ($row = mysql_fetch_assoc($res)) {
$username = $row['username'];
$following = $row['following'];
}
if(!empty($following)) {
$data = explode(':',$following);
foreach($data as $user){
// Now get user 2 (id 2) followers statuses
$res = mysql_query("SELECT * FROM status WHERE user_id='$user'");
while ($row = mysql_fetch_assoc($res)) {
echo $user_status = $row['user_status'].'<br>';
echo $date = $row['date'].'<br>';
}
}
}
我测试过,它似乎工作得很好
希望这是你需要的:)