Mysql Really get rows from result? - mysql

Good day.
For page navigation useally need use two query:
1) $res = mysql_query("SELECT * FROM Table");
-- query which get all count rows for make links on previous and next pages, example <- 2 3 4 5 6 ->)
2) $res = mysql_query("SELECT * FROM Table LIMIT 20, $num"); // where $num - count rows for page
Tell me please really use only one query to database for make links on previous and next pages ( <- 2 3 4 5 6 -> ) and output rows from page (sql with limit) ?
p.s.: i know that can use two query and SELECT * FROM Table LIMIT 20 - it not answer.

If you want to know how many rows would have been returned from a query while still using LIMIT you can use SQL_CALC_FOUND_ROWS and 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:
$res = mysql_query("SELECT SQL_CALC_FOUND_ROWS, * FROM Table");
$count_result = mysql_query("SELECT FOUND_ROWS() AS found_rows");
$rows = mysql_fetch_assoc($rows);
$total_rows = $rows['found_rows'];
This is still two queries (which is inevitable) but is lighter on the DB as it doesn't actually have to run your main query twice.

Many database APIs don't actually grab all the rows of the result set until you access them.
For example, using Python's built-in sqlite:
q = cursor.execute("SELECT * FROM somehwere")
row1 = q.fetchone()
row2 = q.fetchone()
Of course the library is free to prefetch unknown number of rows to improve performance.

Related

Joining three queries, passing results from one to next

I've looked at other questions, and I am not certain this is the same as previously asked questions.
Basically, I have three queries in succession. Query 1 results are passed to Query 2, and Query 2 results are passed to query 3, and write query 3 results to a file.
'select convert(char(10),max(paydate),101) from DATA.dbo.payment where status like 'PAID%'
'select distinct groupkey from custom.dbo.ediX091Header_abc where CheckIssue = '<results from query 1>' and status = 'Go'
'select max(GroupKey) from dbo.ediFuncGroup_abc' where GroupKey = '<results from query 2>'
Write any results from query 3 to an output file.
Thanks in advance
You don't need to run all three queries one after the other. You can pretty easily join the tables from the second and third queries. Then use the first query as a subquery to filter the results. Something like this.
select MAX(GroupKey)
from dbo.ediFunctionGroup_abc fc
join custom.dbo.ediX091Header_abc h on h.GroupKey = fc.GroupKey
where h.status = 'Go'
and h.CheckIssue = (select CONVERT(char(10), max(paydate)) from DATA.dbo.payment where status like 'PAID%')

MYSQL query needs improvement

I have a query below that produces a simple list of plants (the field named "thriller")
Problem:
Query below considers only the first sequential set of matching items from the table, then displays them in random order. The query is not considering other matching items that are found later in the table.
Solution Needed:
I want the query to choose random results from ALL items in the table, not only the first set of matching results.
$row_object = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}personal_creations_assistant
WHERE pot_sun_exposure = %s
AND pot_height = %s
AND pot_size = %s
AND pot_shape = %s
AND pot_placement = %s
GROUP BY thriller
ORDER BY RAND() LIMIT 0,3
",
$sun_exposure,
$height,
$size,
$shape,
$placement
)
);
If i get it right, you want to get some random rows from database. Random 3 rows.
You could do it with php. First make a COUNT to find how many rows you have total. Then say you call your variable $rowstotal; you make another one $limit_s = rand(0, $rowstotal); Then you add it to your query:
... LIMIT $limit_s, 3

How can I "order by" only the LIMIT results in a mysql Query?

Hi I need to get the results and apply the order by only in the limited section. You know, when you apply order by you are ordering all the rows, what I want is to sort only the limited section, here is an example:
// all rows
SELECT * FROM users ORDER BY name
// partial 40 rows ordered "globally"
SELECT * FROM users ORDER BY name LIMIT 200,40
The solution is:
// partial 40 rows ordered "locally"
SELECT * FROM (SELECT * FROM users LIMIT 200,40) AS T ORDER BY name
This solution works well but there is a problem: I'm working with a Listview component that needs the TOTAL rows count in the table (using SQL_CALC_FOUND_ROWS). If I use this solution I cannot get this total count, I will get the limited section count (40).
I hope you will give me solution based on the query, for example something like: "ORDER BY LOCALLY"
Since you're using PHP, might as well make things simple, right? It is possible to do this in MySQL only, but why complicate things? (Also, placing less load on the MySQL server is always a good idea)
$result = db_query_function("SELECT SQL_CALC_FOUND_ROWS * FROM `users` LIMIT 200,40");
$users = array();
while($row = db_fetch_function($result)) $users[] = $row;
usort($users,function($a,$b) {return strnatcasecmp($a['name'],$b['name']);});
$totalcount = db_fetch_function(db_query_function("SELECT FOUND_ROWS() AS `count`"));
$totalcount = $totalcount['count'];
Note that I used made-up function names, to show that this is library-agnostic ;) Sub in your chosen functions.

