如何收集阵列数据?

时间:2010-08-24 02:38:35

标签: mysql arrays count group-by

由于我的知识有限,我有一个需要改进的mysql代码。我是php-mysql的新手。我的数据库中有数据数组,我想显示数据,如:

Date             Model        Qty         Name
2010-08-23       boo          2          Steve
2010-08-24       boo          1          Steve
2010-08-25       boo          2          David
2010-08-25       blob         1          Steve

我尝试过使用此代码,但结果并不像我想要的那样。

"SELECT id, DATE(A.Inspection_datetime) AS Date,
                  A.Model, COUNT(A.Serial_number) AS Qty,
                  B.name
                  FROM inspection_report AS A
                  LEFT JOIN Employee AS B
                  ON A.NIK=B.NIK
                  GROUP BY A.Model, B.name, A.Inspection_datetime"
results:
Date             Model        Qty         Name
2010-08-23       boo          1          Steve
2010-08-23       boo          1          Steve
2010-08-24       boo          1          Steve
2010-08-25       boo          1          David
2010-08-25       boo          1          David
2010-08-25       blob         1          Steve

如何解决此问题?

     CREATE TABLE IF NOT EXISTS `inspection_report` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `Model` varchar(14) NOT NULL,
          `Serial_number` varchar(8) NOT NULL,
          `Lot_no` varchar(6) NOT NULL,
          `Line` char(5) NOT NULL,
          `Shift` char(1) NOT NULL,
          `Inspection_datetime` datetime NOT NULL,
          `Range_sampling` varchar(19) NOT NULL,
          `NIK` int(5) NOT NULL,
          `Class` char(1) NOT NULL,
          `Status` varchar(6) NOT NULL,
          PRIMARY KEY (`id`),
          UNIQUE KEY `Model` (`Model`,`Serial_number`,`Lot_no`,`Line`)
        ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=44 ;

        CREATE TABLE IF NOT EXISTS `Employee` (
      `NIK` char(5) NOT NULL,
      `name` varchar(50) NOT NULL,
      PRIMARY KEY (`NIK`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

2 个答案:

答案 0 :(得分:0)

SELECT id, DATE(A.Inspection_datetime) AS Date,
                  A.Model, COUNT(A.Serial_number) AS Qty,
                  B.name
                  FROM inspection_report AS A
                  LEFT JOIN Employee AS B
                  ON A.NIK=B.NIK
                  GROUP BY Date,B.name

答案 1 :(得分:0)

     SELECT Date(i.Inspection_datetime) InspectionDate, 
            i.model, 
            e.name,
            Count(*) Qty
     FROM inspection_report i, Employee e
     WHERE i.nik = e.nik
     GROUP BY Date(i.Inspection_datetime), i.model, e.name