Variabel di dalam mysql diawali dengan @ untuk variabel user (per sesi) dan @@ untuk variabel global. Urutan eksekusinya juga sesuai dengan letaknya, mulai dari kiri ke kanan.
program:
i := 1
j := i+1
mysql:
select @i:=1, @j:=@i+1
pemberian rangking model 1
No Nilai Rangking
1. 80,00 1
2. 80,00 2
3. 75,00 3
4. 75,00 4
5. 75,00 5
6. 75,00 6
7. 70,00 7
query:
select @i:=0, @j:=0;
select @j:=@j+1 as no, nis, nama, nilai, rangking from (select n.nis, s.nama, n.nilai, @i:=@i+1 as rangking from nilai n inner join siswa s on n.nis=s.nis where idtes = 't001' and id_kelas = 'k001' order by n.nilai desc, n.nis) x order by nis
pemberian rangking model 2
No Nilai Rangking
1. 80,00 1
2. 80,00 1
3. 75,00 2
4. 75,00 2
5. 75,00 2
6. 75,00 2
7. 70,00 3
query:
select @i:=0, @j:=0, @sebelum:=0;
select @j:=@j+1 as no, nis, nama, nilai, rangking from (select n.nis, s.nama, n.nilai, if(@sebelum<>n.nilai,@i:=@i+1,@i) as rangking, @sebelum:=n.nilai from nilai n inner join siswa s on n.nis=s.nis where idtes = 't001' and id_kelas = 'k001' order by n.nilai desc, n.nis) x order by nis
pemberian rangking model 3
No Nilai Rangking
1. 80,00 1
2. 80,00 1
3. 75,00 3
4. 75,00 3
5. 75,00 3
6. 75,00 3
7. 70,00 7
query:
select @i:=0, @j:=0, @sebelum:=0, @terakhir:=1;
select @j:=@j+1 as no, nis, nama, nilai, rangking from (select n.nis, s.nama, n.nilai, @i:=@i+1, if(@sebelum<>n.nilai,@terakhir:=@i,@terakhir), @terakhir as rangking, @sebelum:=n.nilai from nilai n inner join siswa s on n.nis=s.nis where idtes = 't001' and id_kelas = 'k001' order by n.nilai desc, n.nis) x order by nis
cara penggunaan:
1) di vb
Adodc1.RecordSource = "select @i:=0, ..."
Adodc1.Refresh
Adodc1.RecordSource = "select @j:=@j+1 as no, ... order by nis"
Adodc1.Refresh
2) di php
$sql = mysql_query("select @i:=0, ...");
$sql = mysql_query("select @j:=@j+1 as no, ... order by nis");
Tidak ada komentar:
Posting Komentar