Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Using A Limit Clause, But Return The Number Of Rows Of The Query Without The Limit


I have heard of a cool feature that mysql provides a way to return the number of rows of an sql statement that contains a LIMIT as if the LIMIT had not been there.

I search the mysql manual, but could not find anything.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
How To Get The Total Number Of Rows With A Query "limit" ?
I would like to paginate the results of a query on several pages. So I
use a query with a limit X offset Y to display X results on a page,
ok.

But for the first page, I need to run the same query with a count(*)
to know how many pages I will get (number total of rows/ X).

The problem is my query is very slow (maybe 5s) because there is much
worch to do, and on the first page, I need to run this query twice
(not exactly, but ...) so the page is very very slow to load.

My question is : is there a function to get the total number of rows
even on a query with "limit" ? Or what could I do else?

How To Get The Total Number Of Rows With A Query "limit" ?
I would like to paginate the results of a query on several pages. So I
use a query with a limit X offset Y to display X results on a page,
ok.

But for the first page, I need to run the same query with a count(*)
to know how many pages I will get (number total of rows/ X).

The problem is my query is very slow (maybe 5s) because there is much
worch to do, and on the first page, I need to run this query twice
(not exactly, but ...) so the page is very very slow to load.

My question is : is there a function to get the total number of rows
even on a query with "limit" ? Or what could I do else ?

How To Get The Total Number Of Rows With A Query "limit" ?
I would like to paginate the results of a query on several pages. So I
use a query with a limit X offset Y to display X results on a page,
ok.

But for the first page, I need to run the same query with a count(*)
to know how many pages I will get (number total of rows/ X).

The problem is my query is very slow (maybe 5s) because there is much
worch to do, and on the first page, I need to run this query twice
(not exactly, but ...) so the page is very very slow to load.

My question is : is there a function to get the total number of rows
even on a query with "limit" ? Or what could I do else ?

Limit Number Of Rows
I was wondering how can I limit the number of rows used in a table.
For example if we wanna create a table named table1 having only 7 rows.

Retrive A Limit Number Of Rows
How can I make the following query :
I would like to retreive only the number of rows while sum(field1) < 20. Is this possible?
If my table have the following values :

1
4
6
8
5

It only retrieves the first 4 rows. sum(field1)=19

Limit Number Of Rows Or Characters Returned In A Text Field
I understand you can limit the rows of a recordset, but can you somehow tell a query to limit the rows (or characters) returned within just a text field?

I have a page that returns abstracts of many articles and I want to truncate the description for each to just 200 characters. Is there a way to accomplish this with MySQL?

Using Limit On Grouped Query With Large Number Or Records
I have a query that use a "group by" clause that returns 600+ queries

however if I add a LIMIT 500,50

It returns 0 records. I tested LIMIT 450,50 and it returned only 34 records.

Is there something limiting the limit? Is there an option in mysql that can increase this limit?

Select Query With Limit Same As No Limit?
I have a question, EXPLAIN on a SELECT query seems to return the same as explain without doing a limit.

Here is a dump from my console:

Using WHERE Clause With LIMIT
Can I do something like this in MySQL:

LIMIT Clause
I want to be able to use LIMIT to control how many rows from one table are
returned, independent of how many rows there are in a second table that is
joined to the first. Code:

LIMIT Clause
I need to retrived a specific row from a query, these these specific
row number is the result of another query.

Is it possible to do that in one query with a subquery (mysql 4.1.x)?
Here is how I try (that gave me a syntax error.):

SELECT a FROM t1 WHERE a = 1 ORDER BY LIMIT (SELECT b FROM t2 WHERE c
= 2),1;

############
the 2 query standalone worked.

SELECT b FROM t2 WHERE c = 2; # return 3

So:
SELECT a FROM t1 WHERE a = 1 ORDER BY LIMIT 3,1; # return the good row

Limit Return Results
I am using perl. I am looking for ideas on how to return 10 rows at a time and then have the number of pages on the bottom of the web page so the user can view the first 10 rows then click on page 2 and view the next 10 rows, and so on..

