Sql count without duplication in statment - mysql

i have an sql query that selects a bunch of data. I would also like to get the number of records selected by the query (before i limit it). All the examples i have seen of the count statment duplicated the select. My select statment is about 50 lines long and i would rarther not duplicate it.
Thanks

Your question would be easier to answer if you could give us an example SQL statement, however, from what you have said so far, the following should be correct:
Select Columns, Count(Distinct Value) From Table Where x=y Group By Columns

Yes.
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

It isn't really possible to get the number of rows that a query would return without running it or a version of it.
There's sql_calc_found_rows which will let you put a limit clause on the statement and return the total number of rows it would have found had there not been a limit clause in a subsequent call to found_rows(), but it's expensive.

Thanks everyone, i was just trying out sql_calc_found_rows, and your dam right Nick, it is expensive. I think ill just create a separte query, thanks

Related

MySQL : Count returning double the number of entries when using distinct

So I do a count like so
select distinct count(prod.id) from product as prod....
I get back 175590
I do a select like so
select distinct prod.id from product as prod.... (rest of the query is exactly the same)
and I limit it. Now if I limit the query to return anything over the half way point it returns nothing. It appears as if count is returning double the number of entries each time.
Does anyone know of anything that may be causing this?
Thanks
Tracey
The DISTINCT keyword tells MySQL to strip the duplicate rows from the result set. Because SELECT COUNT(prod.id) returns a single row (I guess this, I cannot tell for sure until I see the complete query), adding DISTINCT in front of COUNT() does not change its behaviour in any way.
What you probably want is SELECT COUNT(DISTINCT prod.id) and that's a totally different thing. It removes the duplicate values of prod.id before counting them.
Your first query is counting how many prod.id's there are.
Your second query is showing all distinct prod.id's.
This is quite different.
If you were to do the second query without the distinct key word the number would be the same.

How to determine the number of rows returned from MYSQL select

I want to add a progress bar to my program.
Is it possible to read the number of rows that a select command is going to return before the data is sent?
No. You would have to do something like this before
select count(*) from your_table where ...
and afterwards the real query
select col1, col2 from your_table where ...
The reason is that the DB engine does not know how many records it will return until it applies the query and searches the data.
There is no way to determine how long a statement will take to process. If you want to know how many datasets a statement will return you will need to execute the statement to know.
I think Only way you can do is fire two queries
one with
select count(*) .....
and the other one is with
select * from ....
Select count(*) from `table` where <condition>.
MySql wouldn't know what to count if you don't specify what you're looking for.

MySQL / PHP query performance?

Im in a dilema on which one of these methods are most efficient.
Suppose you have a query joining multiple tables and querying thousand of records. Than, you gotta get the total to paginate throughout all these results.
Is it faster to?
1) Do a complete select (suppose you have to select 50's columns), count the rows and than run another query with limits? (Will the MySQL cache help this case already selecting all the columns you need on the first query used to count?)
2) First do the query using COUNT function and than do the query to select the results you need.
3) Instead of using MySQL COUNT function, do the query selecting the ID's for example and use the PHP function mysql_num_rows?
I think the number 2 is the best option, using MySQL built in COUNT function, but I know MySQL uses cache, so, selecting all the results on first query gonna be faster?
Thanks,
Have a look at Found_Rows()
A SELECT statement may include a LIMIT clause to restrict the number
of rows the server returns to the client. In some cases, it is
desirable to know how many rows the statement would have returned
without the LIMIT, but without running the statement again. To obtain
this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT
statement, and then invoke FOUND_ROWS() afterward:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
The second SELECT returns a number indicating how many rows the first SELECT`
would have returned had it been written without the LIMIT clause.
My guess is number 2, but the truth is that it will depend entirely on data size, tables, indexing, MySql version etc.
The only way of finding the answer to this is to try each one and measure how long they take. But like I say, my hunch would be number 2.

Getting count from complicated mysql statement

I'm trying to get the count of unique questions from the following mysql statement but every time I try to add count(q.id) as questionCount the statement only returns one result. I'm obviously doing something wrong but I can't figure out what it is.
http://www.sqlfiddle.com/#!2/34906/58
Hope somebody can help.
Steve
Just edit 2nd line of your query to this one:
select
count(distinct FinalQA.QUESTION_ID) from.....
It appears you want the total questions "stamped" on every row... for example you are auto-generating a test and want it to show "Out of 5 questions" in the output. To simplify this, since you KNOW you want 5 questions via your WHERE clause, I would slightly adjust it to...
select
FinalQA.*
from
( select
5 as TotalQuestionsOffered,
QWithAllAnswers.*,
... rest of query ) FinalQA
where
FinalQA.ARankSeq <= FinalQA.TotalQuestionsOffered

get total for limit in mysql using same query?

I am making a pagination method, what i did was:
First query will count all results and the second query will do the normal select with LIMIT
Is there technically any way to do this what I've done, but with only one query?
What I have now:
SELECT count(*) from table
SELECT * FROM table LIMIT 0,10
No one really mentions this, but the correct way of using the SQL_CALC_FOUND_ROWS technique is like this:
Perform your query: SELECT SQL_CALC_FOUND_ROWS * FROM `table` LIMIT 0, 10
Then run this query directly afterwards: SELECT FOUND_ROWS(). The result of this query contains the full count of the previous query, i.e. as if you hadn't used the LIMIT clause. This second query is instantly fast, because the result has already been cached.
You can do it with a subquery :
select
*,
(select count(*) from mytable) as total
from mytable LIMIT 0,10
But I don't think this has any kind of advantage.
edit: Like Ilya said, the total count and the rows have a totally different meaning, there's no real point in wanting to retrieve these data in the same query. I'll stick with the two queries. I just gave this answer for showing that this is possible, not that it is a good idea.
While i've seen some bad approaches to this, when i have looked into this previously there were two commonly accepted solutions:
Running your query and then running the same query with a count as you have done in your question.
Run your query and then run it again with the SQL_CALC_FOUND_ROWS keyword.
eg. SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT 0,10
This second approach is how phpMyAdmin does it.
You can run the first query and then the second query, and so you'll get both the count and the first results.
A query returns a set of records. The count is definitely not one of the records a "SELECT *" query can return, because there is only one count for the entire result set.
Anyway, you didn't say what programming language you run these SQL queries from and what interface you're using. Maybe this option exists in the interface.
SELECT SQL_CALC_FOUND_ROWS
your query here
Limit ...
Without running any other queries or destroying the session then run
SELECT FOUND_ROWS();
And you will get the total count of rows
The problem with 2 queries is consistency. In the time between the 2 queries the data may be changed. If your logic depends on what is counted has to be returned then your only approach would be to use SQL_CALC_FOUND_ROWS.
As of Mysql 8.0.17 SQL_CALC_FOUND_ROWS and FOUND_ROWS() will be deprecated. I don't know of a consistent solution after this functionality has been removed.