I have got this MYSQL
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` DESC LIMIT 0,50
This code returns me data in this order
1 newest
2 newest
3 newest
4 newest
5 newest
But i want data to be returned in this way
5 newest
4 newest
3 newest
2 newest
1 newest
How can i do that?
Use this :
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) sub ORDER BY DATE ASC
As I understand you want to change direction of records after getting first 50 records.
You can use sub-query feature of MySQL. Assume that result of your query is new, shiny table. So write SQL for desired result using this table (actually it is data set). (In our case we rename it as results)
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) as results ORDER BY results.DATE ASC
Also if you have "count" of results you can do same thing using single-level query. Think about that;
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` ASC LIMIT ***MyCount***,50
MySQL will not force you to rename your sub-query results but renaming
them results in more readable SQL codes.
Related
I have a table in my database which is updated randomly. I'm trying to pull entries by the latest date. This part is simply and I can do it with ease. However, I want to pull the two latest dates.
Example; If my last update was 2015-06-22 and the one before than was 2015-06-12 and the one before then was 2015-06-02. I would want to pull 2015-06-22 and 2015-06-15.
I would use a LIMIT 2, however, there are an unknown amount of items that may have the same date attached.
I haven't tried anything other than the LIMIT 2. After some research, I wasn't able to find anything to reference.
Update
I used SELECT DISTINCT to get the desired results.
SELECT DISTINCT dates FROM table ORDER BY dates DESC LIMIT 2
Will give you the latest 2 dates in the table.
I would have a column set to id, that is auto incremented, and do my query like this:
SELECT * FROM tbl_name ORDER BY `id` DESC LIMIT 2
Crap McAdam you beat me to it!
You can get the latest two dates using LIMIT, like you mentioned:
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2;
And you can join that to your original table to only select rows that occur on those two dates:
SELECT m.*
FROM myTable m
JOIN(
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2) tmp ON tmp.latestDates = m.dateColumn;
I have posts which has fields (ID,title,date) What I'm looking for it to select rows order by date desc just limit for 25 records
lets say if we run that one it will show the last row with ID (600) like this:
(600,601,602,.....,625)
so I want after that to select another 25 records but min(id) before the last one (600) so it will be like this
(575,576,577,.....,599)
For example consider that the first result gives the descending last five IDs.
SELECT idprocess FROM process ORDER BY idprocess DESC LIMIT 5
248034
248033
248032
248031
248030
You could use a SELECT into a temporary table to mess with your result set's ORDER BY and LIMIT. The sub-query LIMIT should be the sum of the records you want back and how many you want skipped off the end.
SELECT t1.idprocess FROM
(SELECT idprocess FROM process ORDER BY idprocess DESC LIMIT 10) AS t1
ORDER BY idprocess ASC LIMIT 5
248025
248026
248027
248028
248029
I want to create a random image banner which fetches the image data from a MySQL database.
And I only want to fetch the latest three records, randomly.
How can I fetch the the 3 most recent records, in a random order?
I am the following query:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 1
but it's not working.
You said 3 records. So try:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 3
You're limiting your results to one with LIMIT 1. Change it to LIMIT 3 to get three results.
Try this if you want one of the last three records:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' AND id+3>last_insert_id() ORDER BY RAND() LIMIT 1
You could use this:
SELECT *
FROM bottom_advt
WHERE ID IN (SELECT * FROM (
SELECT id
FROM bottom_advt
WHERE bottom_advt_page_name='News'
ORDER BY id DESC
LIMIT 3) last)
ORDER BY RAND()
LIMIT 1
Subquery will return the last 3 id that have bottom_advt_page_name='News', and the outer query will randomly select one of those.
Please see fiddle here.
I have a mysql table named events. Some events are featured. I want to randomly display one of the two latest featured events. The field 'timestamp' holds the UNIX timestamp of the event's creation time.
The query looks like this now:
$query = "SELECT * FROM events WHERE featured = 1 ORDER BY timestamp DESC LIMIT 2;";
Is there a way to syntax the query to return just one of those two events and display it right away, or should I go around it with php?
What is recomended here?
Use a ORDER BY RAND() LIMIT 1;, as per MySQL documentation for RAND() (near the bottom of the explanation). I'm not sure if you can do it without the nesting, but it shouldn't be all that expensive given that your nested table only has 2 rows.
SELECT * FROM
(SELECT * FROM events WHERE featured = 1 ORDER BY timestamp DESC LIMIT 2)
ORDER BY RAND() LIMIT 1;
Try:
SELECT * FROM
(SELECT * FROM EVENTS WHERE featured = 1 ORDER BY `timestamp` DESC LIMIT 2) AS temp
ORDER BY RAND() LIMIT 1
Welcome,
I'm wondering is it possible to reverse returned data in sorting "order by desc" but i want that data in reverse order.
For example, i got table with values
ID
1
2
3
4
And i do
Order by ID ASC LIMIT 3
I got
1
2
3
When i do Order by ID DESC limit 3
i get
4
3
2
I would like to have
3
2
1
So i would like to order by ASC but revers results.
I was always doing this in PHP side using array_reverse, but today i want ask You.
Maybye i'm wrong and i can do this just in Mysql.
Regards
SELECT *
FROM (
SELECT ...
FROM ...
ORDER BY ID ASC
LIMIT 3
) AS sq
ORDER BY ID DESC
Think of it as working in two steps. First it executes the inner query: selects 3 records with lowest IDs. Then in the outer query it sorts them in descending order.
You can fetch the first three rows using a subquery and then reverse the order of these rows in an outer query:
SELECT *
FROM
(
SELECT *
FROM yourtable
ORDER BY ID
LIMIT 3
) T1
ORDER BY ID DESC
Nope, you aren't wrong. There are no difference for any sensible amount of data. Array_reverse is all right.
That's not a thing you have to be concerned too much of. Just use whatever you like more - for readability or other subjective reasons
You can do this with a sub query :
SELECT * FROM
(SELECT * FROM myTable ORDER BY idMyTable LIMIT 0, 3) AS r
ORDER BY r.idMyTable DESC
Resources :
Mysql - Subqueries
You could use an outer SELECT to reverse the order:
SELECT *
FROM (
SELECT …
ORDER BY id ASC
LIMIT 3
) sub
ORDER BY id DESC