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




Set A Limit On Count(*) Function?


I have no way to predict how many results the count(*) function is going to find. However, executing that function on a small set of results will work very fast, and executing it on a big set of results can take minutes or even more.

Is there a way to tell MySQL not to keep counting if it finds, say 1000 rows?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Limit + Function
I have a function "MaxCars()" that returns an integer. Then I try to execute this query:

"SELECT * FROM Cars LIMIT MaxCars()"

I get an error:

"You have an error....near MaxCars()"

How do I use a function in combination with LIMIT?

LIMIT Function
Query 1 : SELECT * FROM TABLE_A, TABLE_B, TABLE_C LIMIT 10 WHERE TABLE_A.ID = TABLE_B.ID AND TABLE_B.ID = TABLE_C.ID
Query 2: SELECT * FROM TableC INNER JOIN (SELECT * FROM TableA INNER JOIN TableB ON ID) RS1 ON ID LIMIT 10

I'm currently using mysql standard v4.1.7. Whenever i run both queries (see above), it generates an error message, which is 'check version....' Could somebody pls tell me what's wrong with my syntax?

LIMIT FUNCTION
in using limit function...please help me what is the coding in displaying the remaining results if i limit the result to 10 then the actual result is 20.

SUM Operator In Subquery With LIMIT Function
I'm trying to use the SUM operator to sum some values. The thing is that these 5 values i'm using were previously retrieved by a recordset using the LIMIT operator as i wanted to get rid of the first and last values.

This way i ended up with only three values.

Now i need to sum these three values and here's the problem.

I know the following code is bad written but it's closed to what i need:

Count And Limit
Is there a way to make one query to the database grabbing the count() and limiting the query to like 10 rows, but have the count() not be limited to those 10 rows?

How To Limit COUNT(*)
I guess there is no way to limit COUNT(*). Is that right? We cannot use the
result of COUNT in WHERE condition or LIMIT doesn't help. In this case so
far I have retrieved a field and used LIMIT. Is there a better way to
control it?

How To Limit COUNT(*)
I guess there is no way to limit COUNT(*). Is that right? We cannot use the
result of COUNT in WHERE condition or LIMIT doesn't help. In this case so
far I have retrieved a field and used LIMIT. Is there a better way to
control it?

Count And Limit
I am having trouble getting the right amount of distinct results.
As a simplified example I want
Code:

select number,id,name,count(*) as total
from tablename
group by number,id
order by number desc, total desc limit 10

I want to retrieve 10 unique rows, but for example when there is 1 count with value '2' then it returns only 9 rows. How can i make this query so that is returns 10 rows?

Count () Function
Cant anyone tell me why this very basic query won't process.
This is to give a breakdown of Mem.heard 1-12 numbers ("where did you hear about us survey") for ea of the MOrig two user types

EX:
MOrig | heardof | count_heardof

UserA 1 5 (times)
UserA 2 6 (times)
UserB 1 3 (times)
UserB 2 8 (times)

SELECT MOrig, heardof, count ( heardof ) as count_heardof
FROM Mem
GROUP BY MOrig, heardof
ORDER BY MOrig

Count Function
I have a table with 40 rows. After I connect to the server and select a database I try to store the number of rows in a certain variable using php.

I'm using....

