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;
Related
Hello developers out there,
I want to create a ranking list in my program, and for that want to get the users of the mysql database ordered by their points. I'm doing this with:
select `name`,`points` from users order by `points` desc
For better performance i don't want to get all users but just 10+ and 10- from a given user, because i don't want to show the whole ranking list in the program anyways. Could somebody help me with this?
One method uses subqueries and union all:
(select name, points
from users
where points <= (select u2.points from users u2 where u2.name = ?)
order by points desc
limit 11
) union all
(select name, points
from users
where points > (select u2.points from users u2 where u2.name = ?)
order by points asc
limit 10
) ;
This returns 21 rows, with the specified user included.
I have a scenario, where I need to find a random winner from a group of entries.
The entries can be multiple times and now I am fetching all the records grouped by user ID and using PHP's array_rand() method to find a random winner. The grouping is used to avoid duplicate elements.
Here I am facing two problems
The query is timing out as it is dealing with almost 10000000 records.
PHP memory is exhausted because of large number of records.
My current query is a simple one and it looks like this
SELECT id, userID from table where id!= 1111 and created_at >='2017-08-10' group by userID
What is the best method, which will work on this large-scale?
Use ORDER BY RAND() and limit results to 1 to avoid memory getting exhausted.
SELECT id, userID
from table
where id!= 1111 and created_at >='2017-08-10'
group by userID
ORDER BY RAND()
LIMIT 1;
Try doing everything in SQL , might be faster.
SELECT id, userID
FROM table
WHERE id!= 1111 and created_at >='2017-08-10'
GROUP BY userID
ORDER BY RAND()
LIMIT 1;
May this will help
SELECT userID
FROM table AS t1 JOIN
(SELECT CEIL(RAND() *
(SELECT MAX(userID)
FROM table)) AS userID)
AS t2
where id!= 1111 and created_at >='2017-08-10'
ORDER BY t1.userID ASC
LIMIT 1
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.
So I'm working on a script that awards "trophies" to the top 4 performers of a game. The table logs each "grab" attempt, the user that performed it, and whether it was successful. I'd like to create a script that is able to pull the top four off of percentage of successful grabs (attempts / successes)
Is something like this possible within the query itself using mysqli?
I have successfully accomplished the code already by just looping through each table entry, but with thousands of attempts per month it just seems like a clunky way to go about it.
Here is an example of a row in the table, I am attempting to grab the top four based off of monthlyTries/monthlySuccessful
id userId PetId PalId tries successfulGrabs monthlyTries MonthlySuccessful
5 44550 84564 3967 825 268 120 37
Assuming you have a success column that's either 1 or 0 you can sum the success and divide that by count(*) which is the total # of attempts
select user_id, sum(success)/count(*) percentage
from attempts a
group by user_id
order by percentage desc
limit 4
If the success column is not a 1/0 value you can use conditional aggregation
select user_id, sum(case when success = 'yes' then 1 else 0 end)/count(*) as percentage
from attempts a
group by user_id
order by percentage desc
limit 4
In MySQL, you can simplify the logic. If success takes on the values 0 and 1:
select a.user_id, avg(success) as percentage
from attempts a
group by a.user_id
order by percentage desc
limit 4;
Otherwise:
select a.user_id, avg(success = 'yes') as percentage
from attempts a
group by a.user_id
order by percentage desc
limit 4;
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