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




Counting Number Of Rows Returned


I want to count the number of rows returned when I execute an SQL query. At the moment I am selecting them all and cycling through them:

$count=0;
$query = "SELECT * FROM swe_picgal WHERE cat="".$cat.""";
if ($mysql_result = mysql_query($query, $linkdb)) {
if (ExecuteQuery($linkdb, $result, $query)) {
while ($row = NextRow($result)) {
$count++;
}}}
print('Number of rows: '.$count.'');

Surely there is an easier way? I assumer mySQL will have a built in counting function?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Count Number Of Rows Returned?
this is my connect and everything code:

<?php // connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT DISTINCT category FROM $tuttable";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo ($row['category']." | ");
}
echo count($row);
?>

Notice "echo count($row);" How do i get this to work properly. It only returns one, because there is only 1 that it is working on. I want to return the count of all the rows that fit that query.

Unions -- Number Of Rows Returned
I have this query:

SELECTcount(*)
FROMpa_album
WHEREcreator_id = 1001
UNION
SELECTcount(*)
FROMmb_post_tbl
WHEREauthor_id = 1001
UNION
SELECTcount(*)
FROMmb_post_tbl
WHEREupdated_person_id = 1001
UNION
SELECTcount(*)
FROMmb_thread_tbl
WHEREauthor_id = 1001
UNION
SELECTcount(*)
FROMevent_tbl
WHEREcreator_id = 1001


Which checks for a specific value in the foreign keys of a few tables. I would expect to see 5 rows in the results of this query, but I do not get 5-- I get a row for each SELECT that has a count > 0, but then I get only one row returned for all tables that have a count = 0. So if there is only one table that has a count > 0, then I get two rows-- value 0, and value 1.

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?

Counting Number Of Rows
Can anyone come up with a novel way of counting the number of rows in a table that contain no NULL values in the columns that each row contains?
Each row contains 200+ columns, so doing a "count" on each column, and then selecting the smallest value is not very practical.

Counting Number Of Valid Rows
We have many kinds of advertisers in our web service:
1.private
2. companies, of which some are members of local enterpreneur community
3. non-profitable communities

Company advertisers can select also a "line of business" while for others
its unavailable. We have a menu where we want to put all the lines of
business in alphabetical order WITH the information, how many companies
(that are ALSO members of local enterpreneur community) are in that line of
business. I made the query following some model. It looked like working,
but now I noticed it doesn't right results always:

SELECT business_lines.*,
IF(seller_id <> 'NULL' AND seller_type='company' AND
seller_membership="LocalEnt", count( * ), 0) as nr_of sellers_in_this_line
FROM business_lines
LEFT OUTER JOIN sellers ON line_id=seller_line_id
GROUP BY line_id
ORDER BY line_name asc

So I suspect that if not ALL companies int certain line are members, that
line results to enpty line. Is that true and how to fix this?

Counting Number Of Valid Rows
We have many kinds of advertisers in our web service:
1.private
2. companies, of which some are members of local enterpreneur community
3. non-profitable communities

Company advertisers can select also a "line of business" while for others
its unavailable. We have a menu where we want to put all the lines of
business in alphabetical order WITH the information, how many companies
(that are ALSO members of local enterpreneur community) are in that line of
business. I made the query following some model. It looked like working,
but now I noticed it doesn't right results always:

SELECT business_lines.*,
IF(seller_id <> 'NULL' AND seller_type='company' AND
seller_membership="LocalEnt", count( * ), 0) as nr_of sellers_in_this_line
FROM business_lines
LEFT OUTER JOIN sellers ON line_id=seller_line_id
GROUP BY line_id
ORDER BY line_name asc

So I suspect that if not ALL companies int certain line are members, that
line results to enpty line. Is that true and how to fix this?

Zero Rows Or One Rows Returned, Same Data And Same Query
I have a query that produces a single row (as I expect) when I run it from the mysql client (mysql 4.0.18-Max/linux, also 5.0.19-standard/OSX-intel), or from sqlgrinder (osx, uses jdbc).

When I run it inside my application (a Java app connecting via jdbc), I get zero rows from this query.

I tried it under phpmyadmin, and once again I get zero rows.

Why do I get inconsistent results? Here's the query:

Limiting The Number Of Letters Returned
I wrote some code to return the last 5 posts from my messageboard. I want to return only 25 characters per post. Here's my code:

Restricting Number Of Characters Returned
is there a way to restrict the number of characters that are returned for a column. if I have a column and there are 400 characters for a particular record, and all I want to return is 50.