$count = mysql_query("SELECT count(*) FROM League1);
echo $count;

...when I do this it displays "resourceID#2resourceID#4"

For some reason this made me think I was getting an array instead of a straight up number so I added this line before echo $count;

$count = count($count);

....and that just displayed "11" no matter how many rows there were in the table.

After all of this I am getting the feeling that I am doing something totally wrong but everywhere I look to find out what I did wrong I get either an answer that is specific to another problem or something way too general.

Count Function
I have a database which I need to query to find out how many duplicate rows have been entered and then to use that query to count how many times the same person has inputed the data twice.
eg -

It counts the duplicate data
Then counts the amount of times that the name of the person appears in the results

I can do the counting the duplicate data but how would I go about counting the reults returned from that query

This is the query I am using to count the duplicates:

SELECT COUNT(*) AS `Rows`, `Initials`
FROM `input data`
GROUP BY `Date`, `Time`, `Name of caller`, `Animal`, `Details`, `Telephone number`, `Method`, `Passed to`, `Initials`, `DDI`
HAVING COUNT(*) > 1;

How could I count the amount of times the same initials appear.

Count Function
I would like to display a table that might look as follows in my browser:

Username Clicks
abcde 100
dfgert 85
rthxdgjn 61

The mysql code I have so far is as follows (Please note the two variables will be variables from a previous mysql_query but I 'should' be ok sorting that out):

SELECT username, COUNT( * ) AS clicks FROM ptchistory
WHERE date > $startdate AND date < $enddate
GROUP BY username
ORDER BY clicks DESC LIMIT 10


I cannot get the resultset to output in a browser window.

Count() Function
I don't know why I cannot use the count function in mysql. I used the following query at the mysql prompt, but it gave me the syntax error at Count(*)

SELECT QProgram, COUNT(*) FROM ContactUs GROUP BY QProgram;

Count Function
I am trying to create a query where I am bringing back order_date, ip_address and email_address. I only want those records where the count of the ip_address is greater than 2.
Here is my code.

select
  count(ip_address)
  order_date,
  email,
  ip_address
from risk.fraud_orders
where sales_district = "IN02"
      and order_date >= "2005-01-01"
      and count(ip_address) > 2;

I get an illegal group by function. What do I need to do to fix this code?

Count Function & Group By
Getting an error saying I cannot Group on JState2

Is this a version issue? What is an alternate?
(want to show only results having greater than 2 of the same JState 's)

SELECT COUNT(JState) as JState2, JID, StateTerm
FROM JPosts2, State
WHERE State.StateAbbr=JPosts2.JState AND (`JClass` = 'Mechanics')
GROUP BY JState2 HAVING JState2 >4
ORDER BY JState

Count Untill Function
I am trying to create a leader board table which is created like so:
User_ID, Week, Weekly_Points, Overall_Points
I need to sort by overall points to find the users position in the overall leader board. This would be easy with a view but in mysql 4 I have been told they are not supported. What way should I go?

Wot Is The Function To Count The Records In Selelct Statmtn In MySQL
wot is the function to count the records in Selelct statement in MySQL?

Just like
PHP

mysql_num_rows($result);

in php.

SELECT COUNT(*) Or SELECT .. LIMIT 1?
If the purpose of a query is to see if atleast 1 record exists which would be faster?

SELECT COUNT(*) or SELECT .. LIMIT 1?

Both would use a WHERE clause.

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.

IF Function,GROUP BY,aggregate Function Problems
Yep, I have all those problems in the title. So I'll explain each one at a time - I did have another thread relating to this very same query but I thought it was time to update where I am with the query because at the moment I feel like I am getting nowhere!

The query I have basically searches through an items_ordered table through each product and checks to see whether the item is VATable or not. This is not where I have the problem though. Where I am really having the first problem is when I am trying to use the IF function to check if the TOTAL of an order is over £300. IF it is then I multiply it my 0.95 (i.e. 5% off). With the query below I get no errors but neither do I get the desired result. It's as though it couldn't even see it. =....

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:

#1136 - Column Count Doesn't Match Value Count At Row 1
I'm getting the above error with the following SQL Statement. I cannot seem to find the error in the code. The select statement does pull multiple rows.

I'm using my SQL version: 4.1.19

Any clue on why this isn't working?

INSERT INTO `Grants` ( `Project_Code` , `Grant_Code` , `Fiscal_Year` , `Capital` )
VALUES (
(
SELECT Project_Code, Grant_Code, FY, SUM( Capital )
FROM Grants_Temp
WHERE Project_Code = 'OSUT'
AND FY = '2006'
AND Claim_Month = '072006'
GROUP BY Grant_Code
ORDER BY Grant_Code
)
)

Column Count Doesn't Match Value Count At Row 1
I am getting the following error when I run my query.
Column count doesn't match value count at row 1

I have looked up this error and have checked and I appear to have the right number and names in my query. In the DB table I have 34 columns, and that is what I have in the query.

The last 4 in the query are for the names of the images being uploaded, but am not sure how this all works, so I don't know if I need them, but have them there until I know for sure. Comment, Purchase and Remarks are not used in this form, but I have added them so everything is being shown in the query. Code:

Count(*) As Count And TotalCount Of Count
data in myTable1

(n) country_id
(1) 3
(2) 1
(41) 1
(5) 2
(6) 3
(7) 4

data in myTable4

(time) param
(10 : 10) c=4
(10 : 12) c=2
(10 : 30) n=41&k=5
(10 : 35) c=1
(10 : 37) n=5
(10 : 50) c=2
(10 : 54) c=2
(10 : 55) n=1&cate=6
(11 : 12) c=2
(11 : 15) n=7
(11 : 20) c=1
I have data in myTables like the above.

I have the following code.

code

(select left(time,2) as hour, count(*) as count
from myTable4
where

left(param,1)='c'
and
substring(param,3,1)=2

group by left(time,2)
)
UNION all
(select left(time,2) as hour, count(*) as count
from myTable4,myTable1

where
left(param,1)='n'
and
substring(substring_index(param, '&', 1) ,3)=myTable1.n
and
myTable1.country_id=2
group by left(time,2)
)

order by hour
And the code above produces the following result.

result

(day) count
(10) 3
(10) 1
(11) 1
The following would-be code doesn't work correctly, but it will show what I want.


would-be code

(select left(time,2) as hour, count(*) as count,
sum(count) as totalCount
from myTable4
where

left(param,1)='c'
and
substring(param,3,1)=2

group by left(time,2)
)
UNION all
(select left(time,2) as hour, count(*) as count,
sum(count) as totalCount
from myTable4,myTable1

where
left(param,1)='n'
and
substring(substring_index(param, '&', 1) ,3)=myTable1.n
and
myTable1.country_id=2
group by left(time,2)
)

order by hour
And the following is my target result.

target result

(day) count totalCount
(10) 3 5
(10) 1 5
(11) 1 5


Difference Between Count(*) And Count(1)
What is the difference between count(*) and count(1)?

Difference Of Count(*) From Count(1)
Is it true that count(1) is more efficient than count(*)? They say that count(*) still goes through all the records without needing to.

How To Fix &quot;Column Count Doesn't Match Value Count At Row 1&quot; ?
I've been trying to install a portal to my installation of Invision Power Board, and I keep coming up with this problem. I post here for two reasons: firstly is that no one is responding over at the portal creator's thread and I need help, second is that this error looks like a mySQL error that could be solved without the need of the creator if possible.

Here is the error returned:

mySQL query error: INSERT INTO `ibf_portal_box` VALUES (1, 'lang.last_topics', '<box><menu>10,20,30,50</menu>
<topics_show>10</topics_show>
<order>last_post</order>
<exforums></exforums>
</box>', 0, 'middle', '*', 0, 4, 'last_topics', '1')

mySQL error: Column count doesn't match value count at row 1
mySQL error code:
Date: Tuesday 21st of December 2004 08:43:10 PM

I am very new to mySQL and don't really understand it. What is wrong with the above that it is trying to insert, and how is it fixed?

Count(*) In A Select Returns &quot;1&quot;. It Should Behave Like Select Count(*)
i'm trying to make a query work properly but I got lost:

SELECT *,count(*)
FROM cancons c, musics m, discos d, r_discos_cancons rdc
WHERE c.c_id_music = m.m_id
AND rdc.rdc_id_disc = d.d_id
AND d.d_id_music = m.m_id
AND m.m_id = 24
GROUP BY c_id

note:
cancons (ca) = songs (en)
discos (ca) = cd's (en)
music (ca) = musician (en)

don't worry for the WHERE part. i need it because of the foreign keys.
this query returns a table with the title of the song and some more data. on the right side, I get another column called "count(c_id)" with the number "1" in it for each row. That's supposed to be due to the "group by" clause, I think.

I'd like to get the table with the songs, as usual, and, with the same query, I'd like to get the total number of rows selected.

What Does LIMIT Do?
in MySQL, when I do LIMIT x, how does the database work?

1) Does it pull everything relevant, then only takes the x starting from 0 or.

2) Does it only pull the first x amount out and stops?

I Need A Help About LIMIT
i have 10 rows in a table
i want to select the last 3 rows only
i tried: "select * from table1 order by id desc limit 0,3" it selected rows 10, 9 & 8 but i want it ordered like 8, 9 & 10, what should i do??
i also tried: "select * from table1 order by id asc limit 0,3" but it only selects the first 3 rows

it worked when i used subselect:
select * from (select * from table1 order by id desc limit 0,3) as test order by test.id asc
but my host is using mysql 3.23, which does not accept subselecting..

i also tried: "select * from table1 order by id asc limit count(*)-2,3" but it only gave me an error msg
i even tried: "select * from table1 order by id asc limit 3,-1" but same: error

Limit On A Key
I was wondering if there was a way, in MySQL, in a table definition, to set a "limit" on how big a key can get with auto_increment that's not based on data type.

More specifically, I'd like the key to be limited to 0-63 or 1-64 (the first prefered, but I know auto_increment starts at 1). The smallest integer data type is 255 values or 127 signed.

Basically, I want to be able to store these keys as a flag in a BIGINT which is limited to 64 possible flags. (If there are bigger precise data types, I'd love to know.)

I sifted thru the MySQL manual and found nothing.

Other solutions are welcome.

The requirement is to be able to store 0-* keys in a single column to be compared to a list of keys from other sources, and I think being able to use bitwise comparison is much more efficient than doing a script side cross-reference.

I could have the script manage the key, but if MySQL could do it for me.

LIMIT
how to display the remaining result using LIMIT function

ex. SELECT * FROM tbl1 WHERE title = 'field1' LIMIT 10

now the query will display only 10 retrieved records but what if found 20 records, that is my problem... how to display it.

LIMIT
When I execute the following query without the LIMIT clause, it executes
under 1 second. When I add the LIMIT clause, it takes 10 seconds to
complete. The client table contains 100,000 records. What is going on?

SELECT * FROM clients
WHERE rep_id = 1
AND last_name
LIKE '%au%'
ORDER BY first_name
DESC
LIMIT 0 , 10

Limit?
i saw the following in a mysql query:

$stmt = "SELECT
id,
first_name,
last_name
FROM
persons
LIMIT
0, 3";

in that code, what does "LIMIT" mean, and what does it do?

Limit Sql 4.1.22
error from query:

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

SELECT * FROM news WHERE news.id NOT IN (SELECT news.id FROM news ORDER BY date DESC LIMIT 15)

If limit is not supported how would i archive this?

Using LIMIT
If I'm querying a table where I know it's going to be a unique answer should I put LIMIT 0,1 . My thinking being that if the record I'm looking for is the 3rd of 250,000 then using LIMIT will stop the query after it's found the record instead of going through the other 249,997 records. Is this right?

$sql = "SELECT * FROM db_sector WHERE sector_x = '" . $x . "' AND sector_y = '" . $y . "' LIMIT 1";
$result = mysql_query($sql);
$sector_id = mysql_result($result, "0", "db_sector.sector_id");

Limit
I'm running a query to get a list of students and the class they belong to. Is there any way I can run a single query to return only 4 students from every class if there are more than 4 in some of them? The students have student ids, so I would want the first 4 student ids.

Using Limit
I am trying to use Limit in a subquery but receive an error
Is there another way to archive the same end result without Limit?
SQL:
SELECT *
FROM news
WHERE news.id NOT IN (SELECT news.id FROM news ORDER BY date DESC LIMIT 15)

Error:
#1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Limit
I want to make a top 3 lists from a table with a query like this
SELECT points FROM table ORDER BY points LIMIT 0,3

This will of course return a list of the 3 lowest values of points in the table, lets say 1,2,3.

Now to my problem...
If several records in the table has 3 points I want to have all those records returned, lets say 1,2,3,3,3

In MS Access this can be solved by SELECT TOP 3 from.......

LIMIT
If I have a sql statement like this:

Code:

SELECT id FROM `asset` WHERE field = 'value' LIMIT 20 , 40

Is it possible to know how many results there would've been if I hadn't used the LIMIT? Or will I need to do the above query and just count the results first, then apply the LIMIT and run the query again to get my limited set of results?

Limit
Could some explain the "LIMIT" function to me? For example, I have the following Code:

$sql = "SELECT name, bio, headShotFile FROM artists WHERE id='$id' LIMIT 0,1";

I bought a web site from a friend and now I'm picking apart the code and learning.
I've done some surfing, but couldn't find a clear explanation, only uses of it. Yet I want to make sure I fully understand it.

Limit
I am in the process of creating a download script, I am using a code to determine if someone is granted access or not to download the file. I want to set it up so they can only download, say, 250 MB a month worth of renders. And then, have the option for them to purchase an upgrade to their download limit, which will increase the limit for that user only.
I want to use my vBulletin memberlist for this, so I already have a database with the users.
I am a noob with MySQL though, and I am completely stumped. I tried this with PHP but failed?

Using LIMIT 0,1
Let's say I do a check for a duplicate email address in a database by using

SELECT COUNT(*) FROM emails WHERE email='' .$email. ''

Will it be faster (as in end sooner) if LIMIT 0,1 is stuck at the end of that query, or does it not matter?

LIMIT Problem
I am having a problem with this mySQL query:

SELECT * FROM my_table LIMIT 4,10

When I do this query in phpMyAdmin, it works like it should - returning 10 records from row 4. When it is in my code, it always returns 10 records from row 0.

Why would the exact same query work in phpMyAdmin but not in my code?

Joins And Limit Used Together
I have a query, which reads all information from tables.

select se.section_name,pr.*,ph.code from prices pr left join pharmacy ph on pr.pharmacy_id=ph.id left join sections se on pr.section_id=se.id where medication_id=7 order by se.order_id asc, cast(`dosage` as SIGNED) asc, cast(`total_price` as SIGNED) asc

result can be viewed here:
http://www.foreign-drugstores-online...oduct.php?id=7

I need to read only top 5 (total price) for each `dosage`

somekind of: group by `dosage` order by total_price desc limit 5 per group

GroupBy And Limit
I have the following query which I would like to sort by a dateToPull field that is stored in a table called date.

SELECT story.title, story.article, story.deptId, story.authorId, author.author FROM story, author WHERE homePage = 1 AND story.authorId = author.id
My goal for the query is to retrieve all the stories that have not yet reached their respective date to be pulled from the page, after retrieving these stories I then will limit only three to be displayed. My attempts to use and IF statement did not work but I have never used GroupBy. I plan to use the LIMIT function at the end of the statement to display only three stories.

The above query works great, except it does not yet have the function to sort by date. Where do I begin to approach this problem from?

Issue With LIMIT
PHP

SELECT item.itemId, item.itemName, 2 * SUM(bans.bansForAgainst) -
COUNT(bans.bansForAgainst)
AS count, bans.bansForAgainst, bans.bansItemId
FROM item, bans
WHERE item.itemId = bans.bansItemId
GROUP BY bans.bansItemId
ORDER BY item.itemId DESC
LIMIT 0,5

But it's not limiting the output...at all.

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


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