[英]PHP - how to select from a table one query
I got a table that contains ID, Names, message, and time
, I want to select from the table only one message query from each ID, Currently I select all the messages, this is my code 我得到了一个包含
ID, Names, message, and time
的表,我想从该表中仅选择每个ID中的一条消息查询,目前我选择了所有消息,这是我的代码
$query= mysql_query("SELECT * FROM `table` ORDER BY `time`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr['message'];
echo '</br>';
echo $msg;
}
That Shows all messages ordered by time, Is there is a way to select only one message query from each ID? 显示按时间排序的所有消息,是否有一种方法可以从每个ID中仅选择一个消息查询?
Thanks, 谢谢,
Klaus 克劳斯
If you want only one message you can use LIMIT
like this 如果您只想发送一条消息,则可以像这样使用
LIMIT
SELECT * FROM table ORDER BY time LIMIT 1
Or if you want only one message from some id then you can use GROUP BY
或者,如果您只想从某个ID获得一封邮件,则可以使用
GROUP BY
SELECT * FROM table ORDER BY time GROUP BY id
Sure, pretty straightforward 当然,很简单
This will fetch all messages given ID: 这将获取给定ID的所有消息:
$id = 10 //Get your id in any way you need
$query= mysql_query("SELECT `message` FROM `table` WHERE `ID` = $id")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr['message'];
echo $msg;
}
and this will fetch only the first message given ID 这将仅获取给定ID的第一条消息
$id = 10
$query= mysql_query("SELECT `message` FROM `table` WHERE `ID` = $id LIMIT 1")or die(mysql_error());
$row = mysql_fetch_array($query));
if($row){
$msg = $row['message'];
echo $msg;
}else{
echo 'no messages for id '.$id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.