Math Expression In The LIMIT Clause?
SELECT code, name, thumbnail, image
 FROM inventory
 WHERE (category_id = 6)
 ORDER BY name
 LIMIT ((2-1)*15) , 3

I need this query statement to have the math calculation in the LIMIT clause to adjust for the page links.

((current_page_link-1)*maxrows/pg)

It keeps getting tossed out because of the (brackets) surrounding the numbers. Is there any way to do it?

Workaround For MySQL's Inability To Use @variable In LIMIT Clause?
SET @rows=0,@chars_so_far=0,@limit=100;
SELECT @rows:=@rows + 1, @chars_so_far:=chars + @chars_so_far, @limit=IF(@chars_so_far >= 4000, @rows, @limit) FROM content ORDER BY id DESC LIMIT @limit;
What it does (or rather, should do) is select rows until @chars_so_far is greater than or equal to 4000. Each row has a chars field holding the number of characters for that article, which is added to the @chars_so_far variable each time a row is grabbed. The IF () says that if we've reached the character limit, set the limit to @rows (so basically, "LIMIT <however many rows have already been fetched>"). If not, limit stays at 100.

MySQL 4.1, it seems, doesn't like LIMIT @limit. Can anyone else think of a way to grab the first ~4000 characters worth of rows?

Limit Number
4671 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.60 mysqld
4672 mysql 8 0 11416 11m 1252 S 0.0 12.4 0:00.00 mysqld
4673 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.00 mysqld
4674 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.00 mysqld
4675 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.07 mysqld
4676 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.00 mysqld
4677 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.01 mysqld
4678 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.00 mysqld
4679 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.00 mysqld
4680 mysql 9 0 11416 11m 1252 S 0.0 12.4 0:00.07 mysqld

I really don't need all these, they're just eating all my ram. I think 2-3 would suffice for my low-traffic website. Where can I limit the number of processes spawned by mysql?

Table Number Limit?
Is there a limit to the number of tables allowed in MySQL? Is there any
non-obvious performance hit associated with having too many tables?

Assume the data size will be the same. In other words, having a table that
is 1.5Gig versus having 1000 tables that are each 1.5MB in size.

LIMIT And Total Number
is there any possibility to find out total number of rows when I use LImit

Number Of Columns In Table ? Is There Any Limit?
My question may sound stupid but i really want to know if there is some kind of recommended limit for number of columns in a table of mysql table.
For example, is it okay if i make 100 colums in one mysql table? or i should make more tables to decrease number of columns?

Limit The Number Of Characters Shown
I'm trying to do a news page; I can get the info out of the table ok but is it possible to limit the number of characters shown?

I want to just show the first, say, 255 characters of the 'reportbody' field on the home page with a link to click through to the full story.

Get Total Number Of Results But Still Use LIMIT?
Is there a (better) way to get the toal number of results from a query
but still limit them with a LIMIT clause. For example, in a Google
search result, it displays 1-100 of 10,000,000 results. Do they hit
the database once for results, and again for a total count or is it
all in one query?

Limit On Number Of Tables In Mysql
In the mysql cluster docs it says 1600.

but is that something different from mysql? I am just interested in plain mysql.

Counting The Number Of Records That's Greater Than The Limit
I have a table that has thousands of schools in it. I'm returning all the schools in a certain state. I'm limiting it to 20 per page. However, I want to display text that says how many there actually are...exampe: 'showing 1-20 of 43'.

Is this possible? My query is below:

SELECT id, city, CONCAT(school_fname, school_lname) AS thename FROM ccn_schools WHERE state='KS' ORDER BY CONCAT(school_fname, school_lname) LIMIT 20

So how do I have to run this query without a limit to get that number?

Limit The Number Of Databases A User Can Create?
how can I limit the number of databases a user can create? I wish to create an account in mysql for each site on my host computer and set the max number of databases each site can create.

