2
我有一張gr_no,year_dob,family_id等的表。 我想根據family_id排名出生年份,並且無法生成siblings_rank結果。如何根據出生組別和出生年份獲得排名
+----------+--------------+-----------+
| gr_no | year_dob | family_id | siblings_rank
+----------+--------------+-----------+
| 1001 | 1992 | 95 | 1
| 10234 | 1995 | 95 | 2
| 10236 | 2004 | 96 | 1
| 15568 | 2006 | 96 | 2
| 1225 | 2004 | 92 | 1
+----------+--------------+-----------+
此查詢工作:
SET @prev := null;
SET @cnt := 1;
SELECT gr_no, gs_id, gf_id, year_dob, IF(@prev <> gf_id, @cnt := 1, @cnt := @cnt + 1) AS siblings_position, @prev := gf_id as previous_gf_id
FROM student_registered
ORDER BY gf_id, year_dob asc
該查詢也在努力:
SELECT gr_no, gs_id, gf_id, year_dob, IF(@prev <> gf_id, @cnt := 1, @cnt := @cnt + 1) AS siblings_position, @prev := gf_id as previous_gf_id
FROM student_registered
JOIN (SELECT @prev := null) p
JOIN (SELECT @cnt := 1) c
ORDER BY gf_id, year_dob asc
...............我不能用這些查詢創建視圖?
或 如果一個過程可以根據查詢更新student_registered列'siblings_position'?
共記錄6019和此查詢SELECT gr_no,gs_id得到同樣的結果,year_dob, gf_id, (SELECT COUNT(*)FROM T1 student_registered其中 = T1.gf_id T.gf_id 和T1.year_dob <= T.year_dob)作爲siblings_position FROM student_registeredŤ ORDER BY gf_id,year_dob ASC爲返回25個結果 – 2014-11-25 05:00:09
@atif Naseem,子查詢不會限制行,位置可能會像25一樣,你能證實嗎?試一下子查詢,看看? – radar 2014-11-25 13:03:58
@AtifNaseem,它工作? – radar 2014-12-01 03:40:57