Select last 5 mysql rows - php

i want to get the last 6 rows but skip the last one in result:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 6
How can i make this?

Try this:
SELECT *
FROM (SELECT *
FROM TableName
ORDER BY FieldName DESC
LIMIT 5) sub
ORDER BY FieldName ASC

Try this:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 1,6

Related

How i can reverse mysqli querie's result order

I don't know the title is relevant enough or not..
Here's the thing i wanna do
i have a table (tbl_users) with two columns
id(Primary)
name(varchar)
with entries..Like this...
when i execute this query
$result=mysqli_query($conn,"select * from tbl_users order by id desc limit 3");
while($row=mysqli_fetch_array($result)){
echo $row["id"]."<br>";
}
and i get these results
7
6
5
But I want result in this order
5
6
7
Edit:
According to your comment, here another try:
$result=mysqli_query($conn,"SELECT * FROM ( SELECT * FROM tbl_users ORDER BY id DESC LIMIT 3) as r ORDER BY id asc");
when you are using order by in mysql you write column for order and (ASC or DESC)
ASC-ascending
DESC- desceding
if you dont put asc or desc, default is asc, in your case its like this
ORDER BY id or
ORDER BY id ASC
$result=mysqli_query($conn,"select * from tbl_users order by id asc limit 3 OFFSET 4");
Try
$result=mysqli_query($conn,"select * from tbl_users order by id ASC limit 3 OFFSET 4");

How to get ORDERED and limited rows form MySQL and randomize show order

Is possible, and how to ask MySQL for
SELECT * FROM my_table ORDER by row_id DESC LIMIT 8
get the last 8, newest record from my table, with randomized order for PHP showing method
$results = $mysqli->query($query);
while($row = $results->fetch_assoc()) {
echo $row['my_col_name'];
}
Colud I, and where put the rand() in my SQL query?
Without randomize I get last 8 rows ORDERED 10,9,8,7,6,5,4,3
I want to get in the following order:
9,7,5,4,6,10,3,8;
8,7,3,6,10,9,5,4
...
You can place it inside another select:
SELECT * FROM (SELECT * FROM my_table ORDER by row_id DESC LIMIT 8) t ORDER BY RAND()
Use a subquery:
SELECT t.*
FROM (SELECT t.*
FROM my_table t
ORDER by row_id DESC
LIMIT 8
) t
ORDER BY rand();

mysql select rows with top max value

i have a table in mysql with columns: id , view , agree.
i have upload my table's image below:
i want to select 8 rows that greater than others in view column. my condition is agree = 1.
can you tell me how can i do it by mysql query or php.
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
Try this:
SELECT * from table
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
use limit and order by
Select * from mytable
where aggree=1
ORDER BY view DESC
LIMIT 8
You can do this:
SELECT *
FROM yourTableName
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
You have to use ORDER BY clause.
SELECT * FROM <TABLE_NAME> WHERE AGREE = 1 ORDER BY VIEW DESC LIMIT 8
Use ORDER BY for DESC or ASC sorting.
and For selection of 8 rows use Limit Data Selections From a MySQL Database
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
If you want to take the top 8 'view' values from the table the MySql query for that is:
SELECT * FROM tablename WHERE agree=1 ORDER BY view DESC LIMIT 8;
Note: Replace tablename with the actual name of your table

Select on Mysql inverse order

i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order

Get Latest Entry from Database

How can I get the latest entry by the latest DATE field from a MySQL database using PHP?
The rows will not be in order of date, so I can't just take the first or last row.
You want the ORDER BY clause, and perhaps the LIMIT clause.
$query = 'SELECT * FROM `table` ORDER BY `date` DESC LIMIT 1';
SELECT * FROM [Table] ORDER BY [dateColumn] DESC
If you want only the first row:
In T-SQL:
SELECT TOP(1) * FROM [Table] ORDER BY [dateColumn] DESC
In MySQL:
SELECT * FROM `Table` ORDER BY `dateColumn` DESC LIMIT 1
You don't have a unique recordid or date revised field you could key in on? I always have at least an auto incrementing numeric field in addition to data created and date revised fields. Are you sure there is nothing you can key in on?
SELECT * FROM table ORDER BY recno DESC LIMIT 1;
or
SELECT * FROM table ORDER BY date_revised DESC LIMIT 1;
So the PHP call would be:
$result = mysql_query("SELECT * FROM table ORDER BY date_revised DESC LIMIT 1");
-- Nicholas
You can use a combination of the LIMIT and ORDER BY clauses.
For example:
SELECT * FROM entries ORDER BY timestamp DESC LIMIT 1

Categories