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.
Related
This question already has answers here:
PHP/MySQL Pagination
(3 answers)
Closed 6 years ago.
I am creating API in php for android project. where I need to Create Product items API that gives first 10 records when request method pass 0
value, and gives 10 records more when value is 1.
Thus API gives 0-10,11-20 records each time request is made with some int value.
currently I am doing this:
$prod=$_REQUEST['prod'];
$result= sql("SELECT * FROM products where pID='$prod'");
echo json_encode(array('item'=>$result));
But it gives me all products from table. how can I get 0-10, then 11-20 and so on.
I think you are looking for something like this:
"SELECT * FROM `products` WHERE `pID` > $prod ORDER BY `pID` ASC LIMIT 10"
This query fetches all products with pID higher than $prod, orders them by pID ascending, and limits the result to 10 rows. So the $prod you send will the pID of the last record you fetched.
I suggest you look some more into mySQL since this is fairly basic stuff. Here's a basic tutorial.
You want to use pagination then you learn about limit and offset what are limit and offset and how works in pagination.
$limit=10; ////per page
$start=$_REQUEST['your-requested-offset'];0,1,2,3, and so on
$query="SELECT * FROM TABLE $limit, $start";
more you read this Artical about Limit and Offset
Pagination using MySQL LIMIT, OFFSET
for how work pagination? read this artical
Paginate Data With PHP
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I just wanna show last 10 or 30 depends on list if there is big data, searched over the net but all by correcting I can see only the 1st data not newest one.
$sql = "SELECT id, tag, count FROM url ORDER BY id, tag, count ASC LIMIT 1";
I am testing with two datat on database and want to display last one but all the time it shows the oldest one (first data), I changed ASC and DESC also, both time result is same.
Try this..
Order by id using "DESC" it get last to first and limit 0,10 means it display last 10 record
$sql = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 0 10"
Use this one:
$Query = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 10"
Change your query & try this:
$Query = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 10";
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 7 years ago.
Improve this question
I'm doing a program in php where I have set up table rms having fields rid, type and room. Type denotes whether its standard, cheap, expensive etc and room denotes the total number of rooms available.
I want to have a ddl to display the type and cascading ddl that display rooms like 1,2,3, etc up to total number available.
For example if I chose cheap from first ddl and its total rooms is 30 in the table, then the next ddl should show 1 to 30 in the list.
I was thinking about retrieving id from the first ddl to fetch the room corresponding to it but seems to have problems. Can it be done?
From the MySQL documentation:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter.
This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
With one argument, the value specifies the number of rows to return from the beginning of the result set:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
From stackoverflow
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
This question already has answers here:
How to request a random row in SQL?
(30 answers)
Closed 8 years ago.
I have names in my database and I want to draw a name for a contest.
Anyone have an idea for that ?
Thanks !!
SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1
This works in constant time, regardless of the table size, if num_value is indexed. One caveat: this assumes that num_value is equally distributed in the range 0..MAX(num_value). If your dataset strongly deviates from this assumption, you will get skewed results (some rows will appear more often than others).
A query like this could work
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;