0

嗨,我正在尝试从同一个表 A 中获取不同的值和其他一些数据。我试过的代码是

$query2="select DISTINCT(from_id) from messages where to_id='$userid' order by messagedate DESC";
$res2=mysql_query($query2);
while($row2=mysql_fetch_row($res2))
{
$query="select * from messages where to_id='$userid' and from_id='$row2[0]' ORDER BY messagedate DESC"

使用上述方法我无法过滤不同的值,因此我尝试这样

select msgid,DISTINCT(from_id) from messages where to_id='21' order by messagedate DESC

它是一个错误。需要帮助请

4

2 回答 2

1

像这样试试

select DISTINCT(from_id),msgid from messages where to_id='21' order by from_id DESC
于 2012-05-07T10:58:18.993 回答
0

查看GROUP BY声明

我认为你需要类似的东西

SELECT    msgid, from_id 
FROM      messages
WHERE     to_id = 21
GROUP BY  from_id
ORDER BY  messagedata DESC

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

于 2012-05-07T10:53:51.230 回答