Mysql ASC function ORDER only first ID's - php

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") ;

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();

PHP SQL Condition on ORDER BY

I am trying to display all records with field_featured = 1 to start at the top of my listing ... and all the others to display at RAND()
So...
SELECT * FROM myTable
If (field_featured = 1 then ORDER BY field_featured) OTHERWISE (ORDER BY RAND() )
How can I do this?
Try this:
ORDER BY (field_fieatured=1) DESC, RAND()

return random 5 records from last 20 records

Simply I have the following
table with records , i want to return randomly 5 records from the last 20 record (order by id desc)
so how we can do it fast
thanks for help.
select * from
(
select * from your_table
order by id desc limit 20
) as lastest_results
order by rand()
limit 5;
Use an inner query to return the last 20, and an outer query to select 5 of them randomly. This may be slow though.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 20) t ORDER BY RAND() LIMIT 5;
This is a slow method but it gets the JOB done:
ORDER BY RAND()
LIMIT 5;
If you are using a large table this can become very slow,
There are multiple alternative that you can read about here
Alternative to order by rand
You need something like
SELECT * FROM table ORDER BY RAND() LIMIT 5;
If you have a time parameter to sort them by for example the last 20 records then use this.
SELECT * FROM table ORDER BY insert_time DESC, RAND() LIMIT 5;
OR
SELECT * FROM table ORDER BY id DESC, RAND() LIMIT 5;

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