我有这样的数据:
id | md_name | total_visit
===+==============+============
1 | Nunu Nugraha | 33
2 | Erwin | 32
3 | Tri Sulistyo | 35
4 | Risdianto | 24
5 | Erma | 22
6 | Dwi Sabana | 19
7 | Ernayanti | 26
8 | Ali | 10
9 | Partini | 13
我用这样的联接代码取得了以上结果:
SELECT datamd.id as id,
datamd.nama_md as md_name,
COUNT(R.id) as total_visit
FROM datamd
LEFT JOIN
(
SELECT id, idmd
FROM rincian_kunjungan WHERE status='1' AND MONTH(tanggal_kunjungan)='$bulan' AND YEAR(tanggal_kunjungan)='$tahun'
) AS R
ON datamd.id = R.idmd WHERE status=1 AND level=8 GROUP BY datamd.id ORDER BY datamd.id
我想成为这样:
rank | id | md_name | total_visit
=====+====+==============+============
1 | 3 | Tri Sulistyo | 35
2 | 1 | Nunu Nurgaha | 33
3 | 2 | Erwin | 32
4 | 7 | Ernayanti | 26
5 | 4 | Risdianto | 24
6 | 5 | Erma | 22
7 | 6 | Dwi Sabana | 19
8 | 9 | Partini | 13
9 | 8 | Ali | 10
这里的朋友可以帮我吗,我尝试使用下面的代码,但是在排名列中不合适
SET @number = 0;
SELECT @number:=@number+1 as rank, datamd.id as id,
datamd.nama_md as md_name,
COUNT(R.id) as total_visit
FROM datamd
LEFT JOIN
(
SELECT id, idmd
FROM rincian_kunjungan WHERE status='1' AND MONTH(tanggal_kunjungan)='$bulan' AND YEAR(tanggal_kunjungan)='$tahun'
) AS R
ON datamd.id = R.idmd WHERE status=1 AND level=8 GROUP BY datamd.id ORDER BY rank ASC
答案 0 :(得分:1)
请尝试使用此查询:
我创建了一个表测试,并插入了这样的记录:
select * from test ;
+----+--------------+-------------+
| id | md_name | total_visit |
+----+--------------+-------------+
| 1 | Nunu Nugraha | 33 |
| 2 | Erwin | 32 |
| 3 | Tri Sulistyo | 35 |
| 4 | Risdianto | 24 |
| 5 | Erma | 22 |
| 6 | Dwi Sabana | 19 |
| 7 | Ernayanti | 26 |
| 8 | Ali | 10 |
| 9 | Partini | 13 |
+----+--------------+-------------+
我已经开发了查询:
SELECT @rownum := @rownum +1 rank, id, md_name, total_visit
FROM `test` , (SELECT @rownum :=0)r
ORDER BY total_visit DESC
LIMIT 0 , 30
got this result :
+------+----+--------------+-------------+
| rank | id | md_name | total_visit |
+------+----+--------------+-------------+
| 1 | 3 | Tri Sulistyo | 35 |
| 2 | 1 | Nunu Nugraha | 33 |
| 3 | 2 | Erwin | 32 |
| 4 | 7 | Ernayanti | 26 |
| 5 | 4 | Risdianto | 24 |
| 6 | 5 | Erma | 22 |
| 7 | 6 | Dwi Sabana | 19 |
| 8 | 9 | Partini | 13 |
| 9 | 8 | Ali | 10 |
答案 1 :(得分:1)
您可以整天对抗用户变量,或者在MySQL中模拟密集排名函数,如下所示:
SELECT main.id, main.md_name, main.total_visit, COUNT(DISTINCT prev.total_visit) + 1 AS rank
FROM datamd AS main
LEFT JOIN datamd AS prev ON prev.total_visit > main.total_visit
GROUP BY main.id, main.md_name, main.total_visit
ORDER BY rank
用原始查询中的子查询替换上面查询中的“表”:
SELECT datamd.id, datamd.nama_md, main.total_visit, COUNT(DISTINCT prev.total_visit) + 1 AS rank
FROM datamd
LEFT JOIN (
SELECT idmd, COUNT(*) AS total_visit
FROM rincian_kunjungan
WHERE status = '1' AND MONTH(tanggal_kunjungan) = $bulan AND YEAR(tanggal_kunjungan) = $tahun
GROUP BY idmd
) AS main ON datamd.id = main.idmd
LEFT JOIN (
SELECT COUNT(*) AS total_visit
FROM rincian_kunjungan
WHERE status = '1' AND MONTH(tanggal_kunjungan) = $bulan AND YEAR(tanggal_kunjungan) = $tahun
GROUP BY idmd
) AS prev ON prev.total_visit > main.total_visit
GROUP BY datamd.id, datamd.nama_md, main.total_visit
ORDER BY rank