I have article table
`id`,
`article_id`,
`stage_1_point`,
`stage_2_point`
I need top 10 articles based on
(stage_1_point+stage_2_point),
in the list and when i view any article i need to show its place.
My question is how can show its place without using order by.
USE ORDER BY (stage_1_point+stage_2_point) DESC
So, It will be like
SELECT `id`,
`article_id`,
`stage_1_point`,
`stage_2_point`
FROM YOUR_TABLE ORDER BY (stage_1_point+stage_2_point) DESC
LIMIT 0,10
UPDATE
As OP stated, he/she needs to know the position of specific article.
SELECT * FROM (SELECT `id`,
`article_id`,
`stage_1_point`,
`stage_2_point`,
#curRank := #curRank + 1 AS rank
FROM YOUR_TABLE, (SELECT #curRank := 0) r ORDER BY
(stage_1_point+stage_2_point) DESC) TAB
WHERE `article_id`=10
Above query will return rows for article_id 10, which will have a column Rank which tells the position of the article.
You can try below Query as below
Select
stage_1_point+stage_2_point as added_point,
columnsid,
article_id,
stage_1_point,
stage_2_point
FROM tablename
ORDER BY 1 desc
Limit 0,10
This will sort data based on column one result and fetch top 10 results only.
Related
I'm building a rank system, I have many horses and on the horse profiles I want to echo the rank of the horse depending on the amount of wins. 1st, 2nd, 3rd etc.
This is my MySQL table containing the horses, in PHP how would I count the rows to get the rank of the horse?
Here's my table, for instance if I was on "Pauls" profile, how would I echo out that he is in 4th position in the global ranking?
1.
select h.*, #rownum := #rownum + 1 AS rank
from horses h, (SELECT #rownum := 0) r
order by h.first DESC, h.second DESC, h.third DESC
2.
SELECT * FROM
(
select h.*, #rownum := #rownum + 1 AS rank
from horses h, (SELECT #rownum := 0) r
order by h.first DESC, h.second DESC, h.third DESC
) t
WHERE id = 428
Two queries to get position of your search record
set #name='Paul',#nr = 0, #namenr = 0;
select #namenr as nr from
(SELECT #nr:=#nr+1,if(name=#name, #namenr := #nr,#namenr=#namenr),name FROM SOME_TABLE order by first DESC ) subsel where subsel.name = #name;
second query will return one record with nr column which you are looking for.
Depending on your scoring system, this could be the right solution for you:
Say your 'first' means 3 points, 'second' means 2 points and and 'third' means 1 point.
This way you can calculate what horse has the most points based on the total.
Example:
Paul has 2 'first', 0 'second' and 1 'third', scoring him to 7 points.
Fletcher has 2 'first', no 'second' and no 'third', scoring him to 6 points.
Calculate the sum of all the points and order it by that field, DESC.
Example query:
SELECT (`first`*3)+(`second`*2)+(`third`*1) AS `total_score` FROM `horses` ORDER BY `total_score` DESC
Additional 'rank' field
SET #rank=0;
SELECT #rank:=#rank+1 AS `rank`, (`first`*3)+(`second`*2)+(`third`*1) AS `total_score` FROM `horses` ORDER BY `total_score` DESC
With the following query you can have your table, and next to it the rank of each horse:
SELECT *,
#curRank := #curRank + 1 AS rank
FROM (SELECT #curRank := 0) r, horses
order by first desc, second desc, third desc
I am using the (SELECT #curRank := 0) to initialize the curRank within the query
Use Order By on multiple columns according to priority.
$sql = "SELECT
#rownum := #rownum + 1 as row_number
FROM horses
CROSS JOIN (SELECT #rownum := 0) r
WHERE name = "Paul"
ORDER BY
first ASC,
second ASC,
third ASC";
now you can obtain the position value from query for any users.
To get the rank of a particular horse, you need only count the number of records that rank above it; sorting by row constructors is a very concise way of accomplishing this:
SELECT COUNT(*) + 1
FROM horses
WHERE (first, second, third)
> (SELECT first, second, third FROM horses WHERE id = ?)
I have a table which has the id column and score column, I want to sort the table based on score column and then find the specific user who is loading the page and show him his/her position. for example, tell him "your position is 40th".
Well I know how to sort a query:
SELECT id,score FROM `table` ORDER BY `score` DESC
But after the sort how can I find an specific id's position?
You don't need an order by for this. Instead:
select 1 + count(*)
from table t
where t.score > (select t2.score from table t2 where id = $id);
Try it:
SELECT #rownum:=#rownum+1 ‘rank’, id, score FROM table t, (SELECT #rownum:=0) r ORDER BY score DESC;
This will create a column and increase 1 in each record.
I have a query that gets me a users rank in a table of scores.
SELECT
*
FROM
(SELECT
*, #rank:=#rank + 1 rank
FROM
(SELECT
user_id, SUM(round_total) TotalPoints
FROM
sx14sp_mem_picks
GROUP BY user_id) s, (SELECT #rank:=0) init
ORDER BY TotalPoints DESC) r
WHERE
user_id = 22234
There is a problem with ties. I have a table field "pick_date" that i would like to use to break ties with. The user who made his picks first beats the tie.
Any ideas?
If sx14sp_mem_picks.pickdate is the field to break ties then in the order by sx14sp_mem_picks subquery, add
min( pickdate) asc
This will put the earliest pickdate first - you have to use MIN() bc you need to use an aggregate function given the use of "group by".
You need to order by the pick date in addition to the total points. However, you are talking about multiple rows per user. So, let's take the last pick date:
SELECT *
FROM (SELECT *, (#rank:=#rank + 1) as rank
FROM (SELECT user_id, SUM(round_total) as TotalPoints, max(pick_date) as max_pick_date
FROM sx14sp_mem_picks
GROUP BY user_id
) s CROSS JOIN
(SELECT #rank := 0) init
ORDER BY TotalPoints DESC, max_pick_date asc
) r
WHERE user_id = 22234;
Here is my query:
SELECT * FROM Photos WHERE Event_ID IN ($eventidstring)
I know I can limit the total amount of results from this query using LIMIT 5
I need the Limit the amount of results Per value in $eventidstring.
So if $eventidstring = 23,41,23*
*And there are 10 results WHERE Event_ID = 23, I want to limit this amount to 5. The same for all the other values in $eventidstring.
You may have some joy doing something similar to Oracle's RANK by PARITION in MySQL.
Sadly this feature is not available in MySQL though you can work around it using this method
Dump that in an inline view and then select those rows with rank <= 5;
Hence:
SELECT t.* FROM (
SELECT (
CASE Event_id
WHEN #curEventId
THEN #curRow := #curRow + 1
ELSE #curRow := 1 AND #curEventId := Event_Id END
) AS rank,
p.*
FROM Photos p INNER JOIN (SELECT #curRow := 0, #curEventId := '') r
ORDER BY p.Event_Id DESC
) t
WHERE t.rank <= 5 ORDER BY t.Event_Id asc;
Consider how you are going to 'choose' the top five by Event_Id too. You can always add in more after the ORDER BY p.Event_Id DESC to decide this.
I take it you're writing that query somewhere inside your PHP, so you need to split the $eventidstring into it's component values, form a SELECT for each and UNION all after the first one.
You sould do this with a loop of some sort, and concatenate the query strings in the loop...
If I understand correctly and you want to get five of each, you can use this:
(SELECT * FROM Photos WHERE Event_ID = 23 LIMIT 5)
UNION
(SELECT * FROM Photos WHERE Event_ID = 41 LIMIT 5)
UNION
(SELECT * FROM Photos WHERE Event_ID = ... LIMIT 5)
...
Maybe with a SELECT UNION but you need a new select for each value:
SELECT * FROM Photos WHERE Event_ID = 23 LIMIT 5
UNION SELECT * FROM Photos WHERE Event_ID = 41 LIMIT 5
UNION SELECT ...
Using PHP and MySQL, is there a way to use a different ORDER BY for each of the SELECT statements in a UNION?
SELECT * FROM the_table WHERE color = 'blue' ORDER BY price ASC LIMIT 5
UNION ALL
SELECT * FROM the_table WHERE color = 'red' ORDER BY RAND() LIMIT 10
The above statement does not work. It seems you can only do an ORDER BY on the final result set. Is there a way to do an ORDER BY on the first SELECT then a different ORDER BY on the second SELECT using UNION?
(SELECT * FROM the_table WHERE color = 'blue' ORDER BY price ASC LIMIT 5)
UNION ALL
(SELECT * FROM the_table WHERE color = 'red' ORDER BY RAND() LIMIT 10)
Please note that this does not work if you don't specify a LIMIT (though you can specify a very large dummy limit). See mysql documentation (13.2.7.3. UNION Syntax):
"Use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows...
"To cause rows in a UNION result to consist of the sets of rows retrieved by each SELECT one after the other, select an additional column in each SELECT to use as a sort column and add an ORDER BY following the last SELECT:
"(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
To additionally maintain sort order within individual SELECT results, add a secondary column to the ORDER BY clause:
"(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col, col1a;"