Limit The Number Of Databases A User Can Create?
how can I limit the number of databases a user can create? I wish to create an account in mysql for each site on my host computer and set the max number of databases each site can create.

Too Many Connections Error, Well How Do I Increase Number Of Connection Limit?
Im always getting a too Many connections error, My website has increased in traffic by 800% this month, Is there anyway to increase the max connection limits on my servers?,
A fast responce will be welcome,
Thanks for reading,

Limit To The Number Of Columns/fields That Can Be Held In A Table?
is there a limit to the number of columns/fields that can be held in a table?

Limit Rows
I got a forum with a table looking like this

ID (int)
name (varchar)
email (varchar)
insert_date (int)
parent (int)
message(longtext)

How do I make the select query so that I only get one row that has a uniqe parent? The parent is the thread-id in the forum and I'm building a search-engine, and when I search in the forum I only want to get one instance of thread, I mean, if it finds the query several times in a single thread, I don't want it to list the thread more than one time anyway, as of now it lists a thread many times because it finds the query several times in it.

Another question:
I got a Q&A table where users have sent in their questions and someone have answered, the table looks like this:

id (int)
date_insert (int)
title (varchar)
message (longtext)
name (varchar)
answered (int)
answerid (int)

only those which have [answered]=1 will be shown cause I don't want to show a unanswered question, the answer is a new post in the same table but the [answered] is 2 there, just to mark it as a answer and not a question. The [answerid] points to the id where the answer is and it's NULL if it is a answer.

I'm making a searchfunktion to this to and I need to search in both question and answer but I only want to list one title per answer/question. As of now it searches in both answer and question and lists em both if it finds the query in both of them but I only want it to list it once. Lets say that I search for "what if", and "what if" is in both the question and answer it will list them both as to results, but I only want one result
ie.
If it finds it in only the question - one result
if it finds it in only the answer - one result
if it finds it in both the answer and the question - still only one result.
that's what I want.
How do I do that?

Limit The Selection To 25 Rows
I am using MySQL 3.23. I would like to know how I can limit the selection to 25 rows based on the latest 25 tests and same batch. For example;

select SN from tester where current_timestamp() > Date and UPS BC-06X1-H-SP-AUX1-C = Batch,? limit to 25 rows?

Limit Rows In MySql 4.1
I've used the LIMIT function in mySql 5.0 with no problem. Such as:
sql = "select * from Events order by EventDate DESC, 'LIMIT 0,3'"

I have tried to use the same query on a mySql 4.1.22 server and I don't get an error but it doesn't adhere to the LIMIT part of the query.

Any ideas on a way to pull four rows from a table without the LIMIT function? I want the four most recent entries in the table.

Limit Rows Showing
I am working on a Rare Values system as an addon for my DJ Panel and need to figure out how to limit the number of images shown per line. I use the below code to put the images of the items into a table but how can I make it so when 11 items are displayed it inserts a "</tr><tr>" and then starts a new line?

$result = mysql_query("SELECT * FROM rp_rares ORDER BY id ASC");
echo '<center>
<table width="475px" border="0" align="center">';
while($row = mysql_fetch_assoc($result))
{
echo '<td width="40px" align="center"><img src="' . $row['img'] . '"></td>';
}
echo '</tr></table>';

Limit Amount Of Rows In Table?
I would like to have a table which only holds the last 50 added rows.
Table: userips

id, userip

4, 22.22.22.22
5, 23.22.22.25
6, 25.22.22.26
|
53, 22.55.55.112

Now if a new ip is INSERTed the last one has to be removed so only 50 rows will be stored at all time.I can do this by first inserting a new ip and then deleting a row which requires 2 query's but is there a more efficient way?

Return A Count Of Number Of Rows Before Desired Row.
Anyone know how? find_in_set doesn't seem to be able to do what I want since I am not searching through an array but am actually running a query.

What I really want is to be able to select all until something = something. I've searched the realm of google but cannot come up with anything useful, and so I am wondering if maybe I don't know the right words to be looking for.

