Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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

Posts: 275
Trades: 0
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
 
 
Register now for full access!
Old 08-27-2006, 08:46 PM Re: LIMIT ? EG. show last 10 records in table ?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
SELECT TOP 10 fieldlist FROM table ...
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
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: 215
Name: Syed Saadat Ali
Location: Lahore, Pakistan
Trades: 0
Read this:

MySQL 5.0 Reference Manual :: 13.2.7 SELECT Syntax
__________________
- -- --- ---- ----- ------ ------- ---------------
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 ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
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
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
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 ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
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
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
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 ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
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 ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
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:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 online now
Reply With Quote
View Public Profile
 
Old 08-29-2006, 11:51 AM Re: LIMIT ? EG. show last 10 records in table ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
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:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 online now
Reply With Quote
View Public Profile
 
Old 08-29-2006, 12:43 PM Re: LIMIT ? EG. show last 10 records in table ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
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
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
TOP does not exist in MySQL so will cause an error.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
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 ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0


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:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 online now
Reply With Quote
View Public Profile
 
Old 08-29-2006, 02:35 PM Re: LIMIT ? EG. show last 10 records in table ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
What where the people thinking that made MySQL ?

So... it looks like im stuck
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 02:55 PM Re: LIMIT ? EG. show last 10 records in table ?
Extreme Talker

Posts: 246
Trades: 3
What version of PHP and MySQL is running on your server?
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 03:24 PM Re: LIMIT ? EG. show last 10 records in table ?
tomcat_fo's Avatar
Ultra Talker

Posts: 275
Trades: 0
the PHP is v.5.something, but i dont know what version MySQL is..... but i guess my host uses the newest version
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-29-2006, 03:50 PM Re: LIMIT ? EG. show last 10 records in table ?
Extreme Talker

Posts: 246
Trades: 3
There goes that theory...
Can you run the query in phpMyAdmin, or something similar? Then you would eliminate the mysql_query vs mysql_db_query question. If it works in phpMyAdmin, you should be able to get it to work in PHP, but it may be a syntax error, or incorrect usage of a function. If it _doesn't_ work in phpMyAdmin, (meaning using "LIMIT 10" still returns more than 10 records), then there is something else wrong with MySQL.
CouponGuy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to LIMIT ? EG. show last 10 records in table ?

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.63092 seconds with 12 queries