Reply
LIMIT ? EG. show last 10 records in table ?
Old 08-27-2006, 08:35 PM LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270
How do you guys use LIMIT in MySQL ?

I print some records from my DB that are sorted by date, but i only want to print the last 10 records....

have tryed LIMIT 10, but it seems like everything is printed anyway
tomcat_fo is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 08-27-2006, 08:46 PM Re: LIMIT ? EG. show last 10 records in table ?
chrishirst's Avatar
Super Moderator

Posts: 15,326
Location: Blackpool. UK
SELECT TOP 10 fieldlist FROM table ...
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System | Bits & Bobs
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-28-2006, 09:40 AM Re: LIMIT ? EG. show last 10 records in table ?
saadatshah's Avatar
Extreme Talker

Posts: 216
Name: Syed Saadat Ali
Location: Lahore, Pakistan
Read this:

MySQL 5.0 Reference Manual :: 13.2.7 SELECT Syntax
__________________
Traffic School - Driver Ed - Defensive Driving - Texas Defensive Driving - Online Traffic School - Defensive Driving Course
If you have knowledge, let others light their candles in it.
saadatshah is offline
Reply With Quote
View Public Profile Visit saadatshah's homepage!
 
Old 08-28-2006, 06:32 PM Re: LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270
Quote:
Originally Posted by chrishirst View Post
SELECT TOP 10 fieldlist FROM table ...


I cant get that to work....


when you say fieldlist.... do you then mean my attributs ? .... have tryed to insert them there but it will not work.

Should it not be possible to show the last 10 records in a table by using LIMIT..... have tryed LIMIT 0,10 and 10,10 but non of it gives the right output.
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-28-2006, 08:14 PM Re: LIMIT ? EG. show last 10 records in table ?
chrishirst's Avatar
Super Moderator

Posts: 15,326
Location: Blackpool. UK
fieldlist is comma seperated list of the columns you want to retrieve from the table (or tables in the case of a JOIN query)
TOP is not supported in MySQL apologies obviously missed the MySQL bit

LIMIT with retrieve a number of rows starting from a known point.
So;

SELECT columns FROM table LIMIT 0, 10 will return the first 10 records as it will start at position 0
LIMIT 20, 15 will return records 21 through to 35

using LIMIT to return the last records in natural order you need to know how many records there are in the table and start from 10 before.

to get the last 10 records you will have to reverse the table order.
so;
SELECT fieldlist FROM table ORDER BY field DESC LIMIT 10

if your version of MySQL supports subqueries (V5 on) it should be possible to do this,

SELECT fieldlist FROM table LIMIT (SELECT COUNT(*) FROM table)-10 ,10
Not tested BTW

other than that you can do 2 queries and use server side code to set the limits

pseudocode (convert into whatever server side code you are using)