Rows Returned
EDIT:I resolved the problem...I was pushing these into an array...and whichever order I wanted, I just had to initialize it in that order.
How does mysql determine the order in which rows are returned when no order is given...for ex "select * from table where (id = '5' OR id = '6')?
I am extremely frustrated on why the same query on 2 different scripts returns rows in a diff order.

How To Get Count Of Rows Returned While Using Group & Having?
i'm using the having keyword to find certain set of rows and they are working properly.

but what i want is , i want to count the total number of rows returned by this query using mysql.

select name,count(date) from emp group by date having count(date)>3;
this returned returns all the names that have more that 3 entries in the same date. it returned 5 rows and how can i get the count of rows(5) the query returned .

Limiting Amount Of Returned Rows?
How do I limit the amount of returned rows?

Lets say, that I want only the latest 400 rows (of e.g. 4034634 rows) -
is there a parameter for that like rownum in Oracle?

e.g. select * from someplace where rownum < 400 order by sometimefield?

MySQL Browser - Rows Returned In
Can anybody tell me what the "rows returned in" numbers at the bottom left of the results window signify, especially the one in ()? I have searched the docs to no avail!

Limiting # Of Rows Returned Per Page...
I'm building a db in MySQL using PHP, and I need to limit the number of results per page returned in a query. Note, I don't want to limit the query, just the number of results shown per page. So, if 100 rows are returned, I want to show 20 per page, and have the others linked at the bottom, like:

Page: 1 2 3 4 5 etc.

I did a search of the forum, but only found references to the LIMIT clause. This doesn't seem to be what I need, as I don't want to limit the number of rows returned in a query, just how the results are display. So I'm not sure if this would be done with SQL, or PHP.

Counting The Number
I'm having difficulty figuring out how count the number of each distinct value in a column.Say I have a column called 'score' that contains a score value of 1-4, how would I could how many 1s, 2s, 3s and 4s there were in this column?

Limiting Rows Returned Based On Count
say if I have 3000 records in my db, and I want to get rows 200~300 when orderd by a specific field in this table, is there a easy way to do it?

I have something like:

select * from table_name order by ID desc limit ....

but I am not sure if this syntax allows me to do what I am trying to do .

Counting The Number Of Foreign Keys
I have table A,B and C. How would i go on about counting the a_id from table C?

I only want to count the UNIQUE a_id and not the repeated ones.

Example:

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?

Counting Rows
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/itsyouma/public_html/staff/memberlist.php on line 6

<?php
$SQL_statement = "SELECT * FROM users";
$resultset = mysql_query($SQL_statement);
$count_rows = mysql_num_rows($resultset);
?>

Counting Rows
I have a table containing

Article | State
-------------------
1 | 10
2 | 10
1 | 5
1 | 5

and I want to count all Articles in one row with the different states

SELECT Article, COUNT(State=10) AS Ordered, COUNT(State=5) AS Free FROM...... GROUP....

but this SQL doesn't work (on my Server 4.0.20) I don't know if it work above but in the docu I can read somthing COUNT(expr) my be it works in 4.1

but is there another way that works wit 4.0.20?

Counting Rows
both of these return the same error. "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource"

$checkip = "SELECT * FROM 'vote' WHERE ip='$user_ip', vote_id='$vote_id'";
$ipcheck=mysql_query($checkip);
$total=mysql_num_rows($ipcheck);

$checkemail = "SELECT * FROM 'vote' WHERE email='$email', vote_id='$vote_id'";
$emailcheck=mysql_query($checkemail);
$total2=mysql_num_rows($emailcheck);

Counting Rows
In my MYISAM table I have an index (Index_A) on 2 fields (Field_A,
Field_B). There are millions of rows in the table. The cardinality of
Index_A is 53. I think a query to count the number of rows that match
a pair of values for Field_A and Field_B should execute blindingly fast
(in the index count the number of leaf nodes for the B-Tree node
representing the indexed pair of values).

My query looks like:
SELECT count(*) FROM `table_A` WHERE `Field_A`=5 and `Field_B`=1

Why does it take 20 seconds for this query? And how can it be made
faster?

Counting Rows
I have a value '120' standing in column 'number' from a table called 'example'.

Let's say I want to know how many records have been before this 120..how should I 'count' that?

Exampledatabase 'example':
----------------
|Number| Word |
----------------
|13 | this|
|22 | is|
|120 | an |
|422 | example|
----------------
In this example my return should be 2(the 13 and 22 are the records counted).

Counting Rows
I have an sql table (messages) that holds messages related to conversation names. I need help to formulate a query that extracts the number of unviewed messages for each distinct conversation.
For example, with the following 4 tuples:

cname viewed

