[英]select query to get all values and also distinct value
嗨,我試圖從同一個表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
這是一個錯誤。 需要幫助
這樣嘗試
select DISTINCT(from_id),msgid from messages where to_id='21' order by from_id DESC
查看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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.