powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / MySQL [игнор отключен] [закрыт для гостей] / finding rownum from result
4 сообщений из 4, страница 1 из 1
finding rownum from result
    #38404924
Shahriyar.R
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Dear Experts i have previously emulated Oracle like rownum in MySQL it works correctly before but at moment:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
set @row_num=0;

select
  @row_num:=@row_num+1,
  t.id,
  t.name,
  count(*) as total_matchs,
  sum(case when g.wins>0 then 1 else 0 end) as wins,
  sum(case when g.wins=0 then 1 else 0 end) as draw,
  sum(case when g.wins=0 then 1 else 0 end) as lost,
  sum(case sign(g.wins)
    when 1 then 3
    when 0 then 1
    else 0 end) as total_point,
  sum(g.GF) as GF,
  sum(g.GA)as GA,
  sum(g.GF)-sum(g.GA) as average
from
 (
  select 
    t.id, 
	l.match, 
	sum(case when t.id=l.team then 1 else -1 end) as wins,
	sum(case when t.id=l.team then 1 else 0 end) as GF,
	sum(case when t.id=l.team then 0 else 1 end) as GA
  from wd_teams t
  join wd_matches m on t.id in (m.home, m.away)
  join wd_timeline l on l.`match`=m.id and l.goal=1
  group by t.id, l.match
  )g
inner join wd_teams t on t.id=g.id
group by t.id, t.name
order by total_point desc,average  desc;



Output:




how i can fix it that it shows exact rownum from 1 to end?

You can check previous topic for table samples:

http://www.sql.ru/forum/1047702/how-to-find-overall-point-of-each-team-from-result

Thanks.
...
Рейтинг: 0 / 0
finding rownum from result
    #38405017
Фотография javajdbc
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Shahriyar.R,

It is expected "unexpected" behaviour.
Solution is rather simple -- move
@row_num:=@row_num+1 and ORDER BY
into yet another level of select:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
select
  @row_num:=@row_num+1,
  zz.* 
from
(
...your big select....
) zz
order by total_point desc,average  desc;



Observations like this are numerous:
moment of evaluation of @variables is
not strictly defined except for
simplest cases.

Apparently GROUP BY and ORDER BY on the same level
bring uncertainty here.

Divide and conquer :-)
...
Рейтинг: 0 / 0
finding rownum from result
    #38405306
Shahriyar.R
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
javajdbc,

Great forum , great people and ofcourse great experts! Thanks :)

Works like a charm:


Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
set @row_num=0;

select 
@row_num:=@row_num+1,
zz.*
from (
select
  t.id,
  t.name,
  count(*) as total_matchs,
  sum(case when g.wins>0 then 1 else 0 end) as wins,
  sum(case when g.wins=0 then 1 else 0 end) as draw,
  sum(case when g.wins=0 then 1 else 0 end) as lost,
  sum(case sign(g.wins)
    when 1 then 3
    when 0 then 1
    else 0 end) as total_point,
  sum(g.GF) as GF,
  sum(g.GA)as GA,
  sum(g.GF)-sum(g.GA) as average
from
 (
  select 
    t.id, 
	l.match, 
	sum(case when t.id=l.team then 1 else -1 end) as wins,
	sum(case when t.id=l.team then 1 else 0 end) as GF,
	sum(case when t.id=l.team then 0 else 1 end) as GA
  from wd_teams t
  join wd_matches m on t.id in (m.home, m.away)
  join wd_timeline l on l.`match`=m.id and l.goal=1
  group by t.id, l.match
  )g
inner join wd_teams t on t.id=g.id
group by t.id, t.name) as zz
order by zz.total_point desc,zz.average  desc;
...
Рейтинг: 0 / 0
finding rownum from result
    #38405323
Shahriyar.R
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Also rewrite this for using without "set @@row_num=0"

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
select 
@row_num:=@row_num+1 as rownum,
zz.*
from (
select
  t.id,
  t.name,
  count(*) as total_matchs,
  sum(case when g.wins>0 then 1 else 0 end) as wins,
  sum(case when g.wins=0 then 1 else 0 end) as draw,
  sum(case when g.wins=0 then 1 else 0 end) as lost,
  sum(case sign(g.wins)
    when 1 then 3
    when 0 then 1
    else 0 end) as total_point,
  sum(g.GF) as GF,
  sum(g.GA)as GA,
  sum(g.GF)-sum(g.GA) as average
from
 (
  select 
    t.id, 
	l.match, 
	sum(case when t.id=l.team then 1 else -1 end) as wins,
	sum(case when t.id=l.team then 1 else 0 end) as GF,
	sum(case when t.id=l.team then 0 else 1 end) as GA
  from wd_teams t
  join wd_matches m on t.id in (m.home, m.away)
  join wd_timeline l on l.`match`=m.id and l.goal=1
  group by t.id, l.match
  )g
inner join wd_teams t on t.id=g.id
group by t.id, t.name) as zz
inner join
(select @row_num:=0) as r
order by zz.total_point desc,zz.average  desc
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / MySQL [игнор отключен] [закрыт для гостей] / finding rownum from result
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]