我已经创建了消息系统,在messages.php上我希望明智地显示消息,最后的消息应该显示但是不知道查询,因为我是php的新手这里是数据库信息
table:conversation_chat table2:chat conversation_id: primary unique id: primary unique from: conversation_id: to: from_user: time: to_user: message: subject:
如果有人可以帮助我
<?php
$req3 = mysql_query("SELECT * FROM `conversation_chat` WHERE
`user_to`='$userid1'");
$hy= mysql_fetch_array($req3);
$convo = $hy['conversation_id'];
if(mysql_num_rows($req3)!=0){
$req4 = mysql_query("SELECT * FROM `chat` WHERE `to`='$userid1' AND
`conversation_id`='$convo'");
while($dn1 = mysql_fetch_array($req4))
{
?>
<td><img alt="example image" src="avatar.png"> <a
href="read?id=<?php echo $dn1['id']; ?>"><?php echo
htmlentities($dn1['from'], ENT_QUOTES, 'UTF-8'); ?></a></td>
<td><span class="label vd_bg-green append-icon">
<?php
echo $dn1['subject']; ?></span></td>
<td style="width:80px" class="text-right">
<strong><?php echo timeAgo($dn1['time']); ?></strong></td><td
style="width:80px" class="text-right">
<?php echo '<a
href="mdelete.php?id='. $dn1['id'] .'">Delete</a>'; ?></td>
</tr>
<?php
}}else{ echo "No new messages";}
?>
答案 0 :(得分:1)
好的,您刚刚编辑了数据库信息。 所以现在我明白了你的数据库是怎样的。
你必须添加&#34;时间&#34;在chat2&#34;
中键入DATETIME的列SELECT *
FROM `chat`
WHERE `to`='$userid1'
AND
`conversation_id`='$convo'
ORDER BY time DESC //Change the order from last to older
如果您只想要最后一条消息,并且如果它是一个MySQL数据库,则可以添加
LIMIT 1
答案 1 :(得分:0)
如果我正确理解您的问题,您希望只显示一条消息。即最新的?
如果确实如此,请从
更改您的查询SELECT * FROM `chat` ...
要
SELECT *, max(date) FROM `chat` ...
选择
答案 2 :(得分:-1)
Select *
From MyTable
Where [Plan] In (
Select [Plan]
From MyTable
Where Attributes = 'IsActive' And Value = 'True'
)