Check if there are at least 2 rows for a query in MySQL

Is there a better way to check if there are at least two rows in a table for a given condition?
Please look at this PHP code:
$result = mysql_query("SELECT COUNT(*) FROM table WHERE .......");
$has_2_rows = (mysql_result($result, 0) >= 2);
The reason I dislike this, is because I assume MySQL will get and count ALL the matching rows, which can be slow for large results.
I would like to know if there is a way that MySQL will stop and return "1" or true when two rows are found. Would a LIMIT 2 at the end help?
Thank you.
This is a good question, and yes there is a way to make this very efficient even for large tables.
Use this query:
SELECT count(*) FROM (
SELECT 1
FROM table
WHERE .......
LIMIT 2
) x
All the work is done by the inner query, which stops when it gets 2 rows. Also note the select 1, which gives you a tiny bit more efficiency, since it doesn't have to retrieve any values from columns - just the constant 1.
The outer count(*) will count at most 2 rows.
Note: Since this is an SQL question, I've omitted PHP code from the answer.
The query below will only inspect two rows as you request. mysql_num_rows can check how many rows are returned without any looping.
$result = mysql_query("SELECT col1 FROM t1 WHERE ... LIMIT 2");
if (mysql_num_rows($result) == 2) {
Please avoid using ext/mysql and switch to PDO or mysqli if you can.
$result = mysql_query("SELECT COUNT(*) FROM table WHERE .......");
if(mysql_num_rows($result) > 1)
{
echo 'at least 2 rows';
}
else
{
echo 'less than 2 rows';
}

Perl MySQL - How do I return a tables last row id without looping through whole table?

my $sth = $dbh->prepare("SELECT id
FROM user
WHERE group == '1'
ORDER BY id DESC
LIMIT 1");
I was trying to get the id of the last row in a table without reading the whole table.
I am already accessing via:
my $sth = $dbh->prepare("SELECT name,
group
FROM user
WHERE group == '1'
LIMIT $from, $thismany");
$sth->execute();
while(my ($name,$group) = $sth->fetchrow_array()) {
...and setting up a little pagination query as you can see.
But, I am trying to figure out how to detect when I am on the last (<= 500) rows so I can turn off my "next 500" link. Everything else is working fine. I figured out how to turn off the "previous 500" link when on first 500 page all by myself!
I thought I would set up a "switch" in the while loop so if ($id = $last_id) I can set the "switches" var.
Like:
if ($id = $last_id) {
$lastpage = 1; #the switch
}
So I can turn off next 500 link if ($lastpage == 1).
I am really new to this and keep getting stuck on these types of things.
Thanks for any assistance.
Try to grab an extra row and see how many rows you really got. Something like this:
my #results = ( );
my $total = 0;
my $sth = $dbh->prepare(qq{
SELECT name, group
FROM user
WHERE group = ?
LIMIT ?, ?
});
$sth->execute(1, $from, $thismany + 1);
while(my ($name, $group) = $sth->fetchrow_array()) {
push(#results, [$name, $group]); # Or something more interesting.
++$total;
}
$sth->finish();
my $has_next = 0;
if($total == $thismany + 1) {
pop(#results);
$has_next = 1;
}
And BTW, please use placeholders in all of your SQL, interpolation is fraught with danger.
Always asking for one more row than you are going to show, as suggested by mu is too short, is a good way.
But if you want to take the other suggested approach of doing two separate queries, one to get the desired rows, and one to get the total count if there had not been a limit clause, MySQL provides an easy way to do that while combining as much of the work as possible:
SELECT SQL_CALC_FOUND_ROWS name, group FROM user WHERE group = '1' LIMIT ..., ...;
then:
SELECT FOUND_ROWS();
The SQL_CALC_FOUND_ROWS qualifier changes what a following FOUND_ROWS() returns without requiring you to do a whole separate SELECT COUNT(*) from user WHERE group = '1' query.
SELECT COUNT(*) from tablename will give you the number of rows, so if you keep a running count of how many rows you have read so far, you'll know when you're on the last page of results.
You could generate that query with (untested; away from a good workstation at the moment):
my $sth = $dbh->prepare("select COUNT(*) FROM user WHERE group == '1'");
my #data = $sth->fetchrow_array;
my $count = $data->[0];
(PS. you should be aware of SQL injection issues -- see here for why.)
As Ether mentioned in the comments, pagination usually requires two queries. One to return your paged set, the other to return the total number of records (using a COUNT query).
Knowing the total number of records, your current offset and the number of records in each page is enough data to work out how many pages there are in total and how many before and after the current page.
Although your initial suggestion of SELECT id FROM table WHERE ... ORDER BY id DESC LIMIT 1 should work for finding the highest matching ID, the standard way of doing this is SELECT max(id) FROM table WHERE ...