MySQL geriye tablo dönmek yapma

8 Cevap php

I want to make a simple news system using PHP and MySQL, right now I got a working post and read system but there is only one problem, I want it to show the 10 latest news but instead it shows the 10 oldest news.

My question is: Is there a way to make MySQL return the results from the bottom of a table or do I have to first get the number of posts and then limit it to the very last 10 ones?

Here is the insert (title and text is escaped and time is time(), poster is not done yet):

mysql_query("INSERT INTO news (title, poster, text, time) VALUES ('$newstitle', '1', '$newstext', '$time')") or die(mysql_error());


Ve (addnews yankılanırken onu) onu retrive:

$myqr = mysql_query('SELECT * FROM news LIMIT 10') or die("Error running news query: ". mysql_error());
while($myres = mysql_fetch_array($myqr))
{
addnews($myres['id'], $myres['title'], "admin", date('l jS F Y - H:i:s', $myres['time']), $myres['text']);
}

Yani, kısa: Ben geriye veritabanını okumak istiyorum, bu mümkün mü?

8 Cevap

ORDER BY maddesini göz atın. Bu sipariş artan veya azalan bir sütuna göre satırları sıralamak için izin verir. Aşağıdaki sorgu azalan sırayla zamanına göre sıralanır 10 haberleri, dönecektir.

SELECT * FROM news ORDER BY time DESC LIMIT 10

Basit, sadece, örneğin, sizin SQL cümlesinin "ORDER BY" bir eklenti

SELECT * FROM news ORDER BY time DESC LIMIT 10

Eğer oluşturulduğu tarihe göre sıralamak için sorguyu değiştirmek gerekir. gibi bir şey

SELECT * FROM news  order by time DESC LIMIT 10

sizin için çalışması gerekir. Ben onun değerinde bir ORDER BY belirtmek yoksa, sonuç döndürülür sırası garanti olmadığını belirterek düşünüyorum. Şu anda, onları artan yerleştirildiği zaman sipariş almak için ne, ancak güvenle her zaman böyle olacaktır kabul edemeyiz.

Bu kimliği varsayarsak birincil anahtar:

SELECT * FROM new ORDER BY id DESC LIMIT 10

ORDER BY yan tümcesi kullanın.

Değil, sadece al zaman LİMİTİ 10 önce Azalan misiniz?

kullanmak

SELECT * FROM news ORDER BY time DESC LIMIT 10

Ayrıca kullanabilirsiniz

SELECT TOP 10 FROM news ORDER BY time DESC

Ben inanıyorum. 'Üst' maddesi ise emin değilim, SQL Server rağmen, sadece.