Simple Sql Select Does Not Return The Correct Number Of Rows
I have 2668 rows in my table.  

select count(distinct ID) from mytable;

gives the following results: 2668

However, when I use:
 
select ID from uniprot;

LIMIT Query
I have a query that returns all the room numbers in a hotel.  What would be the proper syntax for a query to return one instance of each room.  e.g. i want 1 2 3 4, etc. instead of 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3, etc.

Using LIKE And LIMIT In A Query
Is it possible to use LIKE and LIMIT in a query? I am trying to get search results and limit them.

Ive tried different ways but it doesn't seem to work. I have looked up syntax for both LIKE and LIMIT but cannot find one that includes both.

$query = 'SELECT * FROM table WHERE rows LIKE "%$q%" AND LIMIT 0, 10';
$query = 'SELECT * FROM table WHERE rows LIKE "%$q%", LIMIT 0, 10';
$query = 'SELECT * FROM table WHERE rows LIKE "%$q%" LIMIT 0, 10';

Limit SUM() Query
I am trying design a report that will query the "order" table and give me a sum of the "grandtotal" column for the sales in a particular range of dates.
I have a query that selects the data with the range of dates:
my($query) = "SELECT * FROM $sc_mysql_ord_table WHERE time > $sdate AND time < $edate ORDER BY $in{'sort'}";

Query With Limit 3 But Minimum 1
How do I run a query with limit 3 but a minimum of 1? For instance, I'd like to grab news items from the last 3 days. If, however, there are no items from the last 3 days, it grabs the single newest.

Eliminate LIMIT From Sub Query
I have a single table that tracks all revisions to all issues. The format is like so:

id - auto_increment
issue_id - foreign key to an issue
changed_at - timestamp
assignee - foreign key to a user
change_made - varchar of change that was made at that time.

I need to select the latest change to every issue for one specific user. I would normally do this with:

SELECT * FROM historical_data AS hd WHERE hd.id IN (SELECT id FROM historical_data WHERE hd.id = id ORDER BY changed_at DESC LIMIT 1) AND assignee = 6;

Of course this gives the "You can use LIMIT & IN at the same time" error.

Is there something else I can do to get the same result ... besides moving to Postgres?

Limit Query To 1st Occurrence Of A Value
Is there a way to limit the output of a query to only include the incurance of certain values only once?

example: ....

Limit A Delete Query
i have table B with FK to A (many to one)

I want to delete big amount of data from A,B (all the data that already read from the table and flaged as readed)

since i have the FK i try to delete data from B before delete A

i run this query
delete from B using B,A where Query_events.flag='yes' and B.A_B_Hjid = A.hjid

and it is ok . the problem that i want to limit it to 10000 records each execute

so i added delete from B using B,A where Query_events.flag='yes' and B.A_B_Hjid = A.hjid limit 10000

but i have problem with the limit....

Improving LIMIT X,Y Query
I have only 1m records in my database running on a laptop of speed
1.6GHz, memory 512MB ram, and Toshiba MK8032GAX hard disk.

I use 'LIMIT x,10' for the query to utilise record paging.
When the value of x is nearer to 0, the query speed is fast.
Presently, 'LIMIT 0,10', 'LIMIT 50000,10' and 'LIMIT 100000,10' takes
about 0sec, 11secs and 4mins respectively.

1) Is there anything I should do, in terms of sql statement or database
design, to obtain the same speed for cases where x is in the middle or
nearer to the end?

2) How fast is a simple select statement (not select count(*)) on a
system like mine?

3) Suppose I left my original select .. limit query as is, what is the
minimum hardware that I need to improve the speed?

