我的数据(sqlfiddle目前已关闭):
CREATE TABLE IF NOT EXISTS `users` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT UNIQUE,
`first_name` VARCHAR(25) NOT NULL,
`last_name` VARCHAR(25) NOT NULL,
`username` VARCHAR(25) NOT NULL UNIQUE,
`email` VARCHAR(60) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`dob` VARCHAR(10) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE = MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
insert into users values
(0, 'Amy','Stew', 'amy0','amy@mail.com','pwd','dob'),
(1, 'John','Marks', 'john1','john@mail.com','pwd','dob'),
(2, 'Wendy','Crow', 'wendy2','wendy@mail.com','pwd','dob'),
(3, 'Steve','Sky', 'steve3','steve@mail.com','pwd','dob'),
(4, 'Sean','Lake', 'sean4','sean@mail.com','pwd','dob');
CREATE TABLE IF NOT EXISTS privatemessages (
`id` INT(11) NOT NULL AUTO_INCREMENT UNIQUE,
`fromuser` INT(11) NOT NULL,
`touser` INT(11) NOT NULL,
`message` TEXT NOT NULL,
`readit` ENUM(NULL,'1') NOT NULL DEFAULT NULL,
`deleted_fromuser` ENUM(NULL,'1') NOT NULL DEFAULT NULL,
`deleted_touser` ENUM(NULL,'1') NOT NULL DEFAULT NULL,
`date_time` DATETIME NOT NULL,
FOREIGN KEY (fromuser) REFERENCES users(user_id),
FOREIGN KEY (touser) REFERENCES users(user_id),
PRIMARY KEY (id)
) ENGINE = MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
insert into privatemessages values
(0,0,3,'0->3 message',NULL,NULL,NULL,now()),
(1,3,0,'3->0 message',NULL,NULL,NULL,now()),
(2,1,2,'1->2 message',NULL,NULL,NULL,now()),
(3,3,1,'3->1 message',NULL,NULL,NULL,now()),
(4,4,3,'4->3 message',NULL,NULL,NULL,now()),
(5,3,4,'3->4 message',NULL,NULL,NULL,now()),
(6,3,4,'3->4 message',NULL,NULL,NULL,now());
CREATE TABLE IF NOT EXISTS `photos` (
`id` INT(11) NOT NULL AUTO_INCREMENT UNIQUE,
`user_id` INT(11) NOT NULL,
`image` VARCHAR(200),
`thumb` VARCHAR(200),
`date` DATETIME,
`profile` ENUM('0', '1') NOT NULL DEFAULT "0",
`approved` ENUM('0', '1') NOT NULL DEFAULT "0",
FOREIGN KEY (user_id) REFERENCES users(user_id),
PRIMARY KEY (`id`)
) ENGINE = MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
insert into photos values
(0,0, 'amy-image.jpg','amy-thumb-image.jpg',now(),1,1),
(1,1, 'john-image.jpg','john-thumb-image.jpg',now(),1,1),
(2,2, 'wendy-image.jpg','wendy-thumb-image.jpg',now(),1,1),
(3,3, 'steve-image.jpg','steve-thumb-image.jpg',now(),1,1),
(4,4, 'sean-image.jpg','sean-thumb-image.jpg',now(),1,1);
PHP :
if (isset($_GET ['messages'])){
// Get the user id and
// Commented out durring testing - $id = $_GET['messages'];
// For testing id is set to 3
$id = 3;
// fetch all the messages from/to user 3 and join them together by each conversation to display as a single conversation button/link
$result = mysqli_query($DBcon, "
SELECT
pm1.id,
pm1.fromuser,
pm1.touser,
pm1.message,
pm1.readit,
UNIX_TIMESTAMP(pm1.date_time),
us.user_id,
us.username,
pt.user_id,
pt.thumb,
pt.profile
FROM
privatemessages pm1
INNER JOIN
users us
ON us.user_id = pm1.touser
INNER JOIN
photos pt
ON pt.user_id = pm1.touser
WHERE
pm1.date_time = (
Select max(pm1.date_time)
FROM
privatemessages pm2
WHERE
pm1.fromuser = pm2.fromuser
AND
pm1.touser = pm2.touser
AND
(touser = '$id' or fromuser = '$id')
)
GROUP BY
pm1.fromuser,
pm1.touser
ORDER BY pm1.id DESC
LIMIT 10
");
// check there are any messages
if (mysqli_num_rows($result) > 0) {
while ($item = mysqli_fetch_assoc($result)) {
$fromuser=$item['fromuser'];
$touser=$item['touser'];
$username=$item['username'];
$thumb=$item['thumb'];
// if user doesn't have profile picture then use "default.jpg" thumbnail
if (is_null($thumb)) {
$thumb = 'default';
$slash='';
$image = '';
} else {
$image=$touser;
$slash="/";
$thumb=$thumb;
}
// display the messages
echo "<div class='row messages'>";
echo "<a class='message-link' data-read='" . $touser . "' href='message.php?from_id=" . $fromuser . "&to_id=" . $touser . "'>";
echo "<div class='col-xs-1 col-sm-1 col-md-1 message-block'>";
echo "<img class='message-photo' src='http://127.0.0.1/socialnetwork/uploads/images/thumbs/" . $image . "" . $slash . "" . $thumb . ".jpg''>";
echo "</div>";
echo "<div class='col-xs-2 col-sm-1 col-md-9 message-block'>";
echo "<span class='message-meta message-title'>" . $username . "</span>";
echo "</div>";
echo "</a>";
echo "<div class='col-xs-7 col-sm-10 col-md-2 message-block'>";
echo "<span class='message-meta float-right'><a href='components/delete-messages.php?delmessageshistory&user_id=" . $fromuser . "&member_id=" . $touser . "' class='btn btn-danger btn-sm float-right'>Delete</a></span>";
echo "</div>";
echo "</div>";
}
}
else {
echo "<div class='alert alert-danger'><span class='glyphicon glyphicon-info-sign'></span> ERROR - NOTHING FOUND $DBcon->error</div>";
}
}
我想加入这些表,可以在fromuser
表的touser
和privatemessages
字段中找到用户的ID,以便我可以将所有已发送的消息和已接收的消息发送到来自某个用户。
我想从其他两个表中提取相应的username
和thumb
字段。
我觉得我现在正在四处走动。
查询返回结果,但用户名完全相同,ID相同,但它们都有不同的照片。帮助
我想要什么
+----------------------------------------------------------------------------------------------------------+
| Id | fromuser | touser | message | readit | deleted_fromuser | deleted_touser | date_time |
+----+----------+--------+----------------+--------+------------------+----------------+-------------------+
| 4 | 4 | 3 | "4->3 message" | NULL | NULL | NULL |2017-02-19 17:20:00|
| 5 | 3 | 4 | "3->4 message" | NULL | NULL | NULL |2017-02-19 18:25:00|
| 6 | 3 | 4 | "3->4 message" | NULL | NULL | NULL |2017-02-19 21:04:41|
+----------------------------------------------------------------------------------------------------------+
| 0 | 0 | 3 | "0->3 message" | NULL | NULL | NULL |2017-02-19 17:20:09|
| 1 | 3 | 0 | "3->0 message" | NULL | NULL | NULL |2017-02-19 17:24:41|
+----------------------------------------------------------------------------------------------------------+
| 3 | 3 | 1 | "3->1 message" | NULL | NULL | NULL |2017-02-19 13:12:00|
用户3和用户之间的对话4 用户3和用户之间的对话0 用户3和用户之间的对话1
<div class="row messages-container">
<div class='row messages'>
<a class='message-link' data-read='3' href='message.php?from_id=4&to_id=3'>
<div class='col-xs-1 col-sm-1 col-md-1 message-block'>";
<img class='message-photo' src='http://127.0.0.1/socialnetwork/uploads/images/thumbs/4/sean-thumb-image.jpg''>
</div>
<div class='col-xs-2 col-sm-1 col-md-9 message-block'>
<span class='message-meta message-title'>steve3</span>
</div>
</a>
<div class='col-xs-7 col-sm-10 col-md-2 message-block'>
<span class='message-meta float-right'>
<a href='components/delete-messages.php?delmessageshistory&user_id=4&member_id=3' class='btn btn-danger btn-sm float-right'>Delete</a>
</span>
</div>
</div>
<div class='row messages'>
<a class='message-link' data-read='3' href='message.php?from_id=0&to_id=3'>
<div class='col-xs-1 col-sm-1 col-md-1 message-block'>";
<img class='message-photo' src='http://127.0.0.1/socialnetwork/uploads/images/thumbs/0/amy-thumb-image.jpg''>
</div>
<div class='col-xs-2 col-sm-1 col-md-9 message-block'>
<span class='message-meta message-title'>amy0</span>
</div>
</a>
<div class='col-xs-7 col-sm-10 col-md-2 message-block'>
<span class='message-meta float-right'>
<a href='components/delete-messages.php?delmessageshistory&user_id=0&member_id=3' class='btn btn-danger btn-sm float-right'>Delete</a>
</span>
</div>
</div>
<div class='row messages'>
<a class='message-link' data-read='3' href='message.php?from_id=3&to_id=1'>
<div class='col-xs-1 col-sm-1 col-md-1 message-block'>";
<img class='message-photo' src='http://127.0.0.1/socialnetwork/uploads/images/thumbs/1/john-thumb-image.jpg''>
</div>
<div class='col-xs-2 col-sm-1 col-md-9 message-block'>
<span class='message-meta message-title'>john1</span>
</div>
</a>
<div class='col-xs-7 col-sm-10 col-md-2 message-block'>
<span class='message-meta float-right'>
<a href='components/delete-messages.php?delmessageshistory&user_id=3&member_id=1' class='btn btn-danger btn-sm float-right'>Delete</a>
</span>
</div>
</div>
</div><!-- END messages-container -->
我得到了什么
<div class="row messages-container">
<div class='row messages'>
<a class='message-link' data-read='3' href='message.php?from_id=4&to_id=3'>
<div class='col-xs-1 col-sm-1 col-md-1 message-block'>";
<img class='message-photo' src='http://127.0.0.1/socialnetwork/uploads/images/thumbs/4/sean-thumb-image.jpg''>
</div>
<div class='col-xs-2 col-sm-1 col-md-9 message-block'>
<span class='message-meta message-title'>steve3</span>
</div>
</a>
<div class='col-xs-7 col-sm-10 col-md-2 message-block'>
<span class='message-meta float-right'>
<a href='components/delete-messages.php?delmessageshistory&user_id=4&member_id=3' class='btn btn-danger btn-sm float-right'>Delete</a>
</span>
</div>
</div>
<div class='row messages'>
<a class='message-link' data-read='3' href='message.php?from_id=0&to_id=3'>
<div class='col-xs-1 col-sm-1 col-md-1 message-block'>";
<img class='message-photo' src='http://127.0.0.1/socialnetwork/uploads/images/thumbs/0/amy-thumb-image.jpg''>
</div>
<div class='col-xs-2 col-sm-1 col-md-9 message-block'>
<span class='message-meta message-title'>amy0</span>
</div>
</a>
<div class='col-xs-7 col-sm-10 col-md-2 message-block'>
<span class='message-meta float-right'>
<a href='components/delete-messages.php?delmessageshistory&user_id=0&member_id=3' class='btn btn-danger btn-sm float-right'>Delete</a>
</span>
</div>
</div>
<div class='row messages'>
<a class='message-link' data-read='3' href='message.php?from_id=3&to_id=1'>
<div class='col-xs-1 col-sm-1 col-md-1 message-block'>";
<img class='message-photo' src='http://127.0.0.1/socialnetwork/uploads/images/thumbs/3/steve-thumb-image.jpg''>
</div>
<div class='col-xs-2 col-sm-1 col-md-9 message-block'>
<span class='message-meta message-title'>steve3</span>
</div>
</a>
<div class='col-xs-7 col-sm-10 col-md-2 message-block'>
<span class='message-meta float-right'>
<a href='components/delete-messages.php?delmessageshistory&user_id=3&member_id=1' class='btn btn-danger btn-sm float-right'>Delete</a>
</span>
</div>
</div>
</div><!-- END messages-container -->
第三条消息是从steve3到john1的外发消息,我打印的是steve3的用户名而不是收件人的用户名,在这种情况下应该是john1。 我想返回结果,其中steve3已向用户发送消息或已被其他用户发送消息但在steve3发送消息的情况下我不希望消息链接读取用户名“steve3”,其中显示steve的缩略图,我想要其他用户的用户名和图片。