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.
Related
my table keep million records.
i just want to extract the 5 most counter rows in last 200 rows.
Im using this but as i thinking this is two time select .. that may not right for best performace plase advice.
"select * from (
select sid,title,catid,counter from table_stories
where catid=1 order by sid desc limit 200
) astemps order by counter desc limit 5"
thanks very much..
regards
This is your query:
select *
from (select sid, title, catid, counter
from table_stories
where catid = 1
order by sid desc
limit 200
) astemps
order by counter desc
limit 5;
For optimal performance, you want an index on table_stories(catid, sid desc). You can throw title and counter into the index, but they won't help much. Unfortunately, MySQL may not take advantage of the descending key for to replace the sort.
Let's say I have 1000 rows in a database and I want to display 950 of it.
The rows I want to display should be the first 950 without the last 50. So how can I protect the last 50 rows?
Something like this query but I would like to start with ASC to select the rows from the beggining not from the end.
$tab= mysqli_query($con, "SELECT id,title,url FROM users ORDER by id DESC limit 50,950");
What I need is 1,2,4,5...950 from 1000 rows ( without the last $limit rows). I will change the $limit,950 depending when I need it.
Thank you.
MySQL does allow LIMIT in the DELETE. But, I don't believe it allows an offset. This is easily handled by reversing the order:
DELETE u
FROM users u
ORDER BY id ASC
LIMIT 950;
You can also write this more directly incorporating your original query:
DELETE u
FROM users u JOIN
(SELECT u2.*
FROM users u2
ORDER BY u2.id DESC
LIMIT 50, 950
) todelete
ON u.id = todelete.id;
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 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.