Reputation: 3659
Can anyone please help me? I'm a newbie on PHP so please understand. Here's my code.
$query="SELECT MAX(mid) as mid, ticketno FROM tbl_message GROUP BY ticketno";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
echo $row['mid'];
echo $row['ticketno'];
}
DISPLAYS THIS
- mid ticketno
- 2-------21510
- 1-------24693
- 4-------24693
WHAT I WANT
- mid ticketno
- 2-------21510
- 4-------24693
My database :
Upvotes: 1
Views: 85
Reputation: 857
SELECT t.*
FROM tbl_message t
WHERE NOT EXISTS ( SELECT 'a'
FROM tbl_message t2
WHERE t2.ticketno = t.ticketno
AND t2.mid > t.mid
)
Upvotes: 1
Reputation: 3659
Okay so basically I just changed varchar to int (type of TICKETNO) then it returns the most latest ticketno.
I used this query
$query="SELECT MAX(mid) as mid, ticketno FROM tbl_message GROUP BY ticketno";
i used MAX() to return only the latest ticket no and GROUP BY ticketno so that it would return the other unique ticket no.
I guess varchar reads spaces.
Upvotes: 0
Reputation: 31749
Use ORDER BY
. Try with -
$query="SELECT mid, ticketno FROM tbl_message GROUP BY ticketno ORDER BY mid DESC";
Upvotes: 5