weather viewed
weather unviewed
mobiles unviewed
weather unviewed

I would need the output as:
You have 2 unviewed messages in weather and 1 unviewed message in mobiles

The Absolute Best Way Of Counting Rows
I've written a pagination class in PHP that basically gets a query, and returns, let's say, the first 50 results, then prints a navigation bar with links to the pages (1,2,3 etc)

For that class, I need:

1- to know the total results from the query
2- to run a query with "limit 50"

Up to now I have been using mysql_num_rows($query) to count the number of rows and then running the "LIMITed" query, but to me this seems a waste of resources
Isn't there a better alternative for counting rows? I've tried using COUNT(*) but the script receives some really complex queries (sometimes using 3 tables) and sometimes returns "1" as the total number of rows.
I can't find a way to "standardize" it in order to let me feed it whatever query I want

What I mean is, I need a fool-proof way to feed it ANY query, and it will return me the number of rows

Counting Rows More Efficiently
I have a table with around 1-2 million comments. A comment is a little text message (255 characters max) that someone can add to someone's profile.

When a profile is loaded, the 10 latest comments are pulled from the database and shown. Below the 10 comments is a link to the rest of the comments and tells how many more comments there are.

Counting the number of comments is slow, too slow to be doing everytime a profile is loaded. The columns have an index and the keys are packed.

How could this be done more efficiently?

Counting Rows From Other Table
SELECT one.this, COUNT(two.this) FROM one, two WHERE (two.this = one.this)The problem with this query is that it won't return rows from table "one" if they don't match with at least one element on table "two". How to proceed then with just one query?

Counting Rows And Grouping By Day
I have a table called 'msgs' which stores the text of log entries, together with a timestamp of when the message was generated:

