[英]How to select and update from one table in one query using object oriented PHP inside class
[英]PHP - how to select from a table one query
我得到了一個包含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;
}
顯示按時間排序的所有消息,是否有一種方法可以從每個ID中僅選擇一個消息查詢?
謝謝,
克勞斯
如果您只想發送一條消息,則可以像這樣使用LIMIT
SELECT * FROM table ORDER BY time LIMIT 1
或者,如果您只想從某個ID獲得一封郵件,則可以使用GROUP BY
SELECT * FROM table ORDER BY time GROUP BY 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;
}
這將僅獲取給定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.