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
Related
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.
I have this featured listings thing that will grab its information using
mysql_query("SELECT * FROM listings WHERE featured = '1' DESC LIMIT 5")
However, if I didnt have enough results to display, how do I make it so that it would run mysql_query("SELECT * FROM listings ORDER BY date desc") till it reached the 5 limit?
Your first query is missing an order by. But the idea for solving your problem is to sort the data by both conditions, and then choose the first five:
SELECT *
FROM listings
ORDER BY (featured = '1') DESC,
date
LIMIT 5;
I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query
I'm using ajax to fetch more photos to a gallery based on views. The gallery already has a set of 10 photos showing. Now I want to get the next 10 photos in order based on view count from high to low.
$last_image_view_count = 232;
"SELECT * from `gallery` ORDER BY CAST(`views`<'$last_image_view_count' AS SIGNED) DESC LIMIT 10";
The code above works...but its not in order (230 - 216 - 205 etc). Its scattered but under 232. I need help figuring out how to get it in order from high to low.
"SELECT * from `gallery` WHERE `views` < '$last_image_view_count' ORDER BY `views` DESC LIMIT 10";
Use the WHERE-statement to select only the desired set of data (from what I understand you want those, with a view-count that's less than $last_image_view_count).
You can then order it by the views column.
There's no need to cast it in the ORDER BY-statement. What you do with your code is ordering it by 1 or 0 (1 if views are less than your variable, otherwise 0).
I have a table called 'topics' in which all topics are saved. I want to select the latest 5 rows from that table, show them on one page, then select THE OTHER FIVE latest ones and show them on the other page.
I know how to echo all the topic names in a while loop, but the problem here is making mysql select 5 rows, then the other five for another page, not the same ones again. How to achieve this?
SELECT
*
FROM tablename
ORDER BY id DESC
LIMIT 0, 5
on the another page:
LIMIT 5, 5
$start = 0;
$count= 5;
$query = "select *
from topics
order by date desc
limit $start, $count"
First 5:
SELECT * FROM your_table ORDER BY any_order_criteria LIMIT 0,5
Second 5:
SELECT * FROM your_table ORDER BY any_order_criteria LIMIT 5,5
What i got from your problem is that you want to enable Pagination on your page.
This is a wonderful article on Pagination using PHP and MySQL.
http://www.tonymarston.net/php-mysql/pagination.html