LIMIT In Delete Query
I want write delet query who don`t delete 10 lines in table and delete all rest but I don`t know how, I`ll use ORDER and LIMIT but I don`t know how I can limit that with LIMIT!
I know this: LIMIT 10,[some number]
But I want delete all and I don`t know that number (rest after 10 lines.

Double Limit Query
I have 100 records in a table and I've used paging and showing 5 records per page. But I want to get only 40 or 50 total records from 100 from the table and then want to use paging and display 5 products each page. How should I do that. Because In a query i only can use 1 LIMIT that I used for paging but how to ristrict the query to get only 40 or 50 records? Please Help

SELECT * FROM temp LIMIT 0,5;

but how to get 40 or 50 total records?

Select Query Using Limit
i have the basic sql query as follows:

$sql = "select * from photo where CategoryId='$idc' order by Id limit $start, 9";

this will select all rows from db photo based on CategoryId, then stop after it gets the 9 beginning at $start right?

start is the value of the ID i want to start the select from. in this example, i have valid db values ranging from id: 11-20. the 1st page that runs this query has start=11, and displays for 11-19. when i goto the next page setting start=20, i get nothing. Limit returns 9 rows starting at $start right? or am i misunderstanding how all this works?

Optimizing A SELECT .. WHERE .. LIMIT Query
(using MySQL 5.0)

First, the problem: I need to select different "pages" of IDs that match a small list of categories.

Here's my table:

CREATE TABLE `testTable` (
`id` int(10) unsigned NOT NULL auto_increment,
`category` tinyint(3) unsigned NOT NULL,
`username` char(15) NOT NULL,
PRIMARY KEY (`id`),
KEY `cat` (`category`)
) ENGINE=MyISAM AUTO_INCREMENT=1000001 DEFAULT CHARSET=latin1
And an EXPLAIN of my query:

EXPLAIN SELECT * FROM myTestable WHERE category IN(5,8,11) LIMIT 5000,20;

id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | myTestable | range | cat | cat | 1 | null | 49904 | Using where
So from that we can see it's hitting 50,000 rows to get the 20 rows that I want (it's a table with 1,000,000 rows total).

I've been using MySQL for a fair amount of time, but I haven't ever done much optimization beyond the standard "add an index on it".

Simple Query Question: LIMIT
I'm looking to list all the rows in a database, but skipping the first 5. I know that I can use "LIMIT 0,5" to get the first 5 results, but what query do I use to list everything from 6th result onwards?

Limit Text Output In SELECT Query
I'm looking to output a list of news stories, but only outputting the first 100 or so characters of the text and only limited to the newest 4 entries in descending order.

At the moment I'm using this nugget: But it doesn't work.

$result=mysql_query('SELECT newsheading, (LEFT newsbody,100), newsdate FROM news ORDER BY newsid DESC LIMIT 4');

What Query To Check If Any Rows Exist Satisfying WHERE Clause?
I'm looking for a query that will check if any rows exists in a table according to a WHERE condition. I know I can use COUNT(*) but then mysql will do unnecessary task of counting all the rows whereas I just need true or false. So far I did this:

SELECT COUNT(*) AS exists FROM mytable WHERE ...
Sometimes I just select the first row and check later in php how many rows have been returned:

SELECT some_col FROM mytable WHERE ... LIMIT 1
But I cannot do this check (or can I?) in sql alone and I have problems when I want to use this in a subquery, for example:

SELECT id,
name,
(SELECT COUNT(*) FROM mytable WHERE ...) AS exists
FROM othertable
WHERE surname='xxx'
Can I do the same without using COUNT(*)? I would like a query that returns 0 or NULL if no rows were found, or 1 (or some other value) if 1 or more rows were found.

Trying To Count The Number Of Rows In A Result Set After Query
The user fills out this form to sign up to the website, the form checks the database to see if the username has already been taken with the code:

$conn = mysql_connect("localhost:3306", "root", "********")
                                    or die ("Error With Connection");
        echo("connected<br><br>");
        $db_sel = mysql_select_db("game",$conn)
                                    or die ("Error With Database");
        $check = "select * from users where 'username' = '$username'";
        $db_sel = mysql_query($check,$conn)
                or die (mysql_error());


Copyright © 2005-08 www.BigResource.com, All rights reserved