根据日期为mysql数据提供行号

时间:2015-03-10 06:31:07

标签: c# mysql winforms row-number

我一直在尝试为我的数据添加一个行号,即每行都有一个基于我的排序的数字,如果我根据日期列出它们,那个最旧的日期将获得第1行,依此类推无论他们的实际id存储在数据库中。

我已经写了这个查询,但是如果我插入一个日期比之前的日期更早的列,它就不会像我想要的那样得到第1行。

这是我的问题:

SET @row_number=0; SELECT @row_number := @row_number +1 AS 'row number', physiotherapy_evaluation_form_id AS 'id',
CONCAT( student_first_name, ' ', student_second_name, ' ', student_third_name, ' ', student_last_name ) AS 'student name',  
therapist_first_name, date FROM physiotherapy_evaluation_form,student, therapist WHERE therapist_id = therapist_id_fk 
AND student_id_fk  =student_id AND  student_id = 2 AND date BETWEEN '2015-01-01' AND '2015-03-10' ORDER BY date ASC 

我正在使用C#和Windows表单。

我得到的是: unwanted output

我希望采用这种格式

row     date
1       2015-01-10
2       2015-02-02
3       2015-03-05

1 个答案:

答案 0 :(得分:1)

我能够通过单独查询与我的订单相关的查询并获取结果并添加到select row_number的第一个查询来解决它

这是更正后的代码,以防有人想知道:SET @row_number=0; SELECT @row_number := @row_number +1 AS 'row number', table1.* FROM (SELECT physiotherapy_evaluation_form_id AS 'id', CONCAT( student_first_name, ' ', student_second_name, ' ', student_third_name, ' ', student_last_name ) AS 'student name',
therapist_first_name, date FROM physiotherapy_evaluation_form,student, therapist WHERE therapist_id = therapist_id_fk AND student_id_fk =student_id AND student_id = 2 AND date BETWEEN '2015-01-01' AND '2015-03-10' ORDER BY date ASC ) AS table1