开发者

About Mysql Update!

I am doing college project in winform, mysql ,c#.

for that I have created mysql table with this structure,..

CREATE TABLE `attendance_monthly_rpt` (
  `id` int(15) NOT NULL AUTO_INCREMENT,
  `student_no` varchar(50) NOT NULL,
  `student_name` varchar(50) NOT NULL,
  `day1` varchar(15) NOT NULL,
  `day2` varchar(15) NOT NULL,
  `day3` varchar(15) NOT NULL,
  `day4` varchar(15) NOT NULL,
  `day5` varchar(15) NOT NULL,
  `day6` varchar(15) NOT NULL,
  `day7` varchar(15) NOT NULL,
  `day8` varchar(15) NOT NULL,
  `day9` varchar(15) NOT NULL,
  `day10` varchar(15) NOT NULL,
  `day11` varchar(15) NOT NULL,
  `day12` varchar(15) NOT NULL,
  `day13` varchar(15) NOT NULL,
  `day14` varchar(15) NOT NULL,
  `day15` varchar(15) NOT NULL,
  `day16` varchar(15) NOT NULL,
  `day17` varchar开发者_运维技巧(15) NOT NULL,
  `day18` varchar(15) NOT NULL,
  `day19` varchar(15) NOT NULL,
  `day20` varchar(15) NOT NULL,
  `day21` varchar(15) NOT NULL,
  `day22` varchar(15) NOT NULL,
  `day23` varchar(15) NOT NULL,
  `day24` varchar(15) NOT NULL,
  `day25` varchar(15) NOT NULL,
  `day26` varchar(15) NOT NULL,
  `day27` varchar(15) NOT NULL,
  `day28` varchar(15) NOT NULL,
  `day29` varchar(15) NOT NULL,
  `day30` varchar(15) NOT NULL,
  `day31` varchar(15) NOT NULL,
  `tot_persent` int(15) NOT NULL,
  `tot_absent` int(15) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

I update this table columns day1 to day31 with P,Ab,OD. Now i want to Update tot_persent in the logic of tot_absent to sum of Ab and sum of P and OD.

For example...

IHM22557001, Jegadeeswaran , Ab , P, Ab , OD, 0, 0 , OD , Ab , 0 , 0 , 0, Ab

tot_persent is 3 tot_absent is 4..

thanks in advance.


I'm going to suggest a different schema; day-based. If you have day-based, i.e.

Id, StudentNumber, Date, Status

Note that the Update tot_persent etc don't exist in pre-calculated form, since they are trivially available in the data.

Then the data can be obtained by the pretty clean:

SELECT   Date, Status
FROM     StudentAttendance
WHERE    StudentNumber = @no
AND      Date >= @start AND Date <= @end
ORDER BY Date

and for the aggregates

SELECT   Status, COUNT(1)
FROM     StudentAttendance
WHERE    StudentNumber = @no
AND      Date >= @start AND Date <= @end
GROUP BY Status

More-over, working with day-based records would also make it trivial to do the aggregates in your app layer rather than trying to do everything at the database - if you have the data for the days, you don't need the DB to do the aggregates (LINQ-to-Objects would be good, since you mention C#).

Personally I'd use an integer-based Status, but that is subjective. But mapping it to a C# enum would work very well, i.e.

public enum AttendanceStatus : byte {
    Present= 1,
    Absent = 2,
    ...your other values etc...
}

which would presumably by tinyint or similar at the DB. Most ORM/micro-ORM will map that for you automatically so you don't have to do anything clever there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