I have a mysql code that needs to be improved because of my limited knowledge. I am new in php-mysql.I have data arrays at my DB and I want to show data such as:
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
I have tried using this code, but the results is not like what I wanted.
"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
How do I do to resolve this problem?
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;
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
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
精彩评论