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).
Related
I'm working on a time based game (where a lower time is better), and I have a leaderboard for it, which stores every playthrough into a SQL database.
I'd like to get the top 15 lowest (best) times from the database, but only show one output for each user, so that the best times of the top 15 users are displayed, but I can't seem to do it while returning all of the information.
The Query I'm using is:
SELECT * FROM `scores` WHERE size='$size' ORDER BY `time` ASC LIMIT 15
Thank you.
If you group your data using the user column, you can use MIN() to isolate the lowest/best time for each users. Finally, you sort by BestTime ASC (so that lower numbers are listed first) and truncate the result set with LIMIT.
Query:
SELECT `user`, MIN(`time`) AS BestTime
FROM `scores`
WHERE `size` = '10x10'
GROUP BY `user`
ORDER BY `BestTime`
LIMIT 15;
SELECT * FROM (SELECT user,size,min(time) as time FROM scores
WHERE size = '10x10'
GROUP BY user, size)
ORDER BY time
LIMIT 15
Selects minimum time for each users and returns top 15 users with their min time score.
You would appear to want something like this:
select s.*
from scores s
where s.score = (select max(s2.score) from scores s2 where s2.userid = s.userid)
order by s.score asc
limit 15;
I have no idea what size is for in your sample query.
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 have this query but I just want to return the top 10 based on the num_guess. I don't know the format of the query. I don't know where to put LIMIT or TOP. Please help!
"SELECT user,num_guess FROM game JOIN difficulty USING (difficulty_no) WHERE difficulty_no=2 ORDER BY num_guess ASC "
use limit 10
something like this
"SELECT user,num_guess FROM game
JOIN difficulty USING (difficulty_no)
WHERE difficulty_no=2 ORDER BY num_guess ASC limit 10"
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
-- SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
SELECT user,num_guess FROM game
JOIN difficulty USING (difficulty_no)
WHERE difficulty_no=2 ORDER BY num_guess ASC limit 0, 10
you can go with LIMIT 10 here in this context
Just add LIMIT 10 at the end of your query.
use LIMIT 10
"SELECT user,num_guess FROM game
JOIN difficulty USING (difficulty_no)
WHERE difficulty_no=2 ORDER BY num_guess ASC LIMIT 10"
for more information check this http://dev.mysql.com/doc/refman/5.5/en//limit-optimization.html
Yes the limit would work here.
SELECT user,num_guess FROM game
JOIN difficulty USING (difficulty_no)
WHERE difficulty_no=2 ORDER BY num_guess ASC LIMIT 10;
To view more just increase the limit.
My search query runs like:
select * from posts p where p.post like '%test%' ORDER BY p.upvotes DESC,
p.unix_timestamp DESC LIMIT 20
If there are more than 20 results for the searched keyword, i find out the minimum timestamp value, store it in a hidden element and run another query to Load More results like:
select * from posts p where p.post like '%test%' and p.unix_timestamp < 1360662045
ORDER BY p.upvotes DESC, p.unix_timestamp DESC LIMIT 20
Whats really happening is that my first query is ignoring (Obviously, my mistake) posts which haven't had any votes(meaning 0 votes) because of my ORDER BY p.upvotes DESC and as a result of this, i noticed that it fetched the first post in the table in the first 20 results, so the minimum timestamp becomes first post's timestamp. Now after this, if i try to fetch the next 20 results which is less than the minimum timestamp, it doesn't give anything.
Right now, i am simply using the upvotes ordering to fetch top records. Should i be using some algorithm like Bayesian Average or some other algorithm?
Please advise how i can improve the queries if i had to stay with current system of ordering or is there any viable and more efficient method i should be using?
P.S. If possible, please refer some resources about the Bayesian Average(it seems to be most used) or some other alternative?
Storing the timestamp when you first do a search and then using that for the next query you could use something like this:-
$sql = "SELECT *
FROM posts p
LEFT OUTER JOIN (SELECT post_id, COUNT(*) FROM post_ratings WHERE timestamp_rated <= $SomeTimeStoredBetweenPages GROUP BY post_id) pr ON p.id = pr.post_id
WHERE p.post like '%test%'
ORDER BY pr.post_ratings DESC, p.unix_timestamp
DESC LIMIT ".(($PageNo - 1) * 20)." 20";
This is very much an example as I have no real idea of you table structures. Also not sure if you just have a row for each up vote, or whether there are down votes to take account of as well.
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