shin
shin

Reputation: 3659

Displaying the latest data in SQL

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 :

enter image description here

Upvotes: 1

Views: 85

Answers (3)

Javaluca
Javaluca

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

shin
shin

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

Sougata Bose
Sougata Bose

Reputation: 31749

Use ORDER BY . Try with -

$query="SELECT mid, ticketno FROM tbl_message GROUP BY ticketno ORDER BY mid DESC";

Upvotes: 5

Related Questions