-3

你能告诉如何在 php 和 mysql 脚本中更改此结果:

  Model                  Class
Ball                        S
Book                        A
Spoon
Plate                       B
Box                         C

这是我的数据库:

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,      
  `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=48 ;

如果我想显示如下结果:

 Model           s       a       b       c
 Ball            1       0       0       0
 Book            0       1       0       0
 Spoon           0       0       0       0
 Plate           0       0       1       0
 Box             0       0       0       1

做这个的查询是什么?谢谢。

4

3 回答 3

1
SELECT `Model`,
IF(`Class`='S', 1, 0) AS `S`,
IF(`Class`='A', 1, 0) AS `A`,
IF(`Class`='B', 1, 0) AS `B`,
IF(`Class`='C', 1, 0) AS `C`
FROM `inspection_report`
于 2010-08-27T07:40:49.323 回答
0

你的问题有点不清楚,但我假设你有一个数组映射名称中的输入数据要缺陷,并且你希望每一行在适当的列中为 1,在其他任何地方都为 0。如果是这样,那就是这样的:

$arr = array('blue' => 'S', 'red' => 'A', 'yellow' => null, 'green' => 'B', 'black' => 'C');

$defects = array_filter(array_unique(array_values($arr)));
echo "name\t";
echo implode("\t", $defects);
echo "\n";

foreach($arr as $name => $defect) {
    echo "$name";
    foreach($defects as $test) {
        echo "\t";
        echo $test == $defect ? 1 : 0;
    }
    echo "\n";
}
于 2010-08-18T03:53:45.283 回答
0

非常粗略的例子,你可能会在现实中使用 HTML 表格。

<?php // $rows = array(array('name' => 'blue', 'class_defect' => 'S'), ...); ?>

<pre>
name      s  a  b  c
<?php
foreach ($rows as $row) {
    printf('%-10s', $row['name']);  // padding with spaces
    foreach (array('s', 'a', 'b', 'c') as $col) {
        echo (strtolower($row['class_defect']) == $col) ? 1 : 0;
        echo '  ';  // just padding again
    }
}
?>
</pre>
于 2010-08-18T03:58:38.470 回答