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");
Related
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();
I have this line:
$query = mysql_query("SELECT * FROM livechat WHERE type='public' ORDER BY id ASC LIMIT 15") ;
And this is for chat, however ASC takes only first ID comments, so it shows only 15 old comments (id1, id2 and so on). If I use DESC instead of ASC, it shows new comments, but in a bad way - newest at the top, since this is a chat, newest comments must be at the bottom.
Try creating a temporary table that contains the last 15 results, and then ordering from that table.
select * from (
select * from livechat where type='public' order by id desc limit 15
) tmp order by tmp.id asc
try like this:
$query = mysql_query("SELECT *
FROM (
SELECT *
FROM livechat
WHERE type='public'
ORDER BY id DESC LIMIT 15
) t
order by t.id") ;
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
Anybody have any idea why this query isn't working?
$result = mysql_query("SELECT * FROM Events ORDER BY ID ASC LIMIT 0, 10");
I want the data to order backwards, from the highest value ID all the way to 1.
But it doesn't do that. It just orders them 1,2,3,4,5
Any help is greatly appreciated.
You are sorting in ASCending order when you want to sort in DESCending order. Try the following instead:
$result = mysql_query("SELECT * FROM Events ORDER BY ID DESC LIMIT 0, 10");
If per chance, you want the 10 items with the lowest ID's but want them in descending order, then you could use the following:
$result = mysql_query("SELECT * FROM (SELECT * FROM Events ORDER BY ID ASC LIMIT 0, 10) ORDER BY ID DESC");
Pretty simple.
ORDER BY ID DESC
(ASC means ascending, DESC means descending).
Change ASC to DESC.
That's "ascending" to "descending".
Use DESC in your ORDER BY statement:
$result = mysql_query("SELECT * FROM Events ORDER BY ID DESC LIMIT 0, 10");
change your ASC to DESC like this
$result = mysql_query("SELECT * FROM Events ORDER BY ID DESC LIMIT 0, 10");
SELECT * FROM Events ORDER BY ID DESC LIMIT 0, 10
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