Get number of rows in MySQL [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest way to count exact number of rows in a very large table?
What query do I use to get the number of rows that share a common specification.
Example: The number of rows where idpost = 3

You can use COUNT() documented here.
SELECT COUNT(1) FROM posts WHERE idpost = 3
EDIT: Updated according to dbf's suggestion.. Make sure you distinguish between COUNT(*) and COUNT(1), discussed here.

The query looks like this:
SELECT count(*) FROM some_table WHERE (conditions);
In your example:
SELECT count(*) FROM some_table WHERE idpost=3;
More on counting rows in MySQL: MySQL 5.6 Reference Manual: 3.3.4.8. Counting Rows
EDIT:
If you were wondering, which way of counting all rows is better (count(*) or count(1)), see this: What is better in MYSQL count(*) or count(1)?.

Try
SELECT
COUNT(*) as NumberRows
FROM
your_table_name_here
WHERE
idpost = 3
;

Consider learning SQL:
select count(*) from mytable where idpost=3

the select count(*) function grabs the number of rows that meet a certain criteria - define that in the where statement.
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html - read here

Related

Selecting most recent row from a table [duplicate]

This question already has answers here:
Select last row in MySQL
(11 answers)
Closed last year.
id
first_die
24
2
25
6
How do i return the most recent first_die in this set? I want to display the latest roll in my select query. I know returning the latest first_die will be based on id but I'm having difficulty doing this because it is a simple fix, but I'm looking at not so simple answers to the problem. Any help would be nice. Thank you.
Use:
select id,first_die
from test_tbl
order by id desc limit 1 ;
Demo:
Or:
select id,first_die
from test_tbl
where id = (select max(id) from test_tbl);
Demo:
If your most recent id is the maximum number then you would use SELECT MAX(id) query (sql query).
If you want to return the entire recordset or row use the code below
SELECT * FROM tablename WHERE id=(SELECT MAX(id) FROM tablename);

How to check if limit was used in mysql? [duplicate]

This question already has answers here:
Find total number of results in mySQL query with offset+limit
(7 answers)
Closed 7 years ago.
In mysql query, I have something like I select and order, and use where clause, etc..., and finally I have limit 10 for example. Something like this:
Select *
From employee
where fired=0
limit 10
The problem is, how do I know if limit was used and the result was truncated? Basically I want to know easily if there were more results that got cut off from limit. I don't want to do something like
Select (count *)
From employee
where fired=0
subtract
Select (count *)
From employee
where fired=0
limit 10
Is there a simple way for this? Perhaps, like you know how after you run an insert statement, you can get the id using php built in function get_sql_id() (something like that), is there a sql_count_without_limit()?
Thanks
You could do
Select *
From employee
where fired=0
limit 10+1
So that:
0-10 values returned: no limit was reached
11 values returned: Limit was reached
And then only use the first 10 values that are returned.
update Raymond's answer is waaaaay better than mine!
You can use the found_rows function like this:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
For more information see mysql reference
You can return the remaining rows as an column like below
SET #limit = 10;
SELECT *,
(SELECT count(*) from employee)- #limit as remaining
From employee
where fired=0
limit 10

Is there have the best way to count all rows in table mysql? [duplicate]

This question already has answers here:
MySQL: Fastest way to count number of rows
(14 answers)
Closed 8 years ago.
I want to count all of rows in table that match to my condition as fast as mysql can do.
So, i have four SQL and want you to explain all of them, how is it different for each SQL?
and which is fastest or best for query times and server performance? or it has another way that can better than these. Thank you.
select count(*) as total from table where my_condition
select count(1) as total from table where my_condition
select count(primary_key) as total from table where my_condition
or
select * from table where my_condition
//and then use mysql_num_rows()
First of all don't even think about doing the last one! It literally means select all the data I have in the database, return it to the client and the client will count them!
Second, you need to understand that if you're counting by column name, count will not return null values, for example: select count(column1) from table_name; if there is a row where column1 is null, it will not count it, so be careful.
select count(*), select count(1), select count(primary_key) are all equal and you'll see no difference whatsoever.

limit vs exists vs count(*) vs count(id) in MySQL [duplicate]

This question already has answers here:
Is EXISTS more efficient than COUNT(*)>0?
(5 answers)
Closed 1 year ago.
I just want to know which one is the fastest.
What I'm trying to do is to just check if the data is existing on the table.
I've been using "LIMIT" most of the time but in your opinion or if you have basis, which one is the fastest to check if data is existing.
Example:
limit 1:
SELECT ID
FROM TABLE
WHERE ID=1 LIMIT 1;
exists:
SELECT EXISTS(
SELECT *
FROM TABLE
WHERE ID=1);
count(*):
SELECT (*)
FROM TABLE;
count(ID):
SELECT (ID)
FROM TABLE;"
Additional: I'm using InnoDB.
Limit is always the fastest, because it iterate one line of the table.
Exists has little difference with Limit because you just add another select statement, we can say it has the same efficiency as the first one.
Count will iterate all the table and count the result. When you use count(), by default, mysql count the primary key of the table. I've done some tests of count(id), count(), count(field) and count(1) in big table, there is no big difference. In my opinion, 'count' will always try to count the index unless the field you count is not an index, but many people said that we should use count(id) rather than use count(*).
In a small table, the four ways all work fine. But if you join with some big table, count will take a very very long time.
So in all, the time used is count(*) > count(id) >> exists > limit
I think they are all fine; except I would remove the WHERE ID = 1 clauses. If you ever clear your table and start re-inserting then ID 1 will not exist. Just LIMIT 1 will do the trick. Personally I don't favour the exists and count(*) options. I would prefer count(ID) then, as you would normally have an index on ID so I would expect that to run fairly quickly. To be sure, you would have to time them (on a really big table) - I expect them to come out something like exists, limit 1, count(id), count(*) from fastest to slowest. (I am in doubt about the exists though - if it actually evaluates the whole SELECT * it may come out worst).

PHP MYSQL - Count & Average In Same Query

This may be very simple but I am trying to do a query that will return the average of the results but I also want it to count the number of rows it used to get the average. For example:
Row 1 = 5
Row 2 = 2
Row 3 = 9
Row 4 = 1
Average = 4.25
Count = 4
Is there a way to do this with one query apposed to two. When i used the avg function is always just returns one row so my count is 1 instead of 4. I need a query that will average but also tell me how many records it went through.
I am trying to avoid using two queries. Thank you for your help.
This is pretty basic and should have been discoverable via search.
SELECT COUNT(field) as `count`, AVG(field) as `average` FROM table_name
In the terms you have stated - if you aren't GROUPing or things like that - you'd just write
SELECT COUNT(col) AS cnt, AVG(col) AS avg FROM tbl;
and you ought to have no problems. You get one row, with the fields cnt and col containing what you need.
The problem you're having is probably due to the use of mysql_num_rows to get the count, which is not correct.
forgot to add: or to the fact that you did not supplied your whole problem.
You can use multiple aggregate functions in a single query:
SELECT COUNT(*), AVG(field) FROM mytable
SELECT COUNT(*), AVG(YourColumn)
FROM YourTable

Categories