CREATE TABLE `msgs` (
`time_num` int(10) unsigned NOT NULL default '0',
`message` varchar(255) default ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I want to be able to see how many log entries were generated on particular days of the week for the past seven days: Code:

Counting Rows In A Table
Anyone know how to count rows in a table depending on 2 columns, and
different values within the second column.

Basically i am trying to obtain the resultset example below from the table
example below

TABLE example:

Date | Status
-------------------------
2003/07/01 | 1
2003/07/04 | 1
2003/07/06 | 1
2003/07/24 | 2
2003/07/24 | 2
2003/07/24 | 1

RESULTSET required:

Date | Status = 1 | Status = 2
-----------------------------------------------
2003/07/01 | 1 |
2003/07/04 | 1 |
2003/07/06 | 1 |
2003/07/24 | 1 | 2

GROUP BY And Counting Rows
SELECT id,count(*) as c FROM table WHERE id='$_GET[id]' GROUP BY id
The following produces:

1009 8
1010 9
1011 11

"c" is coming back with 8, but that's not what I want. I want the total number of rows returned which in this case would be 3. How can I restructure this query to return the total number of rows while still allowing me to GROUP BY?
Is this even possible? How should I proceed?

Counting Rows In One Table That Match Something In Another
I have 2 tables, events and photos

The photos table contains references to uploaded photos as well as the event id that those files refer to.

I am writing some code to output a list of events and want to say whether or not there are any photos associated with that event.

I have a query like this
CODESELECT e.id,e.eventName,e.eventLocation,e.publish FROM shareEvents e,sharePhotos p ORDER BY id DESC

Multiple Rows Returned Instead Of One (was "why This Is Happening?")

I have this select statement

PHP

$select="select video.id, title, description from video, category,

maincategory where maincategory.mainid='$selectcategory' AND

maincategory.mainid=category.mainid and

video.categoryid=category.categoryid and video.title like '%$searchterm%'

or video.description like '%$searchterm%' order by dateadded DESC";

if i put car in the searchterm which is the name of a video.title everything works fine and the result is shown only once... whereas if i put blabla in the searchterm which is included in the description of video.description two rows r returned for the same result instead of one.... why???


Get Number Of Rows Above A Row
I have one table with a some user info, and a column with the users points. I would like to fetch the user data and sonehow calculate in which place that user is. Basically, the number of users that has more points than the user.

An exampel of the table and data:


+----+---------+--------+
| id | name | points |
+----+---------+--------+
| 1 | chris | 1000 |
| 2 | john | 900 |
| 3 | jeff | 800 |
| 4 | jay | 700 |
| 5 | michael | 600 |
| 6 | eric | 500 |
| 7 | stuart | 400 |
+----+---------+--------+

And an example of the data set I would like:

+----+---------+--------+----------+
| id | name | points | position |
+----+---------+--------+----------+
| 1 | chris | 1000 | 1 |
| 3 | jeff | 800 | 3 |
| 6 | eric | 500 | 6 |
+----+---------+--------+----------+

I'm not sure how to do this in one query, and I'm not even sure that it's possible, but if it is I sure would like to know how.

I'm using MySQL 4.1.16.

Getting The Number Of Rows.
How could I get mysql to just print the number of rows for this query, because this is currently return hundreds of rows. I just want one row.

I want to know how many duplicate first names there is.

CODESELECT COUNT(*) as total FROM members WHERE first!='' GROUP BY first HAVING total>1

Number Of Rows
i have got a script that log all downloads (daily) in a database.
I would like to know how to query that database, to get the number of records per month.
my records look like this
date ip
2006-01-01 200.1.157.11
2006-01-01 192.168.10.3
2006-02-05 198.10.10.2
etc

needed output
01 2
02 1

Get Number Of Rows
I'm developing a hr system which will display Staff that are currently in the financial year(regardless of resigned or not resigned). Our financial year is from Oct - Sep (Oct 2006 - Sep 2007) For staff that have not resigned (@ the pt of query), the dateTerminated field is NULL.

I have a database that stores resignation date and startdate (both columns are date type). I tried with the following query but was not successful. PLease help.

Select * from (select name,dateTerminated,dept from user_info,hr_users where user_info.id=hr_users.sn
or dateTerminated is null
AND (year(dateTerminated)<=2007
AND year(dateTerminated)>=8)
)as table2 dunno how to continue ???
order by table2.name ASC

Number Of Rows
Is there a quick query (without just asking to query the entire database) that will return the number of rows in a given table?
I know I can do SELECT * FROM table but I don't really care about the data, I just want to know how many entries there are.

PHP - Can't Add Rows Past A Certain Id Number (127)
I've got a table which has about 70 rows in. The rows' ids start at 1, have a large gap in the middle, then range up to 127. If I try to add a new row with an autoindexed new id, I get the following MySQL error: duplicate entry '127' for key 1.

If I add a new row with a lower id, e.g. 6, there are no issues - it works fine. If I delete row 127, or give it a lower number, I can then add a new row as normal, but then the error happens again for all subsequent rows.

Everything was working fine with the page that adds new rows until it got to 127. Is this some kind of magic number I don't know about?

Calculate Number Of Rows
From the packet data returned from a query, where in the packet data are the
bytes signifying the number of rows returned?

Restricting The Number Of Rows
I wold like to restrict the number of rows in my table. Currently I am using the mySQL ver 4.1.12a-nt

I have alter the table using this statement,

ALTER TABLE `database1`.`tabley` MAX_ROWS = 7;

But whenever I insert the 8th row, it executed successfully.

Pls explain why and how should I go about restricting the number of rows?

Table: Max Number Of Rows
In a table what is the limit to the number of rows that make it up?
Is the only limitation HardDrive space?

Maximum Number Of Rows
I am looking for information on the total number rows that can be used in an InnoDB,if there is even such a limit.I am not a DBA, so the information on mysql is a bit cryptic.

Number Of Rows In Table
Is there a query which returns the number of rows in a table without using a "SELECT..." followed by recordset.RowCount?

Adding Certain Number Of Rows
I have a table:

Name | Amount
---------------
Moe | 30
Larry | 9
Curly | 12
Larry | 10
Moe | 7

How do I find the total amounts for each person and then output the name of the person with the greatest total? (which in this case is Moe with 37)

Retrieve Number Of Rows
I've got a large table with a lot of entries. only two fields, one's the key (which auto incs) and the other's a text field...if I want to just retrieve the number of rows in the table, what's the best way to do this?I know I could tell mysql to sort the table by the key row and then take the first one, but wouldn't this make it analyze the entire table (which would waste resources, especially considering that this table will have thousands and thousands of entries)? what's a better way to do it?

Number Of Table Rows
I have tried to answer this question by using Google but my search didn't bear any fruits, so to speak.

I am in the middle of designing my db and i have just thought that in some of my tables i think i will have 1000s of entries [rows] and if my web application with a db backend is successful it can turn into millions of rows [wishful] - so i wanted to know is there a limit in the number of rows for a table? I think not, but how will performance be effected?I am using a MySQL database, I am using a linux server not sure if this matters but microsft and i dont get along.

Actual Number Of Rows
I have a query in PHP with a LIMIT statement. Is there an easy way to get the actual number of rows (like a have no LIMIT in it)?

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.

JDBC Number Rows
After you have done a .executeQuery() and returned that to a ResultSet how do you get the number of rows returned?
I have tried using getFetchSize() but that does not help me.


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