找到你要的答案

Q:Displaying the latest data in SQL

Q:在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 :

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'];
}

显示此

 - mid    ticketno
 - 2-------21510
 - 1-------24693
 - 4-------24693

我想要的是什么

 - mid    ticketno
 - 2-------21510
 - 4-------24693

我的数据库:

answer1: 回答1:

Use ORDER BY . Try with -

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

使用顺序。试着用—

$query="SELECT mid, ticketno FROM tbl_message GROUP BY ticketno ORDER BY mid DESC";
answer2: 回答2:
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
                  )
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
                  )
answer3: 回答3:

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.

Okay so basically I just changed varchar to int (type of TICKETNO) then it returns the most latest ticketno.

我使用这个查询

$query="SELECT MAX(mid) as mid, ticketno FROM tbl_message GROUP BY ticketno";

我用max()只返回最新的票没有和组ticketno以便它会返回其他独特的票号

我猜varchar读空间。

php  mysql  sql