SELECT COUNT(*) as rec_count FROM table
var = RS("rec_count) - 10
SELECT fieldlist FROM table LIMIT var , 10
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System | Bits & Bobs
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-29-2006, 07:08 AM Re: LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270
I just get errors when runnig any of that... - mybe it is the fieldlist thing....


But this code will run:
PHP Code:
<?
$test
mysql_db_query("$datab""SELECT * FROM ADD WHERE DATE_SUB(CURDATE(),INTERVAL $expire DAY) >= date AND groupid=1  ORDER by date desc LIMIT 10");
while (
$row mysql_fetch_array ($test)) { 
$test1 "".$row["id"];
$test2 "".$row["test2"];
$test3"".$row["test3"];

print(
"$test2<br>");

?>
}
Put when that runs, arround 20 records are printet even though limit is set to 10...... ORDER by date desc LIMIT 10"); should give me the last 10 record inserted into the DB ?
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 08:00 AM Re: LIMIT ? EG. show last 10 records in table ?
chrishirst's Avatar
Super Moderator

Posts: 15,326
Location: Blackpool. UK
if you have a table called add it will cause problems

add is a reserved word in SQL

using SELECT * is a very inefficient way of querying the DB, the server has to parse the tables twice, once to get the column names and then again to get the data.

in your example above fieldlist would look like

SELECT id,test2,test3 FROM ...
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System | Bits & Bobs
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-29-2006, 08:25 AM Re: LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270
Well i did translate some faroese words in my table last time, but here is how it looks right now:
PHP Code:
<?
$bilar 
mysql_db_query("$datab""SELECT id, yvirskrift, lysingbolkid, date FROM lysingar WHERE DATE_SUB(CURDATE(),INTERVAL $numberdays DAY) >= date AND lysingbolkid=1 ORDER BY date DESC LIMIT 10");
while (
$row mysql_fetch_array ($bilar)) { 
$lysing_id2 "".$row["id"];
$yvirskrift2 "".$row["yvirskrift"];
$bolkid2 "".$row["lysingbolkid"];
$bolkid2 "".$row["date"];

print(
"$yvirskrift2<br>");


?>
it prints arround 25 records, so there is a problem.... only want 10
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 11:21 AM Re: LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270
now i'v tryed the TOP thing again....

I get this error message:
Code:
Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/home/web/web128130/kikarin/newcar.php on line 6
My code looks like this:
PHP Code:
<?
$bilardays 
mysql_db_query("$datab""SELECT TOP 10 id, yvirskrift, lysingbolkid, date FROM lysingar where lysingbolkid=1");
while (
$row mysql_fetch_array ($bilardays)) {   //this is line6
$lysing_id "".$row["id"];
$expire "".$row["dagar"];
$yvirskrift "".$row["yvirskrift"];
$bolkid "".$row["lysingbolkid"];



$bilar mysql_db_query("$datab""SELECT TOP 10 id, yvirskrift, lysingbolkid, date FROM lysingar WHERE DATE_SUB(CURDATE(),INTERVAL $expire DAY) >= date AND lysingbolkid=1 ORDER BY date DESC");
while (
$row mysql_fetch_array ($bilar)) { 
$lysing_id2 "".$row["id"];
$yvirskrift2 "".$row["yvirskrift"];
$bolkid2 "".$row["lysingbolkid"];
$date2 "".$row["date"];

print(
"$yvirskrift2<br>"); ?>
}
}

I usualy get a error message on that spots in other files as well when there is something wrong with the line abow

Last edited by tomcat_fo : 08-29-2006 at 11:24 AM.
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 11:37 AM Re: LIMIT ? EG. show last 10 records in table ?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,339
Name: Keith Marshall
Location: West Hartford, CT
I don't know if it makes a difference, but mysql_db_query() is depreciated, use mysql_query() instead.

PHP: mysql_query - Manual
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 11:51 AM Re: LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270
NOw i got this message:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /usr/home/web/web128130/kikarin/newcar.php on line 5

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/home/web/web128130/kikarin/newcar.php on line 6
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 12:09 PM Re: LIMIT ? EG. show last 10 records in table ?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,339
Name: Keith Marshall
Location: West Hartford, CT
PHP Code:
$bilardays mysql_query("SELECT TOP 10 id, yvirskrift, lysingbolkid, date FROM lysingar where lysingbolkid=1"); 
while (
$row mysql_fetch_array($bilardays)) { . . . 
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 12:43 PM Re: LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270
When i use that one i get:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/home/web/web128130/kikarin/newcar.php on line 6


I was wondering if i'v made misplaced a " ' ( ) { or } but can't see anything like that

Last edited by tomcat_fo : 08-29-2006 at 12:46 PM.
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 12:49 PM Re: LIMIT ? EG. show last 10 records in table ?
chrishirst's Avatar
Super Moderator

Posts: 15,326
Location: Blackpool. UK
TOP does not exist in MySQL so will cause an error.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System | Bits & Bobs
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-29-2006, 01:31 PM Re: LIMIT ? EG. show last 10 records in table ?
Ultra Talker

Posts: 270


So.... anyone got a clue what to do ?


Maybe a good ide to use MIN or MAX instead on E.G. the ID or DATE attribut, and SORT BY something DESC LIMIT 10?

Last edited by tomcat_fo : 08-29-2006 at 01:42 PM.
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 01:54 PM Re: LIMIT ? EG. show last 10 records in table ?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,339
Name: Keith Marshall
Location: West Hartford, CT
Quote:
Originally Posted by chrishirst View Post
TOP does not exist in MySQL so will cause an error.
Oops! My bad!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 02:35 PM Re: LIMIT ? EG. show last 10 records in table ?