Selecting data from the result of an ORDER BY query - php

Let's say I have this sql query:
SELECT * FROM questions ORDER BY votes LIMIT 3
That is loaded on one page. How do I get the the next 3 by providing the ID of the last one retrieved(1). Example of a table:
ID votes
--------
4 29
45 26
1 23
7 13
23 5
9 2

SELECT * FROM questions ORDER BY votes LIMIT 3, 3
You can select range using limit. Ex Limit x,y where x=start, y=length
EDIT:
Since you saying that you want to get this by passing the id. You can obtain relevant data set using following kind of query.
SELECT * FROM questions WHERE votes <= (SELECT votes FROM questions WHERE id = 1) AND id != 1 ORDER BY votes DESC LIMIT 3
However there is an issue if you have multiple records with same votes. So I would like to suggest you keep the record count you have displaced so far.
$displayed = 0;
$rowsPerPage = 3;
So you can simply get data by passing these parameters without an issue.
$start = 1;
if($displayed >0)
$start = $displayed + $rowsPerPage;
$sql = "SELECT * FROM questions ORDER BY votes LIMIT $start, $rowsPerPage ";
In next page load or pagination increase $displayed.
$displayed += $rowsPerPage;
You have to keep $displayed as a Session if you have page loads, Otherwise it could be a parameter.

You can use offset SELECT * FROM table_name LIMIT offset,limit.
Example:
SELECT * FROM questions ORDER BY votes LIMIT 3,3

You should use LIMIT <offset>, <count> tag instead of the id of the last on retrieved, since you are ordering by votes, not by id.
SELECT * FROM questions ORDER BY votes LIMIT 3, 3

Use below
Set 1 : SELECT * FROM questions ORDER BY votes LIMIT 3
Set 2 : SELECT * FROM questions ORDER BY votes LIMIT 4, 3
Set 3 : SELECT * FROM questions ORDER BY votes LIMIT 7, 3
and so on...
You need to keep on changing the offset value...
Good Luck!!!

Related

Select only 5 random rows in the last 50 entries

I'm just starting with MySQL so I would like to know how can I select only 5 random rows in the last 50 entries of my database? I hope you understand my question.
I'm using PDO and what I have now is this:
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
Then I use a PHP foreach loop...
Thank you
The challenge is determining the last 50 entries. Assuming you have an auto-incremented id, you can do:
SELECT a.*
FROM (SELECT a.*
FROM articulos a
WHERE cat = '$ArtCat'
ORDER BY id DESC
LIMIT 50
) a
ORDER BY RAND()
LIMIT 5;
The key idea is the subquery to get the last 50 entries, and then the final query to get the 5 random rows. The subquery needs to specify how you identify the last 50.
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() LIMIT 5 ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
just add limit
i assume in your table you have some date column so we can get last 50
SELECT * FROM (SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY created_tiem desc limit 50 ) t order by RAND() limit 5;

Count how much matches in last 10 rows

I need help to implement how much status=1 I have in last 10 rows from results?
If its there 3 status in last 10 rows, i need to got 3 output.
SELECT * FROM results WHERE $position='$text' and make='$make' and status='1'
You need to use a subquery to get the last 10 rows, and then count how many have status = 1 in that subset.
SELECT COUNT(*) AS count
FROM (SELECT status
FROM results
WHERE $position = '$text' AND make = '$make'
ORDER BY id DESC
LIMIT 10) AS last10
WHERE status = '1'
DEMO

Pulling specific data on top and restricting its limit in MySQL PHP

I have following sql query which pulls data with the id 627 to top and then it displays data with other ids
$sql_query = "select * from listing ORDER BY case when listing.makaan_id='627' then 1 else 2 end, listing_id DESC LIMIT {$start}, {$limit}";
im using php pagination system to paginate the results where i set the pagination limit to 10. Now i want to show only 4 results on top with the id 627 and rest with other ids, so in a set of 10 results 4 would be from the id 627 and other 6 will be from other ids, do i have any solution for this without writing another separate sql statement?
You can try something like this
SELECT *
FROM listing
WHERE makaan_id = 627
ORDER BY listing_id DESC
LIMIT 4
UNION ALL
SELECT *
FROM listing
WHERE makaan_id <> 627
OR makaan_id IS NULL
ORDER BY listing_id DESC
LIMIT {$start} - 4, {$limit} - 4

Select Next mysql row without refer to id

This is My Table:
#id# #cpc#
100 10
87 9
101 9
4 6
188 5
it's sorted DESC according to 'cpc' column.
I Want to extract the rows one by one without referring to id.. such as you can see it.
SELECT * FROM table ORDER BY cpc DESC
first result is with the id 100
next one is with id 101 and cpc 9 not 87.. as the id is only increasing.. so it selects wrong rows not as what i want.
You can do something like this in php:
$sql = mysql_query("SELECT * FROM yourTable ORDER BY cpc DESC");
while(($row = mysql_fetch_array($sql)){
$id = $row['id'];
$cpc = $row['cpc'];
Select them using limit and offset:
SELECT *
FROM table
ORDER BY cpc DESC
LIMIT 1 OFFSET 0;
Then:
LIMIT 1 OFFSET 1
LIMIT 1 OFFSET 2
and so on.
You need to edit your ordering.
SELECT *
FROM table
ORDER BY cpc DESC,
Id ASC

80 rows of data , first select 1-20 rows , second select 21-30 rows

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.

Categories