Get N value from database mysql - php

i have a table i get cars form databases and i list it in this table:
$row_id=$_GET["id"];
$solK = ($row_id-1) * 9;
$sagK = ($row_id) * 9;
$sorgu2 = mysql_query("SELECT * FROM Car WHERE Car_ID > '$solK' AND Car_ID < '$sagK'");
Every page have 9 cars i use id for sort these cars but when i delete a car (for example Carid=5) in first page have 8 cars but other pages have 9 cars how can i get first N values without CarId from databases can you explain with sql codes.

Add a LIMIT to your query.
For example
SELECT * FROM tbl LIMIT 0, 9
will select the first 9 entries from tbl.
In order to match your query and preserve the ordering I'd state it as
SELECT * FROM Car ORDER BY Car_ID LIMIT 0, 9
for the first nine rows. For the next nine rows, just increment both numbers by 10 and so on.

Rather than code it like you have done, just use LIMIT:
SELECT * FROM Car LIMIT 0,9
then
SELECT * FROM Car LIMIT 9,9

Use "LIMIT offset, row_count" in you statement
Try this SQL statement SELECT * FROM Car LIMIT '$pageN*$nperpage', '$nperpage'
Where $nperpage will be a number of items per page and $pageN will be a page number (note that in this case page numbering starts with 0).
http://dev.mysql.com/doc/refman/5.0/en/select.html

I assume the other answers (using limit and offset) will be suited for your case. But if you (or anyone else) ever need to improve performance, and need to manage fast queries for more than the first few pages, you should implement paging like this:
SELECT f1, f2, ...
FROM tbl
WHERE Car_ID > $id
ORDER BY Car_ID
LIMIT 10

Related

Get previous 10 row from specific WHERE condition

Im currently working on a project that requires MySql database and im having a hard time constructing the query that i want get.
i want to get the previous 10 rows from the specific WHERE condition on my mysql query.
for example
My where is date='December';
i want the last 10 months to as a result.
Feb,march,april,may,june,july,aug,sept,oct,nov like that.
Another example is.
if i have a 17 strings stored in my database. and in my where clause i specify that WHERE strings='eyt' limit 3
Test
one
twi
thre
for
payb
six
seven
eyt
nayn
ten
eleven
twelve
tertin
fortin
fiftin
sixtin
the result must be
payb
six
seven
Thanks in advance for your suggestions or answers
If you are using PDO this is the right syntax:
$objStmt = $objDatabase->prepare('SELECT * FROM calendar ORDER BY id DESC LIMIT 10');
You can change ASC to DESC in order to get either the first or the last 10.
Here's a solution:
select t.*
from mytable t
inner join (select id from mytable where strings = 'eyt' order by id limit 1) x
on t.id < x.id
order by t.id desc
limit 3
Demo: http://sqlfiddle.com/#!9/7ffc4/2
It outputs the rows in descending order, but you can either live with that, or else put that query in a subquery and reverse the order.
Re your comment:
x in the above query is called a "correlation name" so we can refer to columns of the subquery as if they were columns of a table. It's required when you use a subquery as a table.
I chose the letter x arbitrarily. You can use anything you like as a correlation name, following the same rules you would use for any identifier.
You can also optionally define a correlation name for any simple table in the query (like mytable t above), so you can refer to columns of that table using a convenient abbreviated name. For example in t.id < x.id
Some people use the term "table alias" but the technical term is "correlation name".

Displaying limited rows from SQL database using PHP [duplicate]

This question already has answers here:
Retrieving only a fixed number of rows in MySQL
(6 answers)
Closed 6 years ago.
Consider a database table with 100 rows and the table is updated ie the number of rows increases on a regular basis.
Suppose that when a visitor visits my website, my table consist Xrows, but I want only rows from (X-25) to (X-50) to be visible.
I need help to code a PHP such that only rows from (X-25) to (X-50) are visible
I am new to PHP ,SQL a help would aid me a lot :)
You can use limit to show particular record.
The SQL query below says "return only 10 records, start on record 15":
"SELECT * FROM Orders LIMIT 15, 10";
1) Consider, you have 100 records 0-99. Ad you want to display 20 records starting from 11th record then query will be
"SELECT * FROM Orders LIMIT 11, 20";
2) If you want to display 1st 20 records then query will be :
"SELECT * FROM Orders LIMIT 20";
3) If you want to display latest 20 records then :
"SELECT * FROM Orders ORDER BY orderid DESC LIMIT 20";
To get last 20 records you have inserted..
if you are using auto increment primary key orderid,you can use the query
"SELECT * FROM Orders ORDER BY orderid DESC LIMIT 20";
In MYSQl
SELECT * FROM table LIMIT 25, 25 -- get 25 records after row 26
If you want row 25-50 in descending order you can do this by :
SELECT * FROM (SELECT * FROM ORDERS LIMIT 25, 25) ORDER BY id DESC

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

Sorting php/MySQL search results [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a tutoring website with approximately 50 tutors. I'd like to split the search results into 5 pages of 10 tutors each.
The results are weighted, so they don't come out in order according to some primary key. I think this prevents me from writing a query like this for the first page:
SELECT * FROM teachers WHERE primary_key > 0 AND primary key < 11
and a query like this for the second page:
SELECT * FROM teachers WHERE primary_key > 11 AND primary key < 21
So, how can I systematically accept only 10 rows at a time, remember what they are, and then select the next ten rows for the next page? Should I have a second key that assigns a numeric order to the results after calculating their weights? Could this be a temporarily column? Is this a poor way to split up results in an PHP/MySQL environment?
You can use the ORDER BY to sort your results as well as LIMIT to declare limitations.
SELECT * FROM teachers ORDER BY primary_key LIMIT 10
To start at a certain point (ie. 11 and show only 10) you can add this:
SELECT * FROM teachers ORDER BY primary_key LIMIT 11,10
You can then create a PHP script to generate the value for the limit offset... so 21,10, 31,10, etc.
MySQL Select Syntax
You can use LIMIT
SELECT * FROM teachers LIMIT 0, 10;
SELECT * FROM teachers LIMIT 10, 10;
The first number is where to start, and the second is how many results you want to get.
I have an implementation of this where I have a php script to get the page number and the first number is a variable $limit_start. It gets the page number from a post or get.
$limit_start = ($this->page - 1) * 10;
$query = 'SELECT * FROM teachers LIMIT '.$limit_start.', 10';
You should do this using an offset limit within your query. Page 1 would be:
SELECT * FROM teachers limit 0,10
page 2:
SELECT * FROM teachers limit 10,10
page 3:
SELECT * FROM teachers limit 20,10
... and so on. First value is the offset and the second value is the number of rows to retrieve.

Counting all results of MySQL query for paging

I am building a search page, each page featuring 10 results. For that purpose, I would like to display pages numbers at the bottom (like in Google for example) so a user can jump to a specific page result. In order to do that I need to know the overall count of the query (e.g., if I show 10 results per page and the specific query returns 73 rows in my Table, i would need to display links to 8 pages).
The only way I can think of is using the following (obviously inefficient) query:
// Query for the current page
$res = mysql_query("select * from TABLE WHERE COL='Sample' LIMIT $offset,10");
// Geeting the total count do I can build links to other pages
$res2 = mysql_query("select COUNT(*) fromTABLE WHERE COL='Sample'");
Is that the only way to do it?
Thanks,
Danny
you can use
SQL_CALC_FOUND_ROWS
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
SQL_CALC_FOUND_ROWS tells MySQL to
calculate how many rows there would be
in the result set, disregarding any
LIMIT clause. The number of rows can
then be retrieved with SELECT
FOUND_ROWS()

Categories