I have created a table that is similar to the one below, my goal is to LIMIT the result to 10 AND THEN return the id of the LAST result which is 10. I've tried doing, the query below but it keeps returning my the value of 15, instead of 10.
SELECT id FROM this_table WHERE value=value ORDER BY id DESC LIMIT 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
select max(id) from
(
SELECT id
FROM this_table
WHERE value = 'some_value'
ORDER BY id
LIMIT 10
) x
LIMIT can take two parameters.
Try
SELECT id FROM this_table WHERE value=value
ORDER BY id LIMIT 9,1
Read all about it.
EDIT: Oh, and loose the DESC part. It seems you don't really need it.
Related
I need to get the top 4 records ordered by a specific column descending. If more than one value is the same then I need to randomise them.
For example:
ID - VALUE
1 - 10
2 - 5
3 - 5
4 - 3
5 - 3
6 - 3
7 - 3
8 - 3
9 - 3
10 - 3
So in this example 10 is the highest so will always be top. 5 is the second highest so it will randomly order both of the 5 values. it will then select a random one with the value of 3.
I hope this is clear.
edit:
I have tried ORDER BY Value DESC and assumed that it would select them randomly but it seems as though there is a predetermined order as the same ones keep displaying.
I have also tried ORDER BY Value DESC, RAND(ID) which does the same as above but with different values.
Use this ORDER clause:
SELECT ... ORDER BY your_column DESC, RAND()
I have generated an sql to query a database table where posts from users are saved.But I am getting error executing it.I have tested it here , It also tells me that it is incorrect and the problem lies in users_user_id section.I can't figure out what might be wrong here.Can any one help me with this error.
My userpost table structure is following:
post_id,post,title,users_user_id
sql:
SELECT title FROM userpost LIMIT 5 OFFSET 10 WHERE users_user_id=9
LIMIT always come after all keywords.
Corrected SQL:
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Generally, SELECT query has following sequence:
SELECT
Fields List
FROM
WHERE
GROUP BY (if needed)
ORDER BY (if needed)
LIMIT (This always come at end and if limit is not specified,
query will return all records from result set.)
You can set offset with limit as per below-
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 10, 5;
Where after limit first argumet is offset and 2nd is for no. of record means limit.
where should be use before limit
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
change:
SELECT title FROM userpost LIMIT 5 OFFSET 10 WHERE users_user_id=9
to:
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Try this way :
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
where clause use after from.
LIMIT always come at last position
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5,10
I hope this will work
You need to change your Query. Always where condition place before LIMIT and OFFSET:
SELECT title FROM userpost WHERE users_user_id = 9 LIMIT 5 OFFSET 10;
limit should be after where condition
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Try this way
And LIMIT should be after where condition
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Write your where condition first then use limit. LIMIT always come after all keywords.
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Posts
-------------
5
4
6
7
3
I want to get the rows where Posts - 4 >= 1
that mean i wanna get
QUERY Result :
5
6
7
Something like that : SELECT content FROM mytable WHERE (Posts - 4) >= 1 LIMIT 10
Thanks
So then you could use:
SELECT content FROM mytable WHERE Posts >= (1 + "yournumber") LIMIT 10;
You want something like this (if I'm reading you want to take the value in your column and subtract some number from it, then narrow the results which are 1 or greater).
SELECT content FROM mytable WHERE mycolumn-4 >= 1
Since you are doing this with php, create a variable to hold your number, create a query parameter from it, and send the paramter to your query.
maybe something like this?
SELECT content FROM mytable WHERE Post >= (SELECT COUNT(*) FROM mytable) LIMIT 10
A couple months ago I asked a question about something similiar to this, received some help and thought I had the answer. 3 months later I am seeing this didnt work 100% so I need some more help but I can ask the question better now that I understand more.
I have a mysql table with id, userid, rounds, reps, exerciseid
I need to pull a users highest round specific to the exercise I am pulling for. So if the exerciseid was 8 I would need the users top round for that exerciseid but if the user has the same rounds more than once, which happens a lot, then I need to sort that by the reps to give me a true highest performance. Now, after obtaining those results I need to then sort this data set by rounds,reps so if multiple unique users have the same rounds that is then ordered by reps.
Can this be done with pure mysql or am I better off pulling the data and sorting everything with PHP?
SELECT
max(l.rounds) as rounds,
l.reps,l.userid
from leaderboard l
where l.exerciseid = 8
group by l.userid
order by
rounds desc,
reps desc
example of structure
First this is a smaple set
userid exerciseid rounds reps
-- -- --
1 8 23 10
1 8 23 15
1 8 20 10
2 8 28 19
2 8 15 12
3 8 40 29
results I want
userid exerciseid rounds reps
-- -- --
3 8 40 29
2 8 28 19
1 8 23 15
If I understand this correctly, you want to first group by userid and rounds to get the maxreps on a round. Then you want to select the max rounds from this.
Here is one way to express this in MySQL:
select userid, rounds, maxreps
from (SELECT userid, l.rounds, MAX(l.reps) as maxreps
from leaderboard l
where l.exerciseid = 8
group by l.userid, l.rounds
) ur
where exists (select 1 from leaderboard lb where ur.userid = lb.userid and lb.exerciseid = 8 group by lb.userid having max(rounds) = ur.rounds))
order by rounds desc, maxreps desc
how about:
SELECT
max(l.rounds) as rounds,
max(l.reps) as reps,l.userid
from leaderboard l
where l.exerciseid = 8
group by l.userid, l.exerciseid
order by
rounds desc,
reps desc
I want to ascend MYSQL from a value in the database.
Hypothetically:
database with table tbl_numbers column numbers has 10 values in it 1-10.
I want to:
order by numbers desc {from 8} LIMIT 3
it would show
8
7
6
instead of:
order by numbers desc LIMIT 3
10
9
8
If this is possible what php and not mysql that's good too.
--update!--
The example I gave was way to easy, I really need to match the date() with the
dates of the entry's in the database and start the asend or decend from there any ideas? The date format is in 2010-06-18
ORDER BY numbers DESC
LIMIT 3,3
First argument for limit is (10 entries + 1) - 3;
second argument is the number of records to return
Alternatively:
WHERE numbers <= 8
ORDER BY numbers DESC
LIMIT 3
Something along the lines of
ORDER BY (numbers < 8) DESC, numbers DESC LIMIT 3
should do the job.