您好我正在尝试从同一个表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
这是一个错误。需要帮助请
答案 0 :(得分:1)
试试这个
select DISTINCT(from_id),msgid from messages where to_id='21' order by from_id DESC
答案 1 :(得分: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