I have a table
scores(user, score)
and I have this query
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as row_index, user, score
FROM scores ORDER BY score DESC
now I want to select, from this query result, the user with name 'john' and score '1400' to know what is his row_index, something like
SELECT row_index
FROM *result* WHERE user='john' AND score=1400
how do I do that? I tried
SET #row_num = 0;
SELECT row_index
FROM (SELECT #row_num := #row_num + 1 as row_index, user, score
FROM scores ORDER BY score DESC)
WHERE user='john' AND score=1400`
but phpMyAdmin says
#1248 - Every derived table must have its own alias
How can I do that?
Thank you,
Alessandro
You haven't added an alias to the derived table.
SELECT row_index FROM (...) AS alias
WHERE alias.user = 'john' AND alias.score = 1400
Query:
SET #row_num = 0;
SELECT a.row_index
FROM (SELECT #row_num := #row_num + 1 as row_index,
user,
score
FROM scores ) a
WHERE a.user='john' AND a.score=1400
ORDER BY a.score DESC
I think Ordering in Subquery not always allowed.
Try to use views.
SET #row_num = 0;
CREATE VIEW v AS SELECT #row_num := #row_num + 1 as row_index, user, score FROM scores ORDER BY score DESC;
SELECT row_index FROM v;
Try this:
SET #row_num = 0;
SELECT row_index FROM (SELECT #row_num := #row_num + 1 as row_index, user,
score FROM scores ORDER BY score DESC) AS alias1 WHERE user='john' AND score=1400
Related
I recently moved to a shared host that has MySQL 5.6.39 instead of MariaDB 10.x, I was wondering what would the equivalent of the following MariaDB statement in MySQL?
SELECT rank,
total
FROM
(SELECT ROW_NUMBER() OVER (
ORDER BY `prestige` DESC, `xp` DESC) AS rank,
(SELECT COUNT(*)
FROM Modular_LS) AS total,
steamid
FROM Modular_LS) sub
WHERE sub.steamid = '%s'
I got as far as this, but now I'm stuck
SELECT rank, total FROM
(SELECT #rank := #rank +1 as rank FROM Modular_LS,
(SELECT COUNT(*) FROM Modular_LS) AS total, steamid FROM Modular_LS) sub,
(SELECT #rank := 0) r ORDER BY `prestige` DESC, `xp` DESC) t;
The table structure contains the column steamid, xp, prestige
My goal is to order by prestige descending first and then xp descending to put it in a ranking like-order, then using WHERE query to find a specific player's ranking. The output of which contains the rank (position) and the total (total amount of records)
Maybe this will get you started:
SELECT #rank := IF(player_id = #prev, #rank + 1, 1), #prev := player_id
FROM ( SELECT #rank := 1, #prev = 0 ) AS init
JOIN ( SELECT player_id
FROM Modular_LS
ORDER BY prestige DESC, SP DESC
) AS x ;
After a few hours, this is what I came up with that solved my problem.
SELECT
sub.rank
,sub.total
FROM
(
SELECT
t.id
,t.steamid
,#rownum : = #rownum + 1 AS rank
,(
SELECT
COUNT (*)
FROM
Modular_LS
) AS total
FROM
Modular_LS t JOIN (
SELECT
#rownum : = 0
) r
ORDER BY
t.prestige DESC
,t.xp DESC
) sub
WHERE
sub.steamid = '%s'
I have a query that loops through each result and updates a column:
SET #counter = 0;
UPDATE users SET rank_level = #counter := #counter + 1 ORDER BY level DESC;
SELECT rank_level, level FROM users ORDER BY rank_level ASC;
Which outputs:
But what I am trying to do is only increment the variable if the level value changes. So where the two rows that have the same level are, they would have the same rank of 8 too.
Any ideas? Thanks.
rank() is a bit tricky in MySQL. One way is with a correlated subquery:
select u.*,
(select count(*) + 1
from users u2
where u2.level < u.level
) as rank
from users u;
This is tricky to put into an update. Assuming you have a userId column, you can use join:
update users u join
(select u.*,
(select count(*) + 1
from users u2
where u2.level < u.level
) as rank
from users u
) ur
on u.userId = ur.userId
set rank_level = rank;
Doing a rank with variables is rather tricky (row_number() and dense_rank() are easier), because it requires three variables. Here is the select version:
select u.*,
(#r := if(#l = level,
if(#rn := #rn + 1, #r, #r)
if(#l := level,
#rn := #rn + 1, #rn := #rn + 1
)
)
) as ranking
from users u cross join
(select #l := -1, #rn : = 0, #r := 0) params
order by level;
So in the end I went with this:
SET #prev_value = NULL;
SET #rank_count = 0;
UPDATE users SET rank_level = CASE
WHEN #prev_value = level THEN #rank_count
WHEN #prev_value := level THEN #rank_count := #rank_count + 1
END
ORDER BY level DESC;
Based on this answer: Rank function in MySQL
I am trying to generate row number for each row selected from my database but it seems that the row number follows the sequence of the table before it's arranged (order by).
Actual table
https://www.dropbox.com/s/otstzak20yxcgt6/test1.PNG?dl=0
After query
https://www.dropbox.com/s/i9jaoy04vq6u2zh/test2.PNG?dl=0
Code
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as Position, Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
Pls help. Looking forward to receiving replies from yall. Thanks!
Try an inner select, so the row number will be generated after the ORDER BY like so:
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as Position, s.*
FROM
(
SELECT
Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
) AS s;
I have table with id (store user id) and score in different match. I want what is the position of a user.
So for i try this sql fiddle;
in this I am getting all the row but I need only user having id 3 and it position in the table.
like this:
Score Postion
26 3
Even i try to do like this but no success
MySql: Find row number of specific record
With MySQL, how can I generate a column containing the record index in a table?
I got the answer: http://sqlfiddle.com/#!2/b787a/2
select * from (
select T.*,(#rownum := #rownum + 1) as rownum from (
select sum(score) as S,id from mytable group by id order by S desc ) as T
JOIN (SELECT #rownum := 0) r
) as w where id = 3
Updated sqlfiddle and above query. Now it is working perfectly.
I think this should do the trick:
SELECT totalScore, rownum FROM (
SELECT id,sum(score) AS totalScore,(#rownum := #rownum + 1) AS rownum
FROM mytable
JOIN (SELECT #rownum := 0) r
group by id) result
WHERE result.ID = 3;
just add a where clause
select x.id,x.sum,x.rownum
from(
select id,sum(score) as sum,(#rownum := #rownum + 1) as rownum
from mytable
JOIN (SELECT #rownum := 0) r
group by id
) x
where id =3
I have this Query:
SET #row_num = 0;
SELECT
(SELECT #row_num := #row_num + 1) AS itempurchase_code,
(SELECT supplier_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY supplier_code ORDER BY COUNT(*) DESC LIMIT 1) AS supplier_code,
(SELECT user_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY user_code ORDER BY COUNT(*) DESC LIMIT 1) AS user_code,
22 AS status_code,
item_invoicecodesupplier AS item_invoicecodesupplier,
(SELECT itempurchase_date FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY itempurchase_date ORDER BY COUNT(*) DESC LIMIT 1) AS itempurchase_date
FROM qa_items_purchases b
GROUP BY (item_invoicecodesupplier)
ORDER BY itempurchase_code;
I get this Result:
If you look there is no (2) itempurchase_code column, What i can do to show the numbers in sequence?
You must add the row number in an outer query if your query contains a GROUP BY.
SET #row_num = 0;
SELECT (SELECT #row_num := #row_num + 1) AS itempurchase_code, *
FROM
(
SELECT ... -- your original query goes here
) AS T1
ORDER BY itempurchase_code