我仍然无法重现该问题。
以下示例的输出是
Books>Art>Modern
Books>Art>Concrete
Books>Art>Classicism
Books>Art>Naive
有两条记录有category=Books>Art>Classicism
. 第一个有status=0
并且被 WHERE 子句排除,而第二个有status=1
。您可以通过将第一条记录的 设置为to
来检查我的示例数据中是否没有拼写错误,您会看到它仍然只显示一次,但紧随其后作为第二条记录。status
0
1
Books>Art>Classicism
Books>Art>Modern
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);
$stmt = $pdo->query("SELECT DISTINCT category FROM soFoo WHERE status='1'", PDO::FETCH_ASSOC);
foreach( $stmt as $row ) {
echo join(', ', $row), "\n";
}
function setup($pdo) {
$pdo->exec("
CREATE TEMPORARY TABLE soFoo (
id int(11) NOT NULL AUTO_INCREMENT,
programname varchar(100) NOT NULL DEFAULT '',
sub0 varchar(300) DEFAULT NULL,
sub1 varchar(300) DEFAULT NULL,
sub2 varchar(300) DEFAULT NULL,
sub3 varchar(300) DEFAULT NULL,
sub4 varchar(300) DEFAULT NULL,
category varchar(300) DEFAULT NULL,
status varchar(8) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE= InnoDB DEFAULT CHARSET= latin1;
");
$stmt = $pdo->prepare('
INSERT INTO
soFoo
(
programname, sub0, sub1, sub2,
sub3, sub4,
category, status
)
VALUES
(
:programname, :sub0, :sub1, :sub2,
:sub3, :sub4,
:category, :status
)
');
$data = array();
$data[] = array(
'programname'=>'BOOK Store',
'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
'sub3'=>null, 'sub4'=>null,
'category'=>'Books>Art>Modern', 'status'=>'1'
);
// this one has category='Books>Art>Classicism' and status=0
// the second but last element is a copy of this one with status=1
$data[] = array(
'programname'=>'BOOK Store',
'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
'sub3'=>null, 'sub4'=>null,
'category'=>'Books>Art>Classicism', 'status'=>'0'
);
$data[] = array(
'programname'=>'BOOK Store',
'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
'sub3'=>null, 'sub4'=>null,
'category'=>'Books>Art>Modern', 'status'=>'1'
);
$data[] = array(
'programname'=>'BOOK Store',
'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
'sub3'=>null, 'sub4'=>null,
'category'=>'Books>Art>Concrete', 'status'=>'1'
);
$data[] = array(
'programname'=>'BOOK Store',
'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
'sub3'=>null, 'sub4'=>null,
'category'=>'Books>Art>Classicism', 'status'=>'1'
);
$data[] = array(
'programname'=>'BOOK Store',
'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
'sub3'=>null, 'sub4'=>null,
'category'=>'Books>Art>Naive', 'status'=>'1'
);
foreach( $data as $rec ) {
$stmt->execute($rec);
}
}