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()
Related
I want to show top 3 results and if is there are any results which match with top 3, they should also be fetched. In the examples below there are top 3 results, 50,40 and 30, but ram also has a mark of 30, so I want to fetch that result as well.
my database sheet table
id
user
marks
1
ram
30
3
sam
30
4
ben
40
2
hari
10
5
joe
50
i want to return top 3 results along with all matching results which is match to last result
id
user
marks
5
joe
50
4
ben
40
3
sam
30
1
ram
30
$top_results = "SELECT * FROM `sheet` ORDER BY `marks` DESC LIMIT 3"
and it only return top 3 not the 4 results
i want to show top 3 results and if is there any results match with top 3 also fetched, in above case there is top 3 results are 50,40 and 30 but ram has also 30. So I want to fetch that result also if it is their along with top 3
I want to show top 3 results and if is there are any results which match with top 3, they should also be fetched.
SELECT t1.*
FROM table t1
JOIN ( SELECT result
FROM table t2
ORDER BY result DESC LIMIT 2,1 ) t3 ON t1.result >= t2.result
how to get top 5 results from database along with all matched results
Adjust LIMIT accordingly - LIMIT 4,1.
I have a rating system that takes the rating of 1-5 then increments the number of votes and adds to a total number of rating. This is problematic for my top 5 leaderboard which currently provides Xml through a php web service from the MySQL database with this query:
FROM ratings ORDER BY totalRating DESC LIMIT 5
Now obviously this isn't going to work.
Is there a way to do the division of totalRating by noRating and THEN return order by?
I'm trying to keep this to the database level. Hopefully it's possible.
Edit:
So let's say my table ratings has two records:
Name | cRating | tRating //current number of ratings and total
Tv1 | 2 | 10
Tv2 | 3 | 9
I need to do tRating / cRating then sort them into the top 5 shows
Desired result is to return the top 5 averagely rated results (sorry for the terrible formatting I'm on my phone)
You can use an arithmetic expression in the order by. So this might do what you want:
order by tRating / nullif(cRating, 0) desc
I already searched but I always find LEAST and GREATEST as hints. I want to have the next ascending number in a row that's not used. Like the following:
entries
1
2
3
5
6
7
If every of the numbers is for one row in my table I want the number 4 as a result and in the following example:
1
2
3
4
5
6
I want the number 7 as a result. Is there any possiblity to accomplish this in an SQL statement?
Best,
Robin
This query assumes that the number 1 is in your table
select min(number) + 1 from entries e1
where not exists (
select 1 from entries e2
where e2.number = e1.number + 1
)
If you want all missing numbers (where gaps are no larger than 1) instead of the smallest one, then remove min()
It think the solution is to do a self-join with the next value, and extract the first lowest result. Example:
Table: values, with column value
SELECT v1.value
FROM values v1
LEFT JOIN values v2 ON v1.value = (v2.value + 1)
WHERE v2.value IS NULL
ORDER BY v1.value ASC
LIMIT 1
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.
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.