I have a table with columns:
id name rank rank_pts
1 Nick 0 15
2 N0NE 0 12
3 Non2 0 26
How can I set rank by rank_pts ?
You need a variable.
SET #i= 0;
UPDATE your_table SET rank = (#i:=#i+1) ORDER BY rank_pts;
update your_table
join (SELECT #row:= 0) r
set rank = (#row := #row + 1)
order by rank_pts desc;
Check out this answer to not only rank by rank_pts but how to handle the situation of two rank_pts being the same. Replace score with rank_pts
Related
I am not even sure if this has been answered because I don't even know how to coin the problem. But here is what am trying to do.
I am using COUNT() to create a tabular representation of a data from top to bottom for a 30 day period.
SELECT id FROM table WHERE col = '123' AND date >= DATE_SUB(CURRENT_DATE, INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY) AND date <= LAST_DAY(CURRENT_DATE) GROUP BY id ORDER BY COUNT(id) DESC
And I get the result with the most at the top
id | col
==========
id3 | 123
id5 | 123
id2 | 123
id4 | 123
id8 | 123
id5 | 123
id1 | 123
id9 | 123
id7 | 123
This works fine for a tabular view and I can use ol to create a numbering system from 1 - 10. My issue is, I want to be able to tell the position of any given id. Eg. if I want to get the position of id9 in this count result i.e. 8, how do I do that?
If you are using MySQL v8.0 or higher you can use the RANK function:
SELECT COUNT(*), RANK() OVER (ORDER BY COUNT(id) DESC) AS r FROM table GROUP BY id ORDER BY COUNT(id) DESC;
For previous version of mysql, you need to create the variable your self:
SELECT COUNT(*), #rank := #rank + 1 AS r FROM table, (SELECT #rank := 0) temp ORDER BY COUNT(id) DESC;
Note SELECT #rank := 0 initiate the variable.
Updated:
To select a specific id and it's rank, you can use:
SELECT * FROM (
SELECT id, COUNT(*), RANK() OVER (ORDER BY COUNT(id) DESC) AS r FROM table GROUP BY id ORDER BY COUNT(id) DESC
) ranked WHERE id = ?;
I need to match the multiple same column value of MySQL table using PHP and MySQL. I am explaining my table below.
db_user:
id status book_id
1 0 22
2 0 22
3 1 22
4 0 23
Here I need the select query and condition is if status=0 for same book_id means if table has lets say book_id=22 and all status=0 then only it will return value true otherwise false. I am writing one example below.
$sql=mysqli_query($connect,"select * from db_user where status=0 and....");
if(mysqli_num_rows($sql) > 0){
$row=mysqli_fetch_array($sql);
$data=$row['book_id'];
}else{
return 0;
}
Here as per the example table only last row will fetch because for book_id=22 there is status=1 present. The data will only fetch when for one book_id all status=0.
One option uses aggregation to check the status values:
SELECT book_id
FROM db_user
GROUP BY book_id
HAVING SUM(CASE WHEN status <> 0 THEN 1 ELSE 0 END) = 0;
We can also use EXISTS:
SELECT DISTINCT t1.book_id
FROM db_user t1
WHERE NOT EXISTS (SELECT 1 FROM db_user t2
WHERE t1.book_id = t2.book_id AND t2.status <> 0);
Back in the day, we would have solved it this way. I say, if it ain't broke, don't fix it...
SELECT DISTINCT x.*
FROM my_table x
LEFT
JOIN my_table y
ON y.book_id = x.book_id
AND y.status = 1
WHERE y.id IS NULL;
You could use a not in for the id with status 1
select *
from my_table
where book_id not in (
select book_id
from my_table
where status = 1 )
You can use below query.
select * from db_user where status=0 and book_id not in (select book_id from db_user where status = 1 );
I have tested it and it works.
its very simple:
SELECT book_id,MAX(status) max_status FROM db_user GROUP BY book_id
is max_status is 0 then all is 0 no larger than that.
I need to select code for 2 and 3 Highest value from the last entry.
SNO Userid Price
1 1004 10
2 1002 20
3 1001 300
4 1003 700
5 1002 80
6 1001 50
Now I need to select last entry price for 1001 , 1002 , 1003, 1004.
Result :
1001 - 50, 1002 - 80, 1003 -700, 1004 -10.
The second highest value is 80 user 1002
The third highest value is 50 user 1001
select * from price order by price desc limit 2,1
union
select * from price order by price asc limit 1
as far as i understood you want second,third highest and least min value.
Mmm , not sure I understood but try this:
SELECT t.*
FROM YourTable t
WHERE NOT EXISTS(SELECT 1 FROM YourTable s
WHERE t.userid = s.userid
AND s.sno > t.sno)
ORDER BY t.price DESC
LIMIT 2
Sample query :
SELECT * FROM table ORDER BY Price LIMIT 2 OFFSET 1 DESC
If I understand your problem !
You can achieve this by giving a row number based on the group by Userid and descending order of SNO.
Query
select t.`SNO`, t.`Userid`, t.`Price` from(
select `SNO`, `Userid`, `Price`, (
case Userid when #curA
then #curRow := #curRow + 1
else #curRow := 1 and #curA := `Userid` end
) as `rn`
from `your_table_name` t,
(select #curRow := 0, #curA := '') r
order by `Userid`, `SNO` desc
)t
where t.`rn` = 1;
We have calculated ranking using following query.
set #rank = 0;
Update rank_table
set position= (select #rank := #rank + 1)
order by points DESC, points_new DESC, points_old DESC;
And it calculates ranking in sequence.
eg.
I want that id's having same criteria should assign same ranking.
so let say,
here id 1 and 2 have same criteria then should have position=1 and then id 3 should have position=3.
Not like current flow.
Currently -> Id 1:position 1 , Id 2:position 2 , Id 3:position 3
but as Id 1 and 2 have same data it should be..
Id 1:position 1 , Id 2:position 1 , Id 3:position 3
Thanks for your help.
What you want is technically rank() rather than row_number(). This is something of a pain, but possible:
set #rn := 0;
set #rank := 0;
set #p := -1;
Update rank_table
set position = if(#rn := #rn + 1,
if(#p = points, #rank,
if(#p := points, #rank := #rn, #rank := #rn)
),
NULL -- never should happen
)
order by points DESC, points_new DESC, points_old DESC;
This is tricky because you need to calculate both the rank and the row number -- the rank stays the same and then it must "jump" to the row number.
H guys
I just need a select query which will have return new position column in it i.e
i want the result to be
id score position
23 345 1
09 309 2
12 123 3
I tried this but didn't work
(SELECT #rownum := #rownum + 1 AS position,
id,
score
FROM tabs
ORDER BY score DESC");
position row was empty, please help using php
SELECT #rownum := #rownum + 1 AS position,
id,
score
FROM tabs, (SELECT #rownum:=0) AS initrownum
ORDER BY score DESC
your query should be like :SELECT id, score FROM tabs ORDER BY score DESCand your php:for($i = 0; $i < sizeof($result); $i++){echo $i;echo $result['id'][$i];echo $result['score'][$i]//br the line here}