This is my first question here so I hope im doing it right. I have the following SQL query:
SELECT *,
MATCH(name, descr) AGAINST ('$q') AS score
FROM songs
WHERE MATCH (name, descr) AGAINST('$q')
ORDER BY score DESC
As you may know, this query selects everything from every row from the songs table, if there is a match in the description or in the name .What I want to do,is to limit the query only to search the latest 10k rows for example. I also have a primary key, id.
You will need a nested query. Try this:
SELECT temp.*,
MATCH(name, descr) AGAINST ('$q') AS score
FROM (
SELECT *
FROM songs
ORDER BY id DESC
LIMIT 10000
) temp
WHERE MATCH (name, descr) AGAINST('$q')
ORDER BY score DESC
You can ORDER BY score ASC (instead of DESC) and add LIMIT 10000 and finally reverse the order of the result :
SELECT * FROM (
SELECT *,
MATCH(name, descr) AGAINST ('$q') AS score
FROM songs
WHERE MATCH (name, descr) AGAINST('$q')
ORDER BY score ASC
LIMIT 10000
) ORDER BY score DESC
Related
I want to select the average of the high score for 2-3 players but im not sure how to go about this.
Table Match has (id, player, score)
I would like to get the maximum score for each specified player (by id) and then calculate the average of these returned scores.
Any help would be appreciated
CREATE TABLE Match(Id integer PRIMARY KEY, score integer);
/* Create few records in this table */
INSERT INTO Match VALUES(1,'10');
INSERT INTO Match VALUES(2,'20');
INSERT INTO Match VALUES(3,'60');
INSERT INTO Match VALUES(4,'100');
INSERT INTO Match VALUES(5,'80');
COMMIT;
select m.*,
(select sum(score)
from (select score
from Match s
where s.id = s.id
order by score desc
limit 3
) s2
) as summax3
from Match m
The following query is going to return the highest score of each player:
SELECT *
FROM
(
SELECT `match`.id, `match`.score
FROM `match`
ORDER BY score DESC
LIMIT 3
) AS high_scores
GROUP BY id
ORDER BY score DESC
And for getting the average:
SELECT AVG(score) AS average
FROM
(
SELECT `match`.id, `match`.score
FROM `match`
ORDER BY score DESC
LIMIT 3
) AS high_scores
I have table in MySQL DB which contains among other things two fields user_id and score. This table is kind of log table so there can be multiple rows for one user_id with different scores. How can I get only top 10 users with highest score from this table?
SELECT DISTINCT user_id
FROM your_table
ORDER BY score DESC
LIMIT 10
EDIT:
SELECT DISTINCT *
FROM your_table
WHERE (user_id, score) IN (SELECT user_id, MAX(score) AS score
FROM your_table
GROUP BY user_id)
ORDER BY score DESC
LIMIT 10
SqlFiddleDemo
This is basic and you should put more effort; here is atemplate you can use -
SELECT TOP 10 distinct *
FROM people
WHERE names='SMITH'
ORDER BY names asc
I am having trouble writing a query for the following problem. I have tried some existing queries but cannot get the results I need.
I have a results table like this:
userid score timestamp
1 50 5000
1 100 5000
1 400 5000
1 500 5000
2 100 5000
3 1000 4000
The expected output of the query is like this:
userid score
3 1000
1 1000
2 100
I want to select a top list where I have n best scores summed for each user and if there is a draw the user with the lowest timestamp is highest. I really tried to look at all old posts but could not find one that helped me.
Here is what I have tried:
SELECT sum(score) FROM (
SELECT score
FROM results
WHERE userid=1 ORDER BY score DESC LIMIT 3
) as subquery
This gives me the results for one user, but I would like to have one query that fetches all in order.
This is a pretty typical greatest-n-per-group problem. When I see those, I usually use a correlated subquery like this:
SELECT *
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score) <= 3;
This is not the whole solution, as it only gives you the top three scores for each user in its own row. To get the total, you can use SUM() wrapped around that subquery like this:
SELECT userId, SUM(score) AS totalScore
FROM(
SELECT userId, score
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score) <= 3) tmp
GROUP BY userId;
Here is an SQL Fiddle example.
EDIT
Regarding the ordering (which I forgot the first time through), you can just order by totalScore in descending order, and then by MIN(timestamp) in ascending order so that users with the lowest timestamp appears first in the list. Here is the updated query:
SELECT userId, SUM(score) AS totalScore
FROM(
SELECT userId, score, timeCol
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score) <= 3) tmp
GROUP BY userId
ORDER BY totalScore DESC, MIN(timeCol) ASC;
and here is an updated Fiddle link.
EDIT 2
As JPW pointed out in the comments, this query will not work if the user has the same score for multiple questions. To settle this, you can add an additional condition inside the subquery to order the users three rows by timestamp as well, like this:
SELECT userId, SUM(score) AS totalScore
FROM(
SELECT userId, score, timeCol
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score
AND mT.timeCol <= m.timeCol) <= 3) tmp
GROUP BY userId
ORDER BY totalScore DESC, MIN(timeCol) ASC;
I am still working on a solution to find out how to handle the scenario where the userid, score, and timestamp are all the same. In that case, you will have to find another tiebreaker. Perhaps you have a primary key column, and you can choose to take a higher/lower primary key?
Query for selecting top three scores from table.
SELECT score FROM result
GROUP BY id
ORDER BY score DESC
LIMIT 3;
Can you please try this?
SELECT score FROM result GROUP BY id ORDER BY score DESC, timestamp ASC LIMIT 3;
if 2 users have same score then it will set order depends on time.
You can use a subquery
SELECT r.userid,
( SELECT sum(r2.score)
FROM results r2
WHERE r2.userid = r.userid
ORDER BY score DESC
LIMIT 3
) as sub
FROM result r
GROUP BY r.userid
ORDER BY sub desc
You should do it like this
SELECT SUM(score) as total, min(timestamp) as first, userid FROM scores
GROUP BY userid
ORDER BY total DESC, first ASC
This is way more efficient than sub queries. If you want to extract more fields than userid, then you need to add them to the group by.
This will of cause not limit the number of scores pr user, which indeed seems to require a subquery to solve.
In my current method, I use order by date in sql query, then set the back results as a new array, sort array by id via php.
SELECT * FROM table
WHERE
(
MATCH (title,content)
AGAINST ('+$search' IN BOOLEAN MODE)
)
order by date DESC
limit 0,10
I wanna to ask, Is there a sql method, first order by date get 10 result back then in these 10 results, order by id again, just in one sql query?
Wrap an outer query around your current query.
SELECT q.*
FROM (SELECT * FROM table
WHERE
(
MATCH (title,content)
AGAINST ('+$search' IN BOOLEAN MODE)
)
order by date DESC
limit 0,10) q
ORDER BY q.id
This might work:
SELECT * FROM (
SELECT * FROM table
WHERE ...
ORDER BY date DESC
LIMIT 10
)
ORDER BY id
Simply put a comma followed by another column to order by:
SELECT * FROM table
WHERE
(MATCH (title,content)
AGAINST ('+$search' IN BOOLEAN MODE))
order by date, id DESC
limit 0,10
I'm trying to write a query which will get the maximum scores for the most popular songs, in order of song popularity.
I have a table called "gameplay" with the following fields:
id
song_id
score
(plus some other arbitrary fields)
So far, the closest I've got is this:
SELECT id, song_id, score
FROM (
SELECT id, song_id, score
FROM (
SELECT id, song_id, score
FROM gameplay
ORDER BY score DESC
) AS all_scores_in_order
GROUP BY song_id
) AS top_scores_per_song
ORDER BY FIELD (song_id, 3,1,2)
But I would like the values in ORDER BY FIELD to be generated by another subquery - the song_id ranked by popularity (in order of counting row occurrences in table) ie.
SELECT song_id
FROM gameplay
GROUP BY song_id
ORDER BY count( id ) DESC
Can't you just group and then order by Count(id) in the outer query?
This should show all sorts sorted by the number of occurrences, and showing the max score:
SELECT song_id, COUNT(id), MAX(score)
FROM gameplay
GROUP BY song_id
ORDER count(id) DESC
Or do you want each song_id to appear several times?