SELECT FOUND_ROWS() not working or return 1 i dont know where i do mistake
$qry ="SELECT SQL_CALC_FOUND_ROWS DISTINCT user_id, login_date
FROM login_members
WHERE (login_date BETWEEN '2012-02-13 00:00:00' AND '2013-02-13 23:59:59')
LIMIT 0, 10";
$rs = mysql_query($qry);
$total_records = mysql_result(mysql_query("SELECT FOUND_ROWS()"),0,0);
The accepted answer seems to be wrong. as it suggest an use of found_rows() after a query with LIMIT in it.
ANSWER: The question is about the RETURN 1 and in some case 0 as a result of found_rows();
it happens when some other query is exectued after your first select statment.
especially when you are using an IDE or some client to run your queries, some additiona 'preparational' queries are executed before and after your query.
and found_rows() will return the numer of result returned by the LAST query run on the server. which in this case is not the one we are expecting.
and hence the RETURN 1 or 0 error.
Verification:
You can verify this by enabling general logging on your server, and executing the queries.
you will see couple of additonal queries exectued between 'the first query and the found row query'.
FIX
stored procedure, or the ver old COUNT(*).
performance wise there is hardly any difference.
ADDITIONAL
if your object is to find the total number of rows returned then it's fine.
and the use of SQL_CALC_FOUND_ROWS becomes immaterial.
here is a general rule, LIMIT is not required for found rows, nor SQL_CALC_FOUND_ROWS.
But three of them can be used together to give a very useful result.
ie on running something like.
SELECT SQL_CALC_FOUND_ROWS * from some_table_name LIMIT 0,10;
SELECT FOUND_ROWS();
We will get the number of rows that would have been returned by the query had we run
SELECT * from sometable_name;
ie: without the LIMIT.
having said that,
SELECT * from some_table_name LIMIT 0, 10;
SELECT FOUND_ROWS();
would give us the total number of results which cos of the limit will be <=10. and doesn't server any practical purpose, but it's NOT an error.
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.
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
use this query
$qry ="SELECT DISTINCT user_id, login_date
FROM login_members
WHERE (login_date BETWEEN '2012-02-13 00:00:00' AND '2013-02-13 23:59:59')
LIMIT 0, 10";
if the preceding SELECT contain SQL_CALC_FOUND_ROWS, but if the preceding SELECT doesn't contain SQL_CALC_FOUND_ROWS, FOUND_ROWS() return the number of rows returned by this preceding SELECT
should be :
$qry ="SELECT DISTINCT user_id, login_date FROM login_members
WHERE (login_date BETWEEN '2012-02-13 00:00:00' AND '2013-02-13 23:59:59')
LIMIT 0, 10";
$rs = mysql_query($qry);
$total_records = mysql_result(mysql_query("SELECT FOUND_ROWS()"),0,0);
echo $total_records // display total record count
There is no need to use SQL_CALC_FOUND_ROWS
For total records use simple:
$total_records = array_shift(mysql_fetch_row(mysql_query('SELECT FOUND_ROWS()')));
You have to set mysql.trace_mode to off.
Use this is in each php page.
ini_set("mysql.trace_mode", "0");
or you can set this on .htaccess file
php_value mysql.trace_mode "0"
Here:
<?php
ini_set("mysql.trace_mode", "0");
$qry ="SELECT SQL_CALC_FOUND_ROWS DISTINCT user_id, login_date
FROM login_members
WHERE (login_date BETWEEN '2012-02-13 00:00:00' AND '2013-02-13 23:59:59')
LIMIT 0, 10";
$rs = mysql_query($qry);
$total_records = mysql_result(mysql_query("SELECT FOUND_ROWS()"),0,0);
echo $total_records;
?>
SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again. An example is a Web script that presents a paged display containing links to the pages that show other sections of a search result. Using FOUND_ROWS() allows you to determine how many other pages are needed for the rest of the result.
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.
In the absence of the SQL_CALC_FOUND_ROWS option in the most recent successful
SELECT statement, FOUND_ROWS() returns the number of rows in the result set
returned by that statement. If the statement includes a LIMIT clause, FOUND_ROWS()
returns the number of rows up to the limit. For example, FOUND_ROWS() returns 10
or 60, respectively, if the statement includes LIMIT 10 or LIMIT 50, 10.
$qry ="SELECT SQL_CAL_FOUND_ROWS *
FROM login_members
WHERE (login_date BETWEEN '2012-02-13 00:00:00' AND '2013-02-13 23:
59:59')
LIMIT 0, 10";
$rs = mysql_query($qry);
$total_records = mysql_query("SELECT FOUND_ROWS() as `found_rows`;");
echo $total_records // display total record count
Related
I must execute the query below in PHP with SQLServer, however I can not use the missing LIMIT clause in Microsoft queries.
$SqlTabelaAtual="SELECT *
FROM BusinessCadTabPreco
RIGHT JOIN BusinessCadTabPrecoItem ON BusinessCadTabPreco.CdTabela = BusinessCadTabPrecoItem.CdTabela
WHERE BusinessCadTabPreco.CdEmpresa =01
AND CdProduto =".$row['CdProduto']."
ORDER BY BusinessCadTabPreco.DtSincronizar DESC LIMIT 1
Use this code, in SQLServer the keyword Limit is TOP
$SqlTabelaAtual="SELECT TOP 1 *
FROM BusinessCadTabPreco
RIGHT JOIN BusinessCadTabPrecoItem ON BusinessCadTabPreco.CdTabela = BusinessCadTabPrecoItem.CdTabela
WHERE BusinessCadTabPreco.CdEmpresa =01
AND CdProduto =".$row['CdProduto']."
ORDER BY BusinessCadTabPreco.DtSincronizar DESC"
You can limit the records returned by adding the TOP expression to your SELECT clause.
Usage
/* Returns the first 10 records.
* Combine with an ORDER BY if you want control over the records returned.
*/
SELECT TOP 10
Id
FROM
TableName
;
Or
-- Percent return.
SELECT TOP 10 PERCENT
Id
FROM
TableName
;
I have a database table 'sales_list'. In this is rows of sales records attributed to a users_sales_guild_id. I'd like to query the table and order results by the number of sales made by each user, highest to lowest.
I thought this query would do it, but alas no...
$total_query = "SELECT *, COUNT(users_sales_guild_id) AS users_sales_guild_id_count FROM sales_list WHERE sales_entry_date BETWEEN '2013-10-01 00:00:00' AND '2014-11-30 23:59:59' ORDER BY users_sales_guild_id_count DESC";
$total_rs = mysql_query($total_query) or trigger_error ("Query: $total_query\n<br>MySQL Error: " .#mysql_error()); // Run the query.
$num_rs = mysql_num_rows($total_rs);
This query returns 1 record. rather than a selection of records ordered by the number of sales by each user.
Your assistance is much welcomed.
count(*) will return one row unless there is a group by clause, so the query should be as
SELECT *,
COUNT(*) AS users_sales_guild_id_count
FROM sales_list
WHERE sales_entry_date BETWEEN '2013-10-01 00:00:00' AND '2014-11-30 23:59:59'
group by users_sales_guild_id
ORDER BY users_sales_guild_id_count DESC
UPDATE : Its better to select col1,col2 ..... instead of * while doing group by - Point raised by InoSHeo
check this link
http://sqlfiddle.com/#!2/1201d/6
check this link if you would like to get details based on username http://sqlfiddle.com/#!2/1201d/4 here i have used type instead you can use username
I have a table containing rows with timestamped.
Normally if I want to get the latest 20 rows out according to the time. I use:
$sql = "SELECT *
FROM comment
ORDER BY time DESC
LIMIT 20";
But now, I want to get the latest comments AFTER the latest 20 rows and LIMIT to 10. That means rows 21-30.(of course , everything is according to timestamp)
How can I do that using MySQL?
MySQL has a built-in offset that you can use with LIMIT:
$sql = "SELECT * FROM comment ORDER BY time DESC LIMIT 10, 20";
Also, refer to this SO post: MySQL LIMIT/OFFSET: get all records except the first X
$sql = "SELECT * FROM comment ORDER BY time DESC LIMIT 20, 10";
Hope it will select from 21 to 30 records
sql = "SELECT * FROM comment ORDER BY ID DESC LIMIT 20, 10";
Try a mixture of limits
$sql = "select * from (SELECT * FROM comment ORDER BY time DESC LIMIT 30) as A order by time ASC limit 10";
The mysql built in offset method others have posted looks better though.
There are two options:
Get 30 rows and use PHP to split the result set into a group of 20
and a group of 10.
Send two queries, one for 20 and one for 10 rows.
I tried using distinct as well and it returns duplicates.
$cubes = mysql_query("SELECT distinct * FROM posts ORDER BY RAND() $limit ") or die(mysql_error());
I just want to take my posts table... and return it in a random order without duplicates.
Select only the distinct id's you need, e.g.
SELECT distinct id FROM posts ORDER BY RAND() $limit
Distinct works over 'all' rows you select, so if you (for example) have a unique timestamp field, chances are you'll return every single row.
Are you sure that you want to execute a SELECT DISTINCT * FROM ... and not just a SELECT DISTINCT column_name FROM ... ?
See the SQL DISTINCT STATEMENT doc for more infos.
I want to return only the last 15 rows in my table, then the 15 before that.
Unfortunately while($rows = mysql_fetch_assoc($result)) where the query is SELECT * FROM table returns the data in all rows.
I thought about doing something like:
In my insert script
SELECT * FROM table then $selection_id = mysql_num_rows($result)-14 before inserting any data, then adding column named selection_id which would contain $selection_id, thus each set of 15 rows would have the same selection_id.
In my select script
SELECT * FROM table then $num_rows = mysql_num_rows($result)/15 then SELECT * FROM table WHERE selection_id='$num_rows' and SELECT * FROM table WHERE selection_id='$num_rows-1'.
I could then perform while(..) on both results as usual.
However, I'm not sure this is the most efficient way (chances are it's not), so if not, I'd really appreciate some suggestions to cut down the amount of code I'll have to use :)!!
Use a LIMIT clause in your query, order by your auto-incrementing primary key in descending order. E.g.
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 0,15
...will get the last 15 rows, and:
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 15,15
...will get the 15 rows before that.
Selecting the last 15 rows:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 0,15
Selecting the 15 rows before the previous ones:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 15,15
And you can continue in a while cycle.
You need to check out mysql LIMIT. To get the last 15, you'd need to know the number of total rows.
$offset=$rowcount-15;
$sql="SELECT * FROM mytable LIMIT $offset,15";
This is just for example, you'd want to make sure there are at least 15 rows, I'm not sure how mysql would deal with a negative offset. I'll let you figure out how to count the rows.
Edit:
Oh, haha, you could also just sort it descending, that will save you having to query twice.
SELECT * FROM mytable ORDER BY id DESC LIMIT 15;