这是我的代码:
$result = $link->query($query);
//Get query results
$x = 0;
$tableA = [];
$tableB = [];
while($tables = $result->fetch(PDO::FETCH_ASSOC))
{
$test[] = $tables;
$assoc = $test[$x];
$tableName = $assoc['TABLE_NAME'];
$columnName = $assoc['COLUMN_NAME'];
$refTableName = $assoc['REFERENCED_TABLE_NAME'];
$refColumnName = $assoc['REFERENCED_COLUMN_NAME'];
$fkQueryA = "SELECT DISTINCT " . $columnName . " FROM " . $dbName . "." . $tableName . " ORDER BY ".$columnName." ;";
$fkQueryB = "SELECT DISTINCT " . $refColumnName . " FROM " . $dbName . "." . $refTableName . " ORDER BY ".$refColumnName." ;";
echo $fkQueryA."\n";
echo $fkQueryB."\n";
$resultA = $link->query($fkQueryA);
$resultB = $link->query($fkQueryB);
$tableA[] = $assoc['COLUMN_NAME'];
while($var = $resultA->fetchColumn())
{
$tableA[] = $var;
}
$tableB[] = $assoc['REFERENCED_COLUMN_NAME'];
while($vari = $resultB->fetchColumn())
{
$tableB[] = $vari;
}
$x++;
}
var_dump($tableA);
//var_dump($tableB);
代码在运行时返回此结果:
SELECT DISTINCT salesRepEmployeeNumber FROM classicmodels.Customers ORDER BY salesRepEmployeeNumber ;
SELECT DISTINCT employeeNumber FROM classicmodels.Employees ORDER BY employeeNumber ;
SELECT DISTINCT officeCode FROM classicmodels.Employees ORDER BY officeCode ;
SELECT DISTINCT officeCode FROM classicmodels.Offices ORDER BY officeCode ;
SELECT DISTINCT orderNumber FROM classicmodels.OrderDetails ORDER BY orderNumber ;
SELECT DISTINCT orderNumber FROM classicmodels.Orders ORDER BY orderNumber ;
SELECT DISTINCT productCode FROM classicmodels.OrderDetails ORDER BY productCode ;
SELECT DISTINCT productCode FROM classicmodels.Products ORDER BY productCode ;
SELECT DISTINCT customerNumber FROM classicmodels.Payments ORDER BY customerNumber ;
SELECT DISTINCT customerNumber FROM classicmodels.Customers ORDER BY customerNumber ;
array(545) {
[0]=>
string(22) "salesRepEmployeeNumber"
[1]=>
string(10) "officeCode"
[2]=>
string(1) "1"
[3]=>
string(1) "2"
[4]=>
string(1) "3"
[5]=>
.....and so on.
fetchColumn()
正在为我的查询结果的所有其他列返回正确的结果,而不是该列的信息。有关如何解决此问题的任何建议?
salesRepEmployeeNumber列中的数据:
mysql> SELECT DISTINCT salesRepEmployeeNumber FROM classicmodels.Customers; +------------------------+
| salesRepEmployeeNumber |
+------------------------+
| NULL |
| 1165 |
| 1166 |
| 1188 |
| 1216 |
| 1286 |
| 1323 |
| 1337 |
| 1370 |
| 1401 |
| 1501 |
| 1504 |
| 1611 |
| 1612 |
| 1621 |
| 1702 |